git ssb

1+

Dominic / ssb-keys



Commit 8282362bcda4a90c9ba177f91a37bb5eb1390a11

tagged hashes & keys

Dominic Tarr committed on 11/20/2014, 6:50:39 PM
Parent: 0644061d62c451cf63f1489c1b7e8c5bab16b76b

Files changed

index.jschanged
index.jsView
@@ -8,14 +8,14 @@
88
99 function bsum (value) {
1010 return new Blake2s().update(value).digest()
1111 }
12+
1213 function empty(v) { return !!v }
1314
1415 function constructKeys() {
1516 var privateKey = crypto.randomBytes(32)
16- var k = ecc.restore(k256, privateKey)
17- k.id = bsum(k.public)
17+ var k = keysToBase64(ecc.restore(k256, privateKey))
1818 k.keyfile = [
1919 '# this is your SECRET name.',
2020 '# this name gives you magical powers.',
2121 '# with it you can mark your messages so that your friends can verify',
@@ -32,16 +32,49 @@
3232 ].join('\n')
3333 return k
3434 }
3535
36+
37+function toBuffer(buf) {
38+ if(buf == null) return buf
39+ return new Buffer(buf.substring(0, buf.indexOf('.')), 'base64')
40+}
41+
42+function keysToBase64 (keys) {
43+ var pub = tag(keys.public, 'k256')
44+ return {
45+ public: pub,
46+ private: tag(keys.private, 'k256'),
47+ id: hash(pub)
48+ }
49+}
50+
3651 function reconstructKeys(privateKeyStr) {
37- privateKeyStr = privateKeyStr.replace(/\s*\#[^\n]*/g, '').split('\n').filter(empty).join('')
38- var privateKey = new Buffer(privateKeyStr, 'hex')
39- var k = ecc.restore(k256, privateKey)
40- k.id = bsum(k.public)
41- return k
52+ privateKeyStr = privateKeyStr
53+ .replace(/\s*\#[^\n]*/g, '')
54+ .split('\n').filter(empty).join('')
55+
56+ var privateKey = (
57+ !/\./.test(privateKeyStr)
58+ ? new Buffer(privateKeyStr, 'hex')
59+ : toBuffer(privateKeyStr)
60+ )
61+
62+ return keysToBase64(ecc.restore(k256, privateKey))
4263 }
4364
65+function tag (key, tag) {
66+ return key.toString('base64')+'.' + tag
67+}
68+
69+var hash = exports.hash = function (data, enc) {
70+ return new Blake2s().update(data, enc).digest('base64') + '.blake2s'
71+}
72+
73+exports.generate = function () {
74+ return keysToBase64(ecc.restore(curve, crypto.randomBytes(32)))
75+}
76+
4477 exports.load = function(namefile, cb) {
4578 fs.readFile(namefile, 'ascii', function(err, privateKeyStr) {
4679 if (err) return cb(err)
4780 try { cb(null, reconstructKeys(privateKeyStr)) }

Built with git-ssb-web