Commit 0ebb74e3c98f4c1f67a18eb369cbd4f2649fd6a6
added tests and sync versions
Paul Frazee committed on 10/29/2014, 12:09:57 AMParent: 5bd750b9be732f28dbe2090adf2035ec2e3f85ff
Files changed
index.js | changed |
package.json | changed |
test/index.js | added |
test/keyfile | added |
index.js | ||
---|---|---|
@@ -6,28 +6,15 @@ | ||
6 | 6 | |
7 | 7 | function bsum (value) { |
8 | 8 | return new Blake2s().update(value).digest() |
9 | 9 | } |
10 | +function empty(v) { return !!v } | |
10 | 11 | |
11 | -exports.load = function(namefile) { | |
12 | - try { | |
13 | - function empty(v) { return !!v } | |
14 | - var privateKeyStr = fs.readFileSync(namefile, 'ascii').replace(/\s*\#[^\n]*/g, '').split('\n').filter(empty).join('') | |
15 | - var privateKey = new Buffer(privateKeyStr, 'hex') | |
16 | - var k = ecc.restore(k256, privateKey) | |
17 | - k.id = bsum(k.public) | |
18 | - return k | |
19 | - } catch (e) { | |
20 | - return null | |
21 | - } | |
22 | -} | |
23 | - | |
24 | -exports.createKeys = function(namefile, cb) { | |
12 | +function constructKeys() { | |
25 | 13 | var privateKey = crypto.randomBytes(32) |
26 | 14 | var k = ecc.restore(k256, privateKey) |
27 | - var id = bsum(k.public) | |
28 | - | |
29 | - var contents = [ | |
15 | + k.id = bsum(k.public) | |
16 | + k.keyfile = [ | |
30 | 17 | '# this is your SECRET name.', |
31 | 18 | '# this name gives you magical powers.', |
32 | 19 | '# with it you can mark your messages so that your friends can verify', |
33 | 20 | '# that they really did come from you.', |
@@ -38,13 +25,44 @@ | ||
38 | 25 | k.private.toString('hex'), |
39 | 26 | '', |
40 | 27 | '# WARNING! It\'s vital that you DO NOT edit OR share your secret name', |
41 | 28 | '# instead, share your public name', |
42 | - '# your public name: ' + id.toString('hex') | |
29 | + '# your public name: ' + k.id.toString('hex') | |
43 | 30 | ].join('\n') |
31 | + return k | |
32 | +} | |
44 | 33 | |
45 | - fs.writeFile(namefile, contents, function(err) { | |
34 | +function reconstructKeys(privateKeyStr) { | |
35 | + privateKeyStr = privateKeyStr.replace(/\s*\#[^\n]*/g, '').split('\n').filter(empty).join('') | |
36 | + var privateKey = new Buffer(privateKeyStr, 'hex') | |
37 | + var k = ecc.restore(k256, privateKey) | |
38 | + k.id = bsum(k.public) | |
39 | + return k | |
40 | +} | |
41 | + | |
42 | +exports.load = function(namefile, cb) { | |
43 | + fs.readFile(namefile, 'ascii', function(err, privateKeyStr) { | |
46 | 44 | if (err) return cb(err) |
47 | - k.id = id | |
45 | + try { cb(null, reconstructKeys(privateKeyStr)) } | |
46 | + catch (e) { cb(err) } | |
47 | + }) | |
48 | +} | |
49 | + | |
50 | +exports.loadSync = function(namefile) { | |
51 | + return reconstructKeys(fs.readFileSync(namefile, 'ascii')) | |
52 | +} | |
53 | + | |
54 | +exports.create = function(namefile, cb) { | |
55 | + var k = constructKeys() | |
56 | + fs.writeFile(namefile, k.keyfile, function(err) { | |
57 | + if (err) return cb(err) | |
58 | + delete k.keyfile | |
48 | 59 | cb(null, k) |
49 | 60 | }) |
61 | +} | |
62 | + | |
63 | +exports.createSync = function(namefile) { | |
64 | + var k = constructKeys() | |
65 | + fs.writeFileSync(namefile, k.keyfile) | |
66 | + delete k.keyfile | |
67 | + return k | |
50 | 68 | } |
package.json | ||
---|---|---|
@@ -11,8 +11,12 @@ | ||
11 | 11 | "eccjs": "git://github.com/dominictarr/eccjs.git#586f6d47507184a2efe84684ed0a30605cbc43a5", |
12 | 12 | "blake2s": "~1.0.0" |
13 | 13 | }, |
14 | 14 | "devDependencies": { |
15 | + "tape": "~3.0.0" | |
15 | 16 | }, |
17 | + "scripts": { | |
18 | + "test": "set -e; for t in test/*.js; do node $t; done" | |
19 | + }, | |
16 | 20 | "author": "Paul Frazee <pfrazee@gmail.com>", |
17 | 21 | "license": "MIT" |
18 | 22 | } |
test/index.js | ||
---|---|---|
@@ -1,0 +1,28 @@ | ||
1 | +var tape = require('tape') | |
2 | +var ssbkeys = require('../') | |
3 | + | |
4 | +var path = require('path').join(__dirname, 'keyfile') | |
5 | + | |
6 | +tape('create and load async', function (t) { | |
7 | + try { require('fs').unlinkSync(path) } catch(e) {} | |
8 | + ssbkeys.create(path, function(err, k1) { | |
9 | + if (err) throw err | |
10 | + ssbkeys.load(path, function(err, k2) { | |
11 | + if (err) throw err | |
12 | + t.equal(k1.id.toString('hex'), k2.id.toString('hex')) | |
13 | + t.equal(k1.private.toString('hex'), k2.private.toString('hex')) | |
14 | + t.equal(k1.public.toString('hex'), k2.public.toString('hex')) | |
15 | + t.end() | |
16 | + }) | |
17 | + }) | |
18 | +}) | |
19 | + | |
20 | +tape('create and load sync', function (t) { | |
21 | + try { require('fs').unlinkSync(path) } catch(e) {} | |
22 | + var k1 = ssbkeys.createSync(path) | |
23 | + var k2 = ssbkeys.loadSync(path) | |
24 | + t.equal(k1.id.toString('hex'), k2.id.toString('hex')) | |
25 | + t.equal(k1.private.toString('hex'), k2.private.toString('hex')) | |
26 | + t.equal(k1.public.toString('hex'), k2.public.toString('hex')) | |
27 | + t.end() | |
28 | +}) |
test/keyfile | ||
---|---|---|
@@ -1,0 +1,13 @@ | ||
1 | +# this is your SECRET name. | |
2 | +# this name gives you magical powers. | |
3 | +# with it you can mark your messages so that your friends can verify | |
4 | +# that they really did come from you. | |
5 | +# | |
6 | +# if any one learns this name, they can use it to destroy your identity | |
7 | +# NEVER show this to anyone!!! | |
8 | + | |
9 | +e6b3a7188067278f559a2eb771472f5a760a3eaabc75b4c8e28d7efe77693c19 | |
10 | + | |
11 | +# WARNING! It's vital that you DO NOT edit OR share your secret name | |
12 | +# instead, share your public name | |
13 | +# your public name: d180310d075a1ab04af5bd24b1707e81f61e6941bc81c97ca372bc766e8b3a2d |
Built with git-ssb-web