Commit 8282362bcda4a90c9ba177f91a37bb5eb1390a11
tagged hashes & keys
Dominic Tarr committed on 11/20/2014, 6:50:39 PMParent: 0644061d62c451cf63f1489c1b7e8c5bab16b76b
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -8,14 +8,14 @@ | ||
8 | 8 | |
9 | 9 | function bsum (value) { |
10 | 10 | return new Blake2s().update(value).digest() |
11 | 11 | } |
12 | + | |
12 | 13 | function empty(v) { return !!v } |
13 | 14 | |
14 | 15 | function constructKeys() { |
15 | 16 | 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)) | |
18 | 18 | k.keyfile = [ |
19 | 19 | '# this is your SECRET name.', |
20 | 20 | '# this name gives you magical powers.', |
21 | 21 | '# with it you can mark your messages so that your friends can verify', |
@@ -32,16 +32,49 @@ | ||
32 | 32 | ].join('\n') |
33 | 33 | return k |
34 | 34 | } |
35 | 35 | |
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 | + | |
36 | 51 | 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)) | |
42 | 63 | } |
43 | 64 | |
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 | + | |
44 | 77 | exports.load = function(namefile, cb) { |
45 | 78 | fs.readFile(namefile, 'ascii', function(err, privateKeyStr) { |
46 | 79 | if (err) return cb(err) |
47 | 80 | try { cb(null, reconstructKeys(privateKeyStr)) } |
Built with git-ssb-web