Files: f57b58569dafb4a037a0648c9e7731063a14b691 / test / index.js
3233 bytesRaw
1 | var chloride = require('chloride') |
2 | var tape = require('tape') |
3 | var group_box = require('group-box') |
4 | var u = require('../util') |
5 | var Scuttlebot = require('ssb-server') |
6 | .use(require('../')) |
7 | |
8 | var alice = Scuttlebot({ |
9 | temp: true |
10 | }) |
11 | |
12 | var bob = alice.createFeed() |
13 | |
14 | function generate (seed) { |
15 | var keys = chloride.crypto_box_seed_keypair(seed) |
16 | return { |
17 | public: keys.publicKey.toString('base64')+'.curve25519', |
18 | private: keys.secretKey.toString('base64') |
19 | } |
20 | } |
21 | |
22 | function toBuffer(s) { |
23 | return Buffer.isBuffer(s) ? s : Buffer.from(s, 'base64') |
24 | } |
25 | |
26 | function scalarmult (pk,sk) { |
27 | return chloride.crypto_scalarmult(toBuffer(pk), toBuffer(sk)) |
28 | } |
29 | function hash (s) { |
30 | return chloride.crypto_hash_sha256(Buffer.from(s, 'utf8')) |
31 | } |
32 | var alice_keys = generate(hash('alice_secret')) |
33 | var bob_keys = generate(hash('bob_secret')) |
34 | |
35 | tape('create a private-msg-key', function (t) { |
36 | alice.privateGroups.addCurvePair(alice_keys, function (err) { |
37 | if(err) throw err |
38 | alice.publish({ |
39 | type: 'private-msg-key', |
40 | key: alice_keys.public |
41 | }, function (err, msg) { |
42 | if(err) throw err |
43 | |
44 | //bob doesn't call addCurvePair because bob is remote. |
45 | //(we are just adding his feed directly so we don't |
46 | // need to bother with replication) |
47 | bob.publish({ |
48 | type: 'private-msg-key', |
49 | key: bob_keys.public |
50 | }, function (err, data) { |
51 | if(err) throw err |
52 | console.log(data) |
53 | t.ok(data.key) |
54 | |
55 | var content = { type: 'private', text: 'hello, alice' } |
56 | var ptxt = Buffer.from(JSON.stringify(content)) |
57 | var nonce = u.id2Buffer(data.key) |
58 | var keys = [bob_keys, alice_keys].map(function (key) { |
59 | return scalarmult(bob_keys.private, key.public) |
60 | }) |
61 | var keys2 = [bob_keys].map(function (key) { |
62 | return scalarmult(alice_keys.private, key.public) |
63 | }) |
64 | console.log('plaintext.length', ptxt.length) |
65 | |
66 | var ctxt = group_box.box( |
67 | ptxt, |
68 | nonce, |
69 | keys |
70 | ) |
71 | // console.log("CTXT", ctxt.toString('base64')) |
72 | // console.log("NONCE", nonce) |
73 | // console.log("KEYS", keys) |
74 | // console.log("KEYS2", keys2) |
75 | var _key = group_box.unboxKey(ctxt, nonce, keys, 8) |
76 | console.log("INPUT", { |
77 | ctxt: ctxt.toString('hex'), |
78 | nonce: nonce, |
79 | key: _key |
80 | }) |
81 | console.log( |
82 | 'INPUT_TEST', |
83 | chloride.crypto_hash_sha256(Buffer.concat([ctxt, nonce, _key])), |
84 | group_box.unboxBody(ctxt, nonce, _key).toString() |
85 | ) |
86 | console.log('d.ptxt', group_box.unboxBody(ctxt, nonce, _key).toString()) |
87 | |
88 | // console.log('ctxt.length', ctxt.length) |
89 | bob.publish( |
90 | ctxt.toString('base64')+'.box2', |
91 | function (err, data) { |
92 | if(err) throw err |
93 | t.ok(data) |
94 | // console.log(data) |
95 | // alice.privateGroups.get(function () { |
96 | alice.get({id: data.key, private: true}, function (err, msg) { |
97 | if(err) throw err |
98 | t.deepEqual(msg.content, content) |
99 | t.end() |
100 | |
101 | }) |
102 | // }) |
103 | } |
104 | ) |
105 | }) |
106 | }) |
107 | }) |
108 | }) |
109 | |
110 | tape('cleanup', function (t) { |
111 | alice.close() |
112 | t.end() |
113 | }) |
114 | |
115 | |
116 | |
117 | |
118 |
Built with git-ssb-web