git ssb

1+

Dominic / ssb-keys



Commit 78c7892812d6c2c28f3d22ca7a879e05f4be20d9

avoid an unnecessary hash

Dominic Tarr committed on 7/5/2015, 1:27:43 AM
Parent: d8e28e38a387d460308701a089afb9674986874c

Files changed

index.jschanged
index.jsView
@@ -11,8 +11,13 @@
1111 var isRef = require('ssb-ref')
1212
1313 var pb = require('private-box')
1414
15+var isBuffer = Buffer.isBuffer
16+
17+function isString (s) {
18+ return 'string' === typeof s
19+}
1520 //UTILS
1621
1722 function clone (obj) {
1823 var _obj = {}
@@ -230,29 +235,30 @@
230235
231236 //takes a public key and a hash and returns a signature.
232237 //(a signature must be a node buffer)
233238
234-exports.sign = function (keys, hash) {
235- if(isObject(hash))
236- throw new Error('hash should be base64 string, did you mean signObj(private, unsigned_obj)')
237- var hashTag = hash.substring(hash.indexOf('.'))
239+exports.sign = function (keys, msg) {
240+ if(isString(msg))
241+ msg = new Buffer(msg)
242+ if(!isBuffer(msg))
243+ throw new Error('msg should be buffer')
238244 var curve = getCurve(keys)
239245
240246 return curves[curve]
241- .sign(toBuffer(keys.private || keys), toBuffer(hash))
242- .toString('base64')+'.sha256.'+curve
247+ .sign(toBuffer(keys.private || keys), msg)
248+ .toString('base64')+'.sig.'+curve
243249
244250 }
245251
246252 //takes a public key, signature, and a hash
247253 //and returns true if the signature was valid.
248-exports.verify = function (keys, sig, hash) {
254+exports.verify = function (keys, sig, msg) {
249255 if(isObject(sig))
250256 throw new Error('signature should be base64 string, did you mean verifyObj(public, signed_obj)')
251257 return curves[getCurve(keys)].verify(
252258 toBuffer(keys.public || keys),
253259 toBuffer(sig),
254- toBuffer(hash)
260+ isBuffer(msg) ? msg : new Buffer(msg)
255261 )
256262 }
257263
258264 // OTHER CRYTPO FUNCTIONS
@@ -263,49 +269,21 @@
263269 }
264270
265271 exports.signObj = function (keys, obj) {
266272 var _obj = clone(obj)
267- var str = JSON.stringify(_obj, null, 2)
268- var h = hash(str, 'utf8')
269- _obj.signature = exports.sign(keys, h)
273+ var b = new Buffer(JSON.stringify(_obj, null, 2))
274+ _obj.signature = exports.sign(keys, b)
270275 return _obj
271276 }
272277
273278 exports.verifyObj = function (keys, obj) {
274279 obj = clone(obj)
275280 var sig = obj.signature
276281 delete obj.signature
277- var str = JSON.stringify(obj, null, 2)
278- var h = hash(str, 'utf8')
279- return exports.verify(keys, sig, h)
282+ var b = new Buffer(JSON.stringify(obj, null, 2))
283+ return exports.verify(keys, sig, b)
280284 }
281285
282-//TODO: remove these (use asymmetric auth for everything)
283-
284-//exports.signObjHmac = function (secret, obj) {
285-// obj = clone(obj)
286-// var str = JSON.stringify(obj, null, 2)
287-// obj.hmac = exports.hmac(str, secret)
288-// return obj
289-//}
290-//
291-//exports.verifyObjHmac = function (secret, obj) {
292-// obj = clone(obj)
293-// var hmac = obj.hmac
294-// delete obj.hmac
295-// var str = JSON.stringify(obj, null, 2)
296-// var _hmac = exports.hmac(str, secret)
297-// return deepEqual(hmac, _hmac)
298-//}
299-//
300-//exports.createAuth = function (keys, role) {
301-// return exports.signObj(keys, {
302-// role: role || 'client',
303-// ts: Date.now(),
304-// public: keys.public
305-// })
306-//}
307-
308286 exports.box = function (msg, recipients) {
309287 msg = new Buffer(JSON.stringify(msg))
310288
311289 recipients = recipients.map(function (keys) {

Built with git-ssb-web