Browse Source

transform a string into secure buffer (encryptionKey)

Kieran Gibb 2 years ago
parent
commit
0ee347eaf8
No known key found for this signature in database
2 changed files with 20 additions and 12 deletions
  1. 3
    2
      index.js
  2. 17
    10
      test/index.test.js

+ 3
- 2
index.js View File

@@ -18,9 +18,10 @@ class Crypto {
18 18
     return this.randomBytes(sodium.crypto_secretbox_KEYBYTES)
19 19
   }
20 20
 
21
-  encryptionKey () {
21
+  encryptionKey (encryptionKey) {
22 22
     var key = sodium.sodium_malloc(sodium.crypto_secretbox_KEYBYTES)
23
-    sodium.randombytes_buf(key)
23
+    if (encryptionKey) key.write(encryptionKey)
24
+    else sodium.randombytes_buf(key)
24 25
     return key
25 26
   }
26 27
 

+ 17
- 10
test/index.test.js View File

@@ -8,13 +8,13 @@ const crypto = require('../')
8 8
 const { cleanup, tmp } = require('./util')
9 9
 
10 10
 describe('key generation', (context) => {
11
-  context('generate a master key', (assert, next) => {
11
+  context('masterKey()', (assert, next) => {
12 12
     const masterKey = crypto.masterKey()
13 13
     assert.ok(masterKey, 'key generated successfully')
14 14
     next()
15 15
   })
16 16
 
17
-  context('generate an derived asymmetric keypair', (assert, next) => {
17
+  context('keyPair()', (assert, next) => {
18 18
     const masterKey = crypto.masterKey()
19 19
     const keypair = crypto.keyPair(masterKey, 0)
20 20
     assert.ok(keypair, 'keys successfully generated')
@@ -23,14 +23,21 @@ describe('key generation', (context) => {
23 23
     next()
24 24
   })
25 25
 
26
-  context('generate an shared encryption key', (assert, next) => {
26
+  context('encryptionKey()', (assert, next) => {
27 27
     const key = crypto.encryptionKey()
28 28
     assert.ok(key, 'Key successfully generated')
29
-    assert.ok(key instanceof Buffer, 'Symmetric key is a buffer')
29
+    assert.ok(key instanceof Buffer, 'key is a secure buffer')
30 30
     next()
31 31
   })
32 32
 
33
-  context('generate an access key', (assert, next) => {
33
+  context('encryptionKey(key)', (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')
37
+    next()
38
+  })
39
+
40
+  context('accessKey()', (assert, next) => {
34 41
     const accessKey = crypto.accessKey()
35 42
     assert.ok(accessKey, 'Key successfully generated')
36 43
     assert.same(accessKey.length, 64, 'Read key is 64 bytes')
@@ -38,7 +45,7 @@ describe('key generation', (context) => {
38 45
     next()
39 46
   })
40 47
 
41
-  context('pack an access key', (assert, next) => {
48
+  context('pack(buffer, buffer)', (assert, next) => {
42 49
     const address = crypto.randomBytes(32)
43 50
     const encryptionKey = crypto.encryptionKey()
44 51
 
@@ -51,7 +58,7 @@ describe('key generation', (context) => {
51 58
     next()
52 59
   })
53 60
 
54
-  context('pack an access key given as strings', (assert, next) => {
61
+  context('pack(string, string)', (assert, next) => {
55 62
     const address = crypto.randomBytes(32)
56 63
     const secretKey = crypto.encryptionKey()
57 64
 
@@ -64,7 +71,7 @@ describe('key generation', (context) => {
64 71
     next()
65 72
   })
66 73
 
67
-  context('unpack an access key', (assert, next) => {
74
+  context('unpack(buffer)', (assert, next) => {
68 75
     const accessKey = crypto.accessKey()
69 76
 
70 77
     const keys = crypto.unpack(accessKey)
@@ -76,7 +83,7 @@ describe('key generation', (context) => {
76 83
     next()
77 84
   })
78 85
 
79
-  context('check a key is a valid key', (assert, next) => {
86
+  context('isKey()', (assert, next) => {
80 87
     const accessKey = crypto.accessKey()
81 88
     assert.ok(crypto.isKey(accessKey), '64 byte buffer is a valid read/write key')
82 89
     assert.ok(crypto.isKey(accessKey.toString('hex')), '64 byte string is a valid read/write key')
@@ -88,7 +95,7 @@ describe('key generation', (context) => {
88 95
     next()
89 96
   })
90 97
 
91
-  context('unpack an access key given as a string', (assert, next) => {
98
+  context('unpack(string)', (assert, next) => {
92 99
     const accessKey = crypto.accessKey()
93 100
 
94 101
     const keys = crypto.unpack(accessKey.toString('hex'))