-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.go
56 lines (51 loc) · 1.28 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package webcrypto
import (
"fmt"
"github.com/gopherjs/gopherjs/js"
)
var subtleCrypto *js.Object
func init() {
crypto := new(js.Object)
// default: crypto, IE: msCrypto
for _, m := range []string{"crypto", "msCrypto"} {
crypto = js.Global.Get(m)
if crypto != js.Undefined {
break
}
}
if crypto == js.Undefined {
return
}
subtle := new(js.Object)
// default: subtle, safari: webkitSubtle
for _, m := range []string{"subtle", "webkitSubtle"} {
subtle = crypto.Get(m)
if subtle != js.Undefined {
break
}
}
subtleCrypto = subtle
}
// Call calls the object's method with the given name and arguments.
func Call(method string, args ...interface{}) (res *js.Object, err error) {
if subtleCrypto == nil {
return nil, fmt.Errorf("unable to find js.crypto.subtle")
}
if subtleCrypto.Get(method) == js.Undefined {
return nil, fmt.Errorf("unable to find js.crypto.subtle.%v", method)
}
chanRes := make(chan *js.Object, 1)
chanErr := make(chan *js.Object, 1)
promise := subtleCrypto.Call(method, args...)
promise.Call(
"then",
func(res *js.Object) { chanRes <- res },
func(err *js.Object) { chanErr <- err },
)
select {
case jsres := <-chanRes:
return jsres, nil
case jserr := <-chanErr:
return nil, fmt.Errorf("js error: %s", jserr.Get("name").String())
}
}