forked from ssbc/ssb-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (140 loc) · 4.12 KB
/
index.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict'
var sodium = require('chloride')
var pb = require('private-box')
var u = require('./util')
var isBuffer = Buffer.isBuffer
//UTILS
function clone (obj) {
var _obj = {}
for(var k in obj) {
if(Object.hasOwnProperty.call(obj, k))
_obj[k] = obj[k]
}
return _obj
}
var hmac = sodium.crypto_auth
exports.hash = u.hash
exports.getTag = u.getTag
function isObject (o) {
return 'object' === typeof o
}
function isFunction (f) {
return 'function' === typeof f
}
function isString(s) {
return 'string' === typeof s
}
var curves = {}
curves.ed25519 = require('./sodium')
curves.secp256k1 = require('./secp256k1')
function getCurve(keys) {
var curve = keys.curve
if(!keys.curve && isString(keys.public))
keys = keys.public
if(!curve && isString(keys))
curve = u.getTag(keys)
if(!curves[curve]) {
throw new Error(
'unkown curve:' + curve +
' expected: '+Object.keys(curves)
)
}
return curve
}
//this should return a key pair:
// {curve: curve, public: Buffer, private: Buffer}
exports.generate = function (curve, seed) {
curve = curve || 'ed25519'
if(!curves[curve])
throw new Error('unknown curve:'+curve)
return u.keysToJSON(curves[curve].generate(seed), curve)
}
//import functions for loading/saving keys from storage
var storage = require('./storage')(exports.generate)
for(var key in storage) exports[key] = storage[key]
exports.loadOrCreate = function (filename, curve, cb) {
if (isFunction(curve))
cb = curve, curve = null
exports.load(filename, function (err, keys) {
if(!err) return cb(null, keys)
exports.create(filename, curve, cb)
})
}
exports.loadOrCreateSync = function (filename, curve) {
try {
return exports.loadSync(filename)
} catch (err) {
return exports.createSync(filename, curve)
}
}
//takes a public key and a hash and returns a signature.
//(a signature must be a node buffer)
function sign (keys, msg) {
if(isString(msg))
msg = new Buffer(msg)
if(!isBuffer(msg))
throw new Error('msg should be buffer')
var curve = getCurve(keys)
return curves[curve]
.sign(u.toBuffer(keys.private || keys), msg)
.toString('base64')+'.sig.'+curve
}
//takes a public key, signature, and a hash
//and returns true if the signature was valid.
function verify (keys, sig, msg) {
if(isObject(sig))
throw new Error('signature should be base64 string, did you mean verifyObj(public, signed_obj)')
return curves[getCurve(keys)].verify(
u.toBuffer(keys.public || keys),
u.toBuffer(sig),
isBuffer(msg) ? msg : new Buffer(msg)
)
}
// OTHER CRYTPO FUNCTIONS
exports.signObj = function (keys, hmac_key, obj) {
if(!obj) obj = hmac_key, hmac_key = null
var _obj = clone(obj)
var b = new Buffer(JSON.stringify(_obj, null, 2))
if(hmac_key) b = hmac(b, u.toBuffer(hmac_key))
_obj.signature = sign(keys, b)
return _obj
}
exports.verifyObj = function (keys, hmac_key, obj) {
if(!obj) obj = hmac_key, hmac_key = null
obj = clone(obj)
var sig = obj.signature
delete obj.signature
var b = new Buffer(JSON.stringify(obj, null, 2))
if(hmac_key) b = hmac(b, u.toBuffer(hmac_key))
return verify(keys, sig, b)
}
exports.box = function (msg, recipients) {
msg = new Buffer(JSON.stringify(msg))
recipients = recipients.map(function (keys) {
return sodium.crypto_sign_ed25519_pk_to_curve25519(u.toBuffer(keys.public || keys))
})
return pb.multibox(msg, recipients).toString('base64')+'.box'
}
exports.unboxKey = function (boxed, keys) {
boxed = u.toBuffer(boxed)
var sk = sodium.crypto_sign_ed25519_sk_to_curve25519(u.toBuffer(keys.private || keys))
return pb.multibox_open_key(boxed, sk)
}
exports.unboxBody = function (boxed, key) {
if(!key) return null
boxed = u.toBuffer(boxed)
key = u.toBuffer(key)
var msg = pb.multibox_open_body(boxed, key)
try {
return JSON.parse(''+msg)
} catch (_) { }
}
exports.unbox = function (boxed, keys) {
boxed = u.toBuffer(boxed)
try {
var sk = sodium.crypto_sign_ed25519_sk_to_curve25519(u.toBuffer(keys.private || keys))
var msg = pb.multibox_open(boxed, sk)
return JSON.parse(''+msg)
} catch (_) { }
return
}