]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rand, crypto/x509: add js/wasm architecture
authorRichard Musiol <mail@richard-musiol.de>
Sun, 4 Mar 2018 12:38:08 +0000 (13:38 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 3 May 2018 18:00:24 +0000 (18:00 +0000)
This commit adds the js/wasm architecture to the crypto packages.

Updates #18892

Change-Id: Id41a9d54920746d5019cbeedcff1b83874f2ef73
Reviewed-on: https://go-review.googlesource.com/110095
Reviewed-by: Austin Clements <austin@google.com>
src/crypto/rand/rand.go
src/crypto/rand/rand_js.go [new file with mode: 0644]
src/crypto/x509/root_js.go [new file with mode: 0644]
src/crypto/x509/root_unix.go

index 6f7523d9d733853d8f1722a53b7466ad8e7de5c5..e80ad368a22246a02faaeef7dd0c48ce6ec97467 100644 (file)
@@ -15,6 +15,7 @@ import "io"
 // On OpenBSD, Reader uses getentropy(2).
 // On other Unix-like systems, Reader reads from /dev/urandom.
 // On Windows systems, Reader uses the CryptGenRandom API.
+// On Wasm, Reader uses the Web Crypto API.
 var Reader io.Reader
 
 // Read is a helper function that calls Reader.Read using io.ReadFull.
diff --git a/src/crypto/rand/rand_js.go b/src/crypto/rand/rand_js.go
new file mode 100644 (file)
index 0000000..bc54ccd
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build js,wasm
+
+package rand
+
+import "syscall/js"
+
+func init() {
+       Reader = &reader{}
+}
+
+var jsCrypto = js.Global.Get("crypto")
+
+// reader implements a pseudorandom generator
+// using JavaScript crypto.getRandomValues method.
+// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues.
+type reader struct{}
+
+func (r *reader) Read(b []byte) (int, error) {
+       jsCrypto.Call("getRandomValues", b)
+       return len(b), nil
+}
diff --git a/src/crypto/x509/root_js.go b/src/crypto/x509/root_js.go
new file mode 100644 (file)
index 0000000..70abb73
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build js,wasm
+
+package x509
+
+// Possible certificate files; stop after finding one.
+var certFiles = []string{}
index 115af6b2f764730f0eb9a2c4afc08b5c30fd74da..8e7036234d7d3d65a134418681ddb01aaad71f2e 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build dragonfly freebsd linux nacl netbsd openbsd solaris
+// +build dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
 
 package x509