Files: c2f2a1a22d5293329dc4c2f03bfdaa7f53bce8d5 / test / index.js
4225 bytesRaw
1 | var tape = require('tape') |
2 | var ssbkeys = require('../') |
3 | var crypto = require('crypto') |
4 | var path = require('path').join(__dirname, 'keyfile') |
5 | var fs = require('fs') |
6 | |
7 | tape('create and load async', function (t) { |
8 | try { fs.unlinkSync(path) } catch(e) {} |
9 | ssbkeys.create(path, function(err, k1) { |
10 | if (err) throw err |
11 | ssbkeys.load(path, function(err, k2) { |
12 | if (err) throw err |
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 | |
21 | tape('create and load sync', function (t) { |
22 | try { fs.unlinkSync(path) } catch(e) {} |
23 | var k1 = ssbkeys.createSync(path) |
24 | var k2 = ssbkeys.loadSync(path) |
25 | t.equal(k1.id.toString('hex'), k2.id.toString('hex')) |
26 | t.equal(k1.private.toString('hex'), k2.private.toString('hex')) |
27 | t.equal(k1.public.toString('hex'), k2.public.toString('hex')) |
28 | t.end() |
29 | }) |
30 | |
31 | |
32 | tape('sign and verify', function (t) { |
33 | |
34 | var keys = ssbkeys.generate() |
35 | var msg = ssbkeys.hash("HELLO THERE?") |
36 | var sig = ssbkeys.sign(keys, msg) |
37 | console.log('public', keys.public) |
38 | console.log('sig', sig) |
39 | t.ok(sig) |
40 | t.equal(ssbkeys.getTag(sig), 'sig.ed25519') |
41 | t.ok(ssbkeys.verify(keys, sig, msg)) |
42 | |
43 | t.end() |
44 | |
45 | }) |
46 | |
47 | tape('sign and verify, call with keys directly', function (t) { |
48 | |
49 | var keys = ssbkeys.generate() |
50 | var msg = ssbkeys.hash("HELLO THERE?") |
51 | var sig = ssbkeys.sign(keys.private, msg) |
52 | console.log('public', keys.public) |
53 | console.log('sig', sig) |
54 | t.ok(sig) |
55 | t.equal(ssbkeys.getTag(sig), 'sig.ed25519') |
56 | t.ok(ssbkeys.verify(keys.public, sig, msg)) |
57 | |
58 | t.end() |
59 | |
60 | }) |
61 | |
62 | tape('sign and verify a javascript object', function (t) { |
63 | |
64 | var obj = require('../package.json') |
65 | |
66 | console.log(obj) |
67 | |
68 | var keys = ssbkeys.generate() |
69 | var sig = ssbkeys.signObj(keys.private, obj) |
70 | console.log(sig) |
71 | t.ok(sig) |
72 | t.ok(ssbkeys.verifyObj(keys, sig, obj)) |
73 | t.end() |
74 | |
75 | }) |
76 | |
77 | tape('test legacy curve: k256', function (t) { |
78 | var keys = ssbkeys.generate('k256') |
79 | |
80 | var msg = ssbkeys.hash("LEGACY SYSTEMS") |
81 | var sig = ssbkeys.sign(keys, msg) |
82 | |
83 | console.log('public', keys.public) |
84 | console.log('sig', sig) |
85 | |
86 | t.ok(sig) |
87 | t.equal(ssbkeys.getTag(sig), 'sig.k256') |
88 | t.ok(ssbkeys.verify(keys, sig, msg)) |
89 | |
90 | t.end() |
91 | }) |
92 | |
93 | tape('create and load async, legacy', function (t) { |
94 | try { fs.unlinkSync(path) } catch(e) {} |
95 | ssbkeys.create(path, 'k256', function(err, k1) { |
96 | if (err) throw err |
97 | ssbkeys.load(path, function(err, k2) { |
98 | if (err) throw err |
99 | |
100 | t.equal(k2.curve, 'k256') |
101 | t.equal(k1.id, k2.id) |
102 | t.equal(k1.private, k2.private) |
103 | t.equal(k1.public, k2.public) |
104 | |
105 | t.end() |
106 | }) |
107 | }) |
108 | }) |
109 | |
110 | tape('create and load sync, legacy', function (t) { |
111 | try { fs.unlinkSync(path) } catch(e) {} |
112 | var k1 = ssbkeys.createSync(path, 'k256', true) |
113 | var k2 = ssbkeys.loadSync(path) |
114 | |
115 | console.log(k2) |
116 | |
117 | t.equal(k2.curve, 'k256') |
118 | t.equal(k1.id, k2.id) |
119 | t.equal(k1.private, k2.private) |
120 | t.equal(k1.public, k2.public) |
121 | |
122 | t.end() |
123 | }) |
124 | |
125 | tape('seeded keys, ed25519', function (t) { |
126 | |
127 | var seed = crypto.randomBytes(32) |
128 | var k1 = ssbkeys.generate('ed25519', seed) |
129 | var k2 = ssbkeys.generate('ed25519', seed) |
130 | |
131 | t.deepEqual(k1, k2) |
132 | |
133 | t.end() |
134 | |
135 | }) |
136 | |
137 | tape('seeded keys, k256', function (t) { |
138 | |
139 | var seed = crypto.randomBytes(32) |
140 | var k1 = ssbkeys.generate('k256', seed) |
141 | var k2 = ssbkeys.generate('k256', seed) |
142 | |
143 | t.deepEqual(k1, k2) |
144 | |
145 | t.end() |
146 | |
147 | }) |
148 | |
149 | tape('ed25519 id === "@" ++ pubkey', function (t) { |
150 | |
151 | var keys = ssbkeys.generate('ed25519') |
152 | t.equal(keys.id, '@' + keys.public) |
153 | |
154 | t.end() |
155 | |
156 | }) |
157 | |
158 | tape('create and load presigil-legacy async', function (t) { |
159 | try { fs.unlinkSync(path) } catch(e) {} |
160 | var keys = ssbkeys.generate('ed25519') |
161 | keys.id = keys.id.substring(1) |
162 | fs.writeFileSync(path, JSON.stringify(keys)) |
163 | |
164 | var k2 = ssbkeys.loadSync(path) |
165 | t.equal(k2.id, '@' + keys.id) |
166 | t.end() |
167 | |
168 | }) |
169 | |
170 | tape('create and load presigil-legacy', function (t) { |
171 | |
172 | try { fs.unlinkSync(path) } catch(e) {} |
173 | var keys = ssbkeys.generate('ed25519') |
174 | keys.id = keys.id.substring(1) |
175 | fs.writeFileSync(path, JSON.stringify(keys)) |
176 | |
177 | ssbkeys.load(path, function (err, k2) { |
178 | if(err) throw err |
179 | t.equal(k2.id, '@' + keys.id) |
180 | t.end() |
181 | }) |
182 | |
183 | }) |
184 |
Built with git-ssb-web