Source file src/crypto/internal/sysrand/rand_js.go

     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  package sysrand
     6  
     7  // The maximum buffer size for crypto.getRandomValues is 65536 bytes.
     8  // https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#exceptions
     9  const maxGetRandomRead = 64 << 10
    10  
    11  //go:wasmimport gojs runtime.getRandomData
    12  //go:noescape
    13  func getRandomValues(r []byte)
    14  
    15  // read calls the JavaScript Crypto.getRandomValues() method.
    16  // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues.
    17  func read(b []byte) error {
    18  	for len(b) > 0 {
    19  		size := len(b)
    20  		if size > maxGetRandomRead {
    21  			size = maxGetRandomRead
    22  		}
    23  		getRandomValues(b[:size])
    24  		b = b[size:]
    25  	}
    26  	return nil
    27  }
    28  

View as plain text