| 11 | type reader struct{} |
| 12 | |
| 13 | func (r *reader) Read(b []byte) (n int, err error) { |
| 14 | array := js.InternalObject(b).Get("$array") |
| 15 | offset := js.InternalObject(b).Get("$offset").Int() |
| 16 | |
| 17 | // browser |
| 18 | crypto := js.Global.Get("crypto") |
| 19 | if crypto == js.Undefined { |
| 20 | crypto = js.Global.Get("msCrypto") |
| 21 | } |
| 22 | if crypto != js.Undefined { |
| 23 | if crypto.Get("getRandomValues") != js.Undefined { |
| 24 | n = len(b) |
| 25 | if n > 65536 { |
| 26 | // Avoid QuotaExceededError thrown by getRandomValues |
| 27 | // when length is more than 65536, as specified in |
| 28 | // http://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues |
| 29 | n = 65536 |
| 30 | } |
| 31 | crypto.Call("getRandomValues", array.Call("subarray", offset, offset+n)) |
| 32 | return n, nil |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Node.js |
| 37 | if require := js.Global.Get("require"); require != js.Undefined { |
| 38 | if randomBytes := require.Invoke("crypto").Get("randomBytes"); randomBytes != js.Undefined { |
| 39 | array.Call("set", randomBytes.Invoke(len(b)), offset) |
| 40 | return len(b), nil |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return 0, errors.New("crypto/rand not available in this environment") |
| 45 | } |