git ssb

1+

Dominic / ssb-keys



Commit 45461343ddedfb725b5199f266c25dd2cca752fc

added signObj, verifyObj, signObjHmac, and verifyObjHmac

Paul Frazee committed on 11/30/2014, 8:28:21 PM
Parent: 636ed354c6f8428f86e8fe9aa7ba2796ca043152

Files changed

README.mdchanged
index.jschanged
README.mdView
@@ -45,8 +45,24 @@
4545 var hash = ssbkeys.hash(new Buffer('deadbeef', 'hex'))
4646 ssbkeys.isHash(hash) // => true
4747
4848 var sig = ssbkeys.sign(k, hash)
49-ssbkeys.verify(k.public, sig, hash)
49+ssbkeys.verify(k.public, sig, hash) // => true
5050
51-ssbkeys.hmac(new Buffer('deadbeef', 'hex'), k.private) // => String
51+var secret = new Buffer('deadbeef', 'hex')
52+ssbkeys.hmac(secret, k.private) // => String
53+
54+var obj = ssbkeys.signObj(k, { foo: 'bar' })
55+console.log(obj) /* => {
56+ foo: 'bar',
57+ signature: ...
58+} */
59+ssbkeys.verifyObj(k, obj) // => true
60+
61+var secret = new Buffer('deadbeef', 'hex')
62+var obj = ssbkeys.signObjHmac(secret, { foo: 'bar' })
63+console.log(obj) /* => {
64+ foo: 'bar',
65+ hmac: ...
66+} */
67+ssbkeys.verifyObjHmac(secret, obj) // => true
5268 ```
index.jsView
@@ -6,9 +6,18 @@
66 var mkdirp = require('mkdirp')
77 var path = require('path')
88 var curve = ecc.curves.k256
99 var createHmac = require('hmac')
10+var deepEqual = require('deep-equal')
1011
12+function clone (obj) {
13+ var _obj = {}
14+ for(var k in obj) {
15+ if(Object.hasOwnProperty.call(obj, k))
16+ _obj[k] = obj[k]
17+ }
18+ return _obj
19+}
1120
1221 function hash (data, enc) {
1322 return new Blake2s().update(data, enc).digest('base64') + '.blake2s'
1423 }
@@ -170,4 +179,36 @@
170179 return createHmac(createHash, 64, key)
171180 .update(data).digest('base64')+'.blake2s.hmac'
172181 }
173182
183+exports.signObj = function (keys, obj) {
184+ var _obj = clone(obj)
185+ var str = JSON.stringify(_obj, null, 2)
186+ var h = hash(str, 'utf8')
187+ _obj.signature = sign(keys, h)
188+ return _obj
189+}
190+
191+exports.verifyObj = function (keys, obj) {
192+ obj = clone(obj)
193+ var sig = obj.signature
194+ delete obj.signature
195+ var str = JSON.stringify(obj, null, 2)
196+ var h = hash(str, 'utf8')
197+ return exports.verify(keys, sig, h)
198+}
199+
200+exports.signObjHmac = function (secret, obj) {
201+ obj = clone(obj)
202+ var str = JSON.stringify(obj, null, 2)
203+ obj.hmac = exports.hmac(str, secret)
204+ return obj
205+}
206+
207+exports.verifyObjHmac = function (secret, obj) {
208+ obj = clone(obj)
209+ var hmac = obj.hmac
210+ delete obj.hmac
211+ var str = JSON.stringify(obj, null, 2)
212+ var _hmac = exports.hmac(str, secret)
213+ return deepEqual(hmac, _hmac)
214+}

Built with git-ssb-web