git ssb

1+

Dominic / ssb-keys



Commit 8db3f5554f2454d89f10568cee609dc9a6304907

strip down public api: hide sign, verify, remove eccjs curves, add hmac_key option to signObj, verifyObj.

Dominic Tarr committed on 10/3/2016, 8:48:21 PM
Parent: 21deb97a3ee7ca191aa07a8ae90da508fd57388b

Files changed

index.jschanged
index.jsView
@@ -25,8 +25,10 @@
2525
2626 var isLink = ssbref.isLink
2727 var isFeedId = ssbref.isFeedId
2828
29 +var hmac = sodium.crypto_auth
30 +
2931 exports.hash = u.hash
3032
3133 exports.getTag = u.getTag
3234
@@ -43,10 +45,8 @@
4345 }
4446
4547 var curves = {}
4648 curves.ed25519 = require('./sodium')
47-try { curves.k256 = require('./eccjs') }
48-catch (_) {}
4949
5050 function getCurve(keys) {
5151 var curve = keys.curve
5252
@@ -101,9 +101,9 @@
101101
102102 //takes a public key and a hash and returns a signature.
103103 //(a signature must be a node buffer)
104104
105-exports.sign = function (keys, msg) {
105 +function sign (keys, msg) {
106106 if(isString(msg))
107107 msg = new Buffer(msg)
108108 if(!isBuffer(msg))
109109 throw new Error('msg should be buffer')
@@ -116,9 +116,9 @@
116116 }
117117
118118 //takes a public key, signature, and a hash
119119 //and returns true if the signature was valid.
120-exports.verify = function (keys, sig, msg) {
120 +function verify (keys, sig, msg) {
121121 if(isObject(sig))
122122 throw new Error('signature should be base64 string, did you mean verifyObj(public, signed_obj)')
123123 return curves[getCurve(keys)].verify(
124124 u.toBuffer(keys.public || keys),
@@ -128,21 +128,25 @@
128128 }
129129
130130 // OTHER CRYTPO FUNCTIONS
131131
132-exports.signObj = function (keys, obj) {
132 +exports.signObj = function (keys, hmac_key, obj) {
133 + if(!obj) obj = hmac_key, hmac_key = null
133134 var _obj = clone(obj)
134135 var b = new Buffer(JSON.stringify(_obj, null, 2))
135- _obj.signature = exports.sign(keys, b)
136 + if(hmac_key) b = hmac(b, hmac_key)
137 + _obj.signature = sign(keys, b)
136138 return _obj
137139 }
138140
139-exports.verifyObj = function (keys, obj) {
141 +exports.verifyObj = function (keys, hmac_key, obj) {
142 + if(!obj) obj = hmac_key, hmac_key = null
140143 obj = clone(obj)
141144 var sig = obj.signature
142145 delete obj.signature
143146 var b = new Buffer(JSON.stringify(obj, null, 2))
144- return exports.verify(keys, sig, b)
147 + if(hmac_key) b = hmac(b, hmac_key)
148 + return verify(keys, sig, b)
145149 }
146150
147151 exports.box = function (msg, recipients) {
148152 msg = new Buffer(JSON.stringify(msg))
@@ -151,11 +155,8 @@
151155 var public = keys.public || keys
152156 return sodium.crypto_sign_ed25519_pk_to_curve25519(u.toBuffer(public))
153157 })
154158
155- //it's since the nonce is 24 bytes (a multiple of 3)
156- //it's possible to concatenate the base64 strings
157- //and still have a valid base64 string.
158159 return pb.multibox(msg, recipients).toString('base64')+'.box'
159160 }
160161
161162 exports.unbox = function (boxed, keys) {

Built with git-ssb-web