git ssb

1+

Dominic / ssb-keys



Tree: dbb050ac8c0d5b0772f2838f1d149b6420b2e583

Files: dbb050ac8c0d5b0772f2838f1d149b6420b2e583 / test / index.js

2553 bytesRaw
1var tape = require('tape')
2var ssbkeys = require('../')
3var crypto = require('crypto')
4var path = '/tmp/ssb-keys_'+Date.now()
5
6tape('create and load async', function (t) {
7 console.log(ssbkeys)
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 console.log(k1, k2)
13 t.equal(k1.id.toString('hex'), k2.id.toString('hex'))
14 t.equal(k1.private.toString('hex'), k2.private.toString('hex'))
15 t.equal(k1.public.toString('hex'), k2.public.toString('hex'))
16 t.end()
17 })
18 })
19})
20
21tape('create and load sync', function (t) {
22 var k1 = ssbkeys.createSync(path+'1')
23 var k2 = ssbkeys.loadSync(path+'1')
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})
29
30tape('sign and verify a javascript object', function (t) {
31
32 var obj = require('../package.json')
33 var hmac_key = crypto.randomBytes(32)
34 var hmac_key2 = crypto.randomBytes(32)
35 console.log(obj)
36
37 var keys = ssbkeys.generate()
38 var sig = ssbkeys.signObj(keys.private, hmac_key, obj)
39 console.log(sig)
40 t.ok(sig)
41 //verify must be passed the key to correctly verify
42 t.notOk(ssbkeys.verifyObj(keys, sig))
43 t.notOk(ssbkeys.verifyObj({public: keys.public}, sig))
44 t.ok(ssbkeys.verifyObj(keys, hmac_key, sig))
45 t.ok(ssbkeys.verifyObj({public: keys.public}, hmac_key, sig))
46 //a different hmac_key fails to verify
47 t.notOk(ssbkeys.verifyObj(keys, hmac_key2, sig))
48 t.notOk(ssbkeys.verifyObj({public: keys.public}, hmac_key2, sig))
49 t.end()
50
51})
52
53//allow sign and verify to also take a separate key
54//so that we can create signatures that cannot be used in other places.
55//(i.e. testnet) avoiding chosen protocol attacks.
56tape('sign and verify a hmaced object javascript object', function (t) {
57
58 var obj = require('../package.json')
59
60 console.log(obj)
61
62 var keys = ssbkeys.generate()
63 var sig = ssbkeys.signObj(keys.private, obj)
64 console.log(sig)
65 t.ok(sig)
66 t.ok(ssbkeys.verifyObj(keys, sig))
67 t.ok(ssbkeys.verifyObj({public: keys.public}, sig))
68 t.end()
69
70})
71
72tape('seeded keys, ed25519', function (t) {
73
74 var seed = crypto.randomBytes(32)
75 var k1 = ssbkeys.generate('ed25519', seed)
76 var k2 = ssbkeys.generate('ed25519', seed)
77
78 t.deepEqual(k1, k2)
79
80 t.end()
81
82})
83
84tape('ed25519 id === "@" ++ pubkey', function (t) {
85
86 var keys = ssbkeys.generate('ed25519')
87 t.equal(keys.id, '@' + keys.public)
88
89 t.end()
90
91})
92
93
94
95
96
97
98
99
100
101

Built with git-ssb-web