github.com/insolar/x-crypto@v0.0.0-20191031140942-75fab8a325f6/rand/rand_js.go (about) 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 package rand 8 9 import "syscall/js" 10 11 func init() { 12 Reader = &reader{} 13 } 14 15 var jsCrypto = js.Global().Get("crypto") 16 17 // reader implements a pseudorandom generator 18 // using JavaScript crypto.getRandomValues method. 19 // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues. 20 type reader struct{} 21 22 func (r *reader) Read(b []byte) (int, error) { 23 a := js.TypedArrayOf(b) 24 jsCrypto.Call("getRandomValues", a) 25 a.Release() 26 return len(b), nil 27 }