Browse Source

Merge branch 'master' of keybase://team/blockades.cobox.development/cobox-crypto

Kieran Gibb 3 years ago
parent
commit
4a49881a25
No account linked to committer's email address
3 changed files with 55 additions and 17 deletions
  1. 20
    5
      index.js
  2. 30
    8
      test/index.test.js
  3. 5
    4
      test/util.js

+ 20
- 5
index.js View File

@@ -26,11 +26,11 @@ class Crypto {
26 26
     return accessKey
27 27
   }
28 28
 
29
-  unpack (accessKey) {
30
-    if (accessKey.length < sodium.crypto_sign_PUBLICKEYBYTES) throw new Error('Invalid key length')
31
-    if (accessKey.length === sodium.crypto_sign_PUBLICKEYBYTES) return { publicKey: accessKey }
32
-    const publicKey = accessKey.slice(0, sodium.crypto_sign_PUBLICKEYBYTES)
33
-    const symmetricKey = accessKey.slice(sodium.crypto_secretbox_KEYBYTES)
29
+  unpack (key) {
30
+    if (key.length < sodium.crypto_sign_PUBLICKEYBYTES) throw new Error('Invalid key length')
31
+    if (key.length === sodium.crypto_sign_PUBLICKEYBYTES) return { publicKey: key }
32
+    const publicKey = key.slice(0, sodium.crypto_sign_PUBLICKEYBYTES)
33
+    const symmetricKey = key.slice(sodium.crypto_secretbox_KEYBYTES)
34 34
     return { publicKey, symmetricKey }
35 35
   }
36 36
 
@@ -62,6 +62,8 @@ class Crypto {
62 62
 
63 63
     assert(can, 'cobox-crypto: key must be a buffer of length ' + sodium.crypto_secretbox_KEYBYTES)
64 64
 
65
+    opts.valueEncoding = this._resolveStringEncoder(opts.valueEncoding)
66
+
65 67
     return {
66 68
       encode (message, buffer, offset) {
67 69
         // Run originally provided encoder if any
@@ -93,4 +95,17 @@ class Crypto {
93 95
       }
94 96
     }
95 97
   }
98
+
99
+  _resolveStringEncoder (encoder) {
100
+    if (encoder === 'json') return {
101
+      encode: (msg) => Buffer.from(JSON.stringify(msg)),
102
+      decode: (msg) => JSON.parse(msg.toString())
103
+    }
104
+
105
+    if (encoder === 'utf-8') return {
106
+      encode: (msg) => Buffer.from(msg),
107
+      decode: (msg) => msg.toString()
108
+    }
109
+    return encoder
110
+  }
96 111
 }

+ 30
- 8
test/index.test.js View File

@@ -84,15 +84,19 @@ describe('message encoding', (context) => {
84 84
     assert.ok(encrypted instanceof Buffer, 'Returns a buffer')
85 85
 
86 86
     const message = encoder.decode(encrypted)
87
-    assert.same(message, Buffer.from('Hello World'), 'Decrypts the message')
87
+    assert.same(message, 'Hello World', 'Decrypts the message')
88 88
     next()
89 89
   })
90 90
 })
91 91
 
92 92
 describe('hypercore', (context) => {
93
-  context('encrypted the log', (assert, next) => {
94
-    const storage = tmp()
93
+  let storage
94
+
95
+  context.beforeEach((c) => {
96
+    storage = tmp()
97
+  })
95 98
 
99
+  context('encrypted the log', (assert, next) => {
96 100
     const key = crypto.symmetricKey()
97 101
     const encoder = crypto.encoder(key, { valueEncoding: 'utf-8' })
98 102
     const feed = hypercore(storage.path, { valueEncoding: encoder })
@@ -101,15 +105,33 @@ describe('hypercore', (context) => {
101 105
       assert.error(err, 'no error')
102 106
 
103 107
       var data = fs.readFileSync(path.join(storage.path, 'data'))
104
-      assert.notSame(data.toString(), 'boop', 'log entry is encrypted')
105
-      assert.same(encoder.decode(data), Buffer.from('boop'), 'log entry is encrypted')
108
+      assert.notSame(data, 'boop', 'log entry is encrypted')
109
+      assert.same(encoder.decode(data), 'boop', 'log entry is encrypted')
110
+
111
+      feed.get(0, (err, entry) => {
112
+        assert.error(err, 'no error')
113
+        assert.same('boop', entry, 'hypercore decrypts the message')
114
+
115
+        cleanup(storage.root, next)
116
+      })
117
+    })
118
+  })
119
+
120
+  context('encrypted the log, with a json object', (assert, next) => {
121
+    const key = crypto.symmetricKey()
122
+    const encoder = crypto.encoder(key, { valueEncoding: 'json' })
123
+    const feed = hypercore(storage.path, { valueEncoding: encoder })
124
+
125
+    const message = { boop: 'beep' }
126
+
127
+    feed.append(message, (err) => {
128
+      assert.error(err, 'no error')
106 129
 
107 130
       feed.get(0, (err, entry) => {
108 131
         assert.error(err, 'no error')
109
-        assert.same(Buffer.from('boop'), entry, 'hypercore decrypts the message')
132
+        assert.same(message, entry, 'hypercore decrypts the message')
110 133
 
111
-        cleanup(storage.root)
112
-        next()
134
+        cleanup(storage.root, next)
113 135
       })
114 136
     })
115 137
   })

+ 5
- 4
test/util.js View File

@@ -3,10 +3,11 @@ const mkdirp = require('mkdirp')
3 3
 const rimraf = require('rimraf')
4 4
 const debug = require('debug')('cobox-crypto')
5 5
 
6
-function cleanup (dir) {
6
+function cleanup (dir, cb) {
7 7
   rimraf(dir, (err) => {
8
-    debug(`[CLEANUP] ${ err ? 'failed' : 'success'}`)
9
-    if (err) throw (err)
8
+    debug(`[CLEANUP] cleaning ${dir} ${ err ? 'failed' : 'success'}`)
9
+    if (err) return console.error(err)
10
+    cb()
10 11
   })
11 12
 }
12 13
 
@@ -14,7 +15,7 @@ function tmp () {
14 15
   var root = `./tmp/`
15 16
   var path = `.${tmpdir().name}`
16 17
   mkdirp.sync(path)
17
-  debug(`[TEMP] ${path}`)
18
+  debug(`[TEMP] generated temp directory ${path}`)
18 19
   return { path, root }
19 20
 }
20 21