Debian Codegraph @

libgo/go/crypto/rand/rand_js.go

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 //go: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 var uint8Array = js.Global().Get("Uint8Array")
17
18 // reader implements a pseudorandom generator
19 // using JavaScript crypto.getRandomValues method.
20 // See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues.
21 type reader struct{}
22
23 func (r *reader) Read(b []byte) (int, error) {
24 a := uint8Array.New(len(b))
25 jsCrypto.Call("getRandomValues", a)
26 js.CopyBytesToGo(b, a)
27 return len(b), nil
28 }