|
@@ -3,29 +3,48 @@ const hypercore = require('hypercore')
|
3
|
3
|
const path = require('path')
|
4
|
4
|
const fs = require('fs')
|
5
|
5
|
|
6
|
|
-const Crypto = require('../')
|
7
|
|
-const crypto = Crypto()
|
|
6
|
+const crypto = require('../')
|
8
|
7
|
|
9
|
8
|
const { cleanup, tmp } = require('./util')
|
10
|
9
|
|
11
|
10
|
describe('key generation', (context) => {
|
12
|
|
- context('generate an asymmetric keypair', (assert, next) => {
|
|
11
|
+ context('masterKey()', (assert, next) => {
|
|
12
|
+ const masterKey = crypto.masterKey()
|
|
13
|
+ assert.ok(masterKey, 'key generated successfully')
|
|
14
|
+ next()
|
|
15
|
+ })
|
|
16
|
+
|
|
17
|
+ context('keyPair()', (assert, next) => {
|
13
|
18
|
const masterKey = crypto.masterKey()
|
14
|
19
|
const keypair = crypto.keyPair(masterKey, 0)
|
15
|
|
- assert.ok(keypair, 'Keys successfully generated')
|
16
|
|
- assert.ok(keypair.publicKey instanceof Buffer, 'Public Key is a buffer')
|
17
|
|
- assert.ok(keypair.secretKey instanceof Buffer, 'Secret Key s a buffer')
|
|
20
|
+ assert.ok(keypair, 'keys successfully generated')
|
|
21
|
+ assert.ok(keypair.publicKey instanceof Buffer, 'public key is a buffer')
|
|
22
|
+ assert.ok(keypair.secretKey instanceof Buffer, 'secret key is a buffer')
|
|
23
|
+ next()
|
|
24
|
+ })
|
|
25
|
+
|
|
26
|
+ context('encryptionKey()', (assert, next) => {
|
|
27
|
+ const key = crypto.encryptionKey()
|
|
28
|
+ assert.ok(key, 'Key successfully generated')
|
|
29
|
+ assert.ok(key instanceof Buffer, 'key is a secure buffer')
|
|
30
|
+ next()
|
|
31
|
+ })
|
|
32
|
+
|
|
33
|
+ context('encryptionKey(string)', (assert, next) => {
|
|
34
|
+ const key = crypto.encryptionKey(crypto.randomBytes(32).toString('hex'))
|
|
35
|
+ assert.ok(key, 'Key successfully generated')
|
|
36
|
+ assert.ok(key instanceof Buffer, 'key is a secure buffer')
|
18
|
37
|
next()
|
19
|
38
|
})
|
20
|
39
|
|
21
|
|
- context('generate a symmetric key', (assert, next) => {
|
22
|
|
- const key = crypto.symmetricKey()
|
|
40
|
+ context('encryptionKey(buffer)', (assert, next) => {
|
|
41
|
+ const key = crypto.encryptionKey(crypto.randomBytes(32))
|
23
|
42
|
assert.ok(key, 'Key successfully generated')
|
24
|
|
- assert.ok(key instanceof Buffer, 'Symmetric key is a buffer')
|
|
43
|
+ assert.ok(key instanceof Buffer, 'key is a secure buffer')
|
25
|
44
|
next()
|
26
|
45
|
})
|
27
|
46
|
|
28
|
|
- context('generate an access key', (assert, next) => {
|
|
47
|
+ context('accessKey()', (assert, next) => {
|
29
|
48
|
const accessKey = crypto.accessKey()
|
30
|
49
|
assert.ok(accessKey, 'Key successfully generated')
|
31
|
50
|
assert.same(accessKey.length, 64, 'Read key is 64 bytes')
|
|
@@ -33,70 +52,84 @@ describe('key generation', (context) => {
|
33
|
52
|
next()
|
34
|
53
|
})
|
35
|
54
|
|
36
|
|
- context('pack an access key', (assert, next) => {
|
37
|
|
- const pubKey = crypto.randomBytes(32)
|
38
|
|
- const secretKey = crypto.symmetricKey()
|
|
55
|
+ context('pack(buffer, buffer)', (assert, next) => {
|
|
56
|
+ const address = crypto.randomBytes(32)
|
|
57
|
+ const encryptionKey = crypto.encryptionKey()
|
39
|
58
|
|
40
|
|
- const accessKey = crypto.pack(pubKey, secretKey)
|
|
59
|
+ const accessKey = crypto.pack(address, encryptionKey)
|
41
|
60
|
assert.ok(accessKey, 'Key successfully generated')
|
42
|
61
|
assert.ok(accessKey instanceof Buffer, 'Access key is a buffer')
|
43
|
62
|
assert.same(accessKey.length, 64, 'read key is 64 bytes')
|
44
|
|
- assert.same(accessKey.slice(0, 32), pubKey)
|
45
|
|
- assert.same(accessKey.slice(32, 64), secretKey)
|
|
63
|
+ assert.same(accessKey.slice(0, 32), address)
|
|
64
|
+ assert.same(accessKey.slice(32, 64), encryptionKey)
|
46
|
65
|
next()
|
47
|
66
|
})
|
48
|
67
|
|
49
|
|
- context('pack an access key given as strings', (assert, next) => {
|
50
|
|
- const pubKey = crypto.randomBytes(32)
|
51
|
|
- const secretKey = crypto.symmetricKey()
|
|
68
|
+ context('pack(string, string)', (assert, next) => {
|
|
69
|
+ const address = crypto.randomBytes(32)
|
|
70
|
+ const secretKey = crypto.encryptionKey()
|
52
|
71
|
|
53
|
|
- const accessKey = crypto.pack(pubKey.toString('hex'), secretKey.toString('hex'))
|
|
72
|
+ const accessKey = crypto.pack(address.toString('hex'), secretKey.toString('hex'))
|
54
|
73
|
assert.ok(accessKey, 'Key successfully generated')
|
55
|
74
|
assert.ok(accessKey instanceof Buffer, 'Access key is a buffer')
|
56
|
75
|
assert.same(accessKey.length, 64, 'read key is 64 bytes')
|
57
|
|
- assert.same(accessKey.slice(0, 32), pubKey)
|
|
76
|
+ assert.same(accessKey.slice(0, 32), address)
|
58
|
77
|
assert.same(accessKey.slice(32, 64), secretKey)
|
59
|
78
|
next()
|
60
|
79
|
})
|
61
|
80
|
|
62
|
|
- context('unpack an access key', (assert, next) => {
|
|
81
|
+ context('unpack(buffer)', (assert, next) => {
|
63
|
82
|
const accessKey = crypto.accessKey()
|
64
|
83
|
|
65
|
84
|
const keys = crypto.unpack(accessKey)
|
66
|
|
- const pubKey = accessKey.slice(0, 32)
|
67
|
|
- const symKey = accessKey.slice(32, 64)
|
|
85
|
+ const address = accessKey.slice(0, 32)
|
|
86
|
+ const encryptionKey = accessKey.slice(32, 64)
|
68
|
87
|
|
69
|
|
- assert.same(keys.publicKey, pubKey, 'Unpacks the public key')
|
70
|
|
- assert.same(keys.symmetricKey, symKey, 'Unpacks the symmetric key')
|
|
88
|
+ assert.same(keys.address, address, 'Unpacks the public key')
|
|
89
|
+ assert.same(keys.encryptionKey, encryptionKey, 'Unpacks the symmetric key')
|
71
|
90
|
next()
|
72
|
91
|
})
|
73
|
92
|
|
74
|
|
- context('check a key is a valid key', (assert, next) => {
|
|
93
|
+ context('isKey()', (assert, next) => {
|
75
|
94
|
const accessKey = crypto.accessKey()
|
76
|
95
|
assert.ok(crypto.isKey(accessKey), '64 byte buffer is a valid read/write key')
|
77
|
96
|
assert.ok(crypto.isKey(accessKey.toString('hex')), '64 byte string is a valid read/write key')
|
|
97
|
+
|
78
|
98
|
const blindKey = crypto.randomBytes(32)
|
79
|
99
|
assert.ok(crypto.isKey(blindKey), '32 byte public key is a valid blind replication key')
|
80
|
100
|
assert.ok(crypto.isKey(blindKey.toString('hex')), '32 byte string is a valid blind replication key')
|
|
101
|
+
|
81
|
102
|
next()
|
82
|
103
|
})
|
83
|
104
|
|
84
|
|
- context('unpack an access key given as a string', (assert, next) => {
|
|
105
|
+ context('unpack(string)', (assert, next) => {
|
85
|
106
|
const accessKey = crypto.accessKey()
|
86
|
107
|
|
87
|
108
|
const keys = crypto.unpack(accessKey.toString('hex'))
|
88
|
|
- const pubKey = accessKey.slice(0, 32)
|
89
|
|
- const symKey = accessKey.slice(32, 64)
|
|
109
|
+ const address = accessKey.slice(0, 32)
|
|
110
|
+ const encryptionKey = accessKey.slice(32, 64)
|
|
111
|
+
|
|
112
|
+ assert.same(keys.address, address, 'Unpacks the public key')
|
|
113
|
+ assert.same(keys.encryptionKey, encryptionKey, 'Unpacks the symmetric key')
|
|
114
|
+ next()
|
|
115
|
+ })
|
|
116
|
+
|
|
117
|
+ context('mnemnonic', (assert, next) => {
|
|
118
|
+ const encryptionKey = crypto.encryptionKey()
|
|
119
|
+ const words = crypto.keyToMnemonic(encryptionKey)
|
|
120
|
+ assert.ok(words, 'generates a mnemonic')
|
|
121
|
+ assert.ok(words.split(/\s/).length, 24, '24 words long')
|
|
122
|
+
|
|
123
|
+ var check = crypto.mnemonicToKey(words)
|
|
124
|
+ assert.same(encryptionKey, check, 'returns the correct key')
|
90
|
125
|
|
91
|
|
- assert.same(keys.publicKey, pubKey, 'Unpacks the public key')
|
92
|
|
- assert.same(keys.symmetricKey, symKey, 'Unpacks the symmetric key')
|
93
|
126
|
next()
|
94
|
127
|
})
|
95
|
128
|
})
|
96
|
129
|
|
97
|
130
|
describe('message encoding', (context) => {
|
98
|
131
|
context('encrypt and decrypt a message', (assert, next) => {
|
99
|
|
- const key = crypto.symmetricKey()
|
|
132
|
+ const key = crypto.encryptionKey()
|
100
|
133
|
const encoder = crypto.encoder(key, { valueEncoding: 'utf-8' })
|
101
|
134
|
|
102
|
135
|
const encrypted = encoder.encode("Hello World")
|
|
@@ -117,7 +150,7 @@ describe('hypercore', (context) => {
|
117
|
150
|
})
|
118
|
151
|
|
119
|
152
|
context('encrypted the log', (assert, next) => {
|
120
|
|
- const key = crypto.symmetricKey()
|
|
153
|
+ const key = crypto.encryptionKey()
|
121
|
154
|
const encoder = crypto.encoder(key, { valueEncoding: 'utf-8' })
|
122
|
155
|
const feed = hypercore(storage, { valueEncoding: encoder })
|
123
|
156
|
|
|
@@ -138,7 +171,7 @@ describe('hypercore', (context) => {
|
138
|
171
|
})
|
139
|
172
|
|
140
|
173
|
context('encrypted the log, with a json object', (assert, next) => {
|
141
|
|
- const key = crypto.symmetricKey()
|
|
174
|
+ const key = crypto.encryptionKey()
|
142
|
175
|
const encoder = crypto.encoder(key, { valueEncoding: 'json' })
|
143
|
176
|
const feed = hypercore(storage, { valueEncoding: encoder })
|
144
|
177
|
|