git ssb

1+

Dominic / ssb-keys



Commit 0ebb74e3c98f4c1f67a18eb369cbd4f2649fd6a6

added tests and sync versions

Paul Frazee committed on 10/29/2014, 12:09:57 AM
Parent: 5bd750b9be732f28dbe2090adf2035ec2e3f85ff

Files changed

index.jschanged
package.jsonchanged
test/index.jsadded
test/keyfileadded
index.jsView
@@ -6,28 +6,15 @@
66
77 function bsum (value) {
88 return new Blake2s().update(value).digest()
99 }
10+function empty(v) { return !!v }
1011
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() {
2513 var privateKey = crypto.randomBytes(32)
2614 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 = [
3017 '# this is your SECRET name.',
3118 '# this name gives you magical powers.',
3219 '# with it you can mark your messages so that your friends can verify',
3320 '# that they really did come from you.',
@@ -38,13 +25,44 @@
3825 k.private.toString('hex'),
3926 '',
4027 '# WARNING! It\'s vital that you DO NOT edit OR share your secret name',
4128 '# instead, share your public name',
42- '# your public name: ' + id.toString('hex')
29+ '# your public name: ' + k.id.toString('hex')
4330 ].join('\n')
31+ return k
32+}
4433
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) {
4644 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
4859 cb(null, k)
4960 })
61+}
62+
63+exports.createSync = function(namefile) {
64+ var k = constructKeys()
65+ fs.writeFileSync(namefile, k.keyfile)
66+ delete k.keyfile
67+ return k
5068 }
package.jsonView
@@ -11,8 +11,12 @@
1111 "eccjs": "git://github.com/dominictarr/eccjs.git#586f6d47507184a2efe84684ed0a30605cbc43a5",
1212 "blake2s": "~1.0.0"
1313 },
1414 "devDependencies": {
15+ "tape": "~3.0.0"
1516 },
17+ "scripts": {
18+ "test": "set -e; for t in test/*.js; do node $t; done"
19+ },
1620 "author": "Paul Frazee <pfrazee@gmail.com>",
1721 "license": "MIT"
1822 }
test/index.jsView
@@ -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/keyfileView
@@ -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