Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Prev Previous commit
Next Next commit
compiler/natives/src/crypto/rand factorize Crypto interface access
  • Loading branch information
r-l-x committed Apr 20, 2018
commit fb758f01fdcc7a9fdf5455e9123c6f1e4c0ccb0c
25 changes: 10 additions & 15 deletions compiler/natives/src/crypto/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"

"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/gopherjs/js/webcrypto"
)

func init() {
Expand All @@ -19,22 +20,16 @@ func (r *rngReader) Read(b []byte) (n int, err error) {
offset := js.InternalObject(b).Get("$offset").Int()

// browser
crypto := js.Global.Get("crypto")
if crypto == js.Undefined {
crypto = js.Global.Get("msCrypto")
}
if crypto != js.Undefined {
if crypto.Get("getRandomValues") != js.Undefined {
n = len(b)
if n > 65536 {
// Avoid QuotaExceededError thrown by getRandomValues
// when length is more than 65536, as specified in
// http://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
n = 65536
}
crypto.Call("getRandomValues", array.Call("subarray", offset, offset+n))
return n, nil
if webcrypto.Crypto != nil && webcrypto.Crypto.Get("getRandomValues") != js.Undefined {
n = len(b)
if n > 65536 {
// Avoid QuotaExceededError thrown by getRandomValues
// when length is more than 65536, as specified in
// http://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
n = 65536
}
webcrypto.Crypto.Call("getRandomValues", array.Call("subarray", offset, offset+n))
return n, nil
}

// Node.js
Expand Down