rand_wasm.mx raw

   1  // Copyright 2018 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  //:build js && wasm
   6  
   7  // This implementation of crypto/rand takes its entropy from the host's
   8  // cryptographically secure generator (crypto.getRandomValues in the browser),
   9  // reached through crypto/internal/sysrand's wasm implementation.
  10  
  11  package rand
  12  
  13  import (
  14  	"io"
  15  
  16  	"crypto/internal/sysrand"
  17  )
  18  
  19  func _reader() io.Reader { return &reader{} }
  20  
  21  type reader struct{}
  22  
  23  func (r *reader) Read(b []byte) (n int32, err error) {
  24  	if len(b) == 0 {
  25  		return 0, nil
  26  	}
  27  	sysrand.Read(b)
  28  	return int32(len(b)), nil
  29  }
  30