Commit 78c7892812d6c2c28f3d22ca7a879e05f4be20d9
avoid an unnecessary hash
Dominic Tarr committed on 7/5/2015, 1:27:43 AMParent: d8e28e38a387d460308701a089afb9674986874c
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -11,8 +11,13 @@ | ||
11 | 11 | var isRef = require('ssb-ref') |
12 | 12 | |
13 | 13 | var pb = require('private-box') |
14 | 14 | |
15 | +var isBuffer = Buffer.isBuffer | |
16 | + | |
17 | +function isString (s) { | |
18 | + return 'string' === typeof s | |
19 | +} | |
15 | 20 | //UTILS |
16 | 21 | |
17 | 22 | function clone (obj) { |
18 | 23 | var _obj = {} |
@@ -230,29 +235,30 @@ | ||
230 | 235 | |
231 | 236 | //takes a public key and a hash and returns a signature. |
232 | 237 | //(a signature must be a node buffer) |
233 | 238 | |
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') | |
238 | 244 | var curve = getCurve(keys) |
239 | 245 | |
240 | 246 | 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 | |
243 | 249 | |
244 | 250 | } |
245 | 251 | |
246 | 252 | //takes a public key, signature, and a hash |
247 | 253 | //and returns true if the signature was valid. |
248 | -exports.verify = function (keys, sig, hash) { | |
254 | +exports.verify = function (keys, sig, msg) { | |
249 | 255 | if(isObject(sig)) |
250 | 256 | throw new Error('signature should be base64 string, did you mean verifyObj(public, signed_obj)') |
251 | 257 | return curves[getCurve(keys)].verify( |
252 | 258 | toBuffer(keys.public || keys), |
253 | 259 | toBuffer(sig), |
254 | - toBuffer(hash) | |
260 | + isBuffer(msg) ? msg : new Buffer(msg) | |
255 | 261 | ) |
256 | 262 | } |
257 | 263 | |
258 | 264 | // OTHER CRYTPO FUNCTIONS |
@@ -263,49 +269,21 @@ | ||
263 | 269 | } |
264 | 270 | |
265 | 271 | exports.signObj = function (keys, obj) { |
266 | 272 | 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) | |
270 | 275 | return _obj |
271 | 276 | } |
272 | 277 | |
273 | 278 | exports.verifyObj = function (keys, obj) { |
274 | 279 | obj = clone(obj) |
275 | 280 | var sig = obj.signature |
276 | 281 | 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) | |
280 | 284 | } |
281 | 285 | |
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 | - | |
308 | 286 | exports.box = function (msg, recipients) { |
309 | 287 | msg = new Buffer(JSON.stringify(msg)) |
310 | 288 | |
311 | 289 | recipients = recipients.map(function (keys) { |
Built with git-ssb-web