-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.go
142 lines (110 loc) · 2.95 KB
/
lib.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"crypto/ed25519"
"encoding/base64"
"fmt"
"syscall/js"
"github.com/keybase/saltpack"
"github.com/keybase/saltpack/basic"
)
func JsResult(res js.Value, err js.Value) js.Value {
return js.ValueOf([]interface{}{res, err})
}
func JsError(err error) js.Value {
errCon := js.Global().Get("Error")
return JsResult(js.Null(), errCon.New(err.Error()))
}
func JsOk(val interface{}) js.Value {
return JsResult(js.ValueOf(val), js.Null())
}
func KeyGen(this js.Value, args []js.Value) interface{} {
keyring := basic.NewKeyring()
signer, err := keyring.GenerateSigningKey()
if err != nil {
return JsError(err)
}
signingVal := wrapSigningKey(signer)
return JsOk(map[string]interface{}{
"signing": signingVal,
})
}
func wrapSigningKey(key *basic.SigningSecretKey) js.Value {
enc := base64.StdEncoding
signingSecretKey := *key.GetRawSecretKey()
signingPublicKey := *key.GetRawPublicKey()
return js.ValueOf(map[string]interface{}{
"type": js.ValueOf("signing"),
"public": enc.EncodeToString(signingPublicKey[:]),
"private": enc.EncodeToString(signingSecretKey[:]),
})
}
func unwrapSigningPubKey(val js.Value) (*basic.SigningPublicKey, error) {
enc := base64.StdEncoding
pubB, err := enc.DecodeString(val.String())
if err != nil {
return nil, err
}
var pub [ed25519.PublicKeySize]byte
for i, v := range pubB {
pub[i] = v
}
key := basic.NewSigningPublicKey(&pub)
return &key, nil
}
func unwrapSigningKey(val js.Value) (*basic.SigningSecretKey, error) {
enc := base64.StdEncoding
priStr := val.Get("private").String()
priB, err := enc.DecodeString(priStr)
if err != nil {
return nil, err
}
var pri [ed25519.PrivateKeySize]byte
for i, v := range priB {
pri[i] = v
}
pubStr := val.Get("public").String()
pubB, err := enc.DecodeString(pubStr)
if err != nil {
return nil, err
}
var pub [ed25519.PublicKeySize]byte
for i, v := range pubB {
pub[i] = v
}
key := basic.NewSigningSecretKey(&pub, &pri)
return &key, nil
}
func SignValue(this js.Value, args []js.Value) interface{} {
signer, err := unwrapSigningKey(args[0])
if err != nil {
return JsError(err)
}
msg := []byte(args[1].String())
signed, err := saltpack.SignArmor62(saltpack.CurrentVersion(), msg, *signer, "")
if err != nil {
return JsError(err)
}
return JsOk(signed)
}
func VerifyValue(this js.Value, args []js.Value) interface{} {
keyring := basic.NewKeyring()
key, err := unwrapSigningPubKey(args[0])
if err != nil {
return JsError(err)
}
signed := args[1].String()
foundKey, verifiedMsg, _, err := saltpack.Dearmor62Verify(saltpack.CheckKnownMajorVersion, signed, keyring)
if err != nil {
return JsError(err)
}
if !saltpack.PublicKeyEqual(key, foundKey) {
return JsError(fmt.Errorf("keys do not match"))
}
return JsOk(string(verifiedMsg))
}
func main() {
js.Global().Set("keyGen", js.FuncOf(KeyGen))
js.Global().Set("signValue", js.FuncOf(SignValue))
js.Global().Set("verifyValue", js.FuncOf(VerifyValue))
select {}
}