Browse Source

use hypercore-crypto-encoder

Kieran Gibb 2 years ago
parent
commit
5e3f9ac821
No known key found for this signature in database
2 changed files with 5 additions and 39 deletions
  1. 4
    39
      index.js
  2. 1
    0
      package.json

+ 4
- 39
index.js View File

@@ -2,6 +2,7 @@ const sodium = require('sodium-native')
2 2
 const crypto = require('hypercore-crypto')
3 3
 const assert = require('assert')
4 4
 const bip39 = require('bip39')
5
+const encoder = require('crypto-encoder')
5 6
 const zero = sodium.sodium_memzero
6 7
 
7 8
 class Crypto {
@@ -24,10 +25,11 @@ class Crypto {
24 25
   }
25 26
 
26 27
   encryptionKey (encryptionKey) {
28
+    if (!encryptionKey) return encoder.encryptionKey()
27 29
     var key = sodium.sodium_malloc(sodium.crypto_secretbox_KEYBYTES)
28 30
     if (encryptionKey && Buffer.isBuffer(encryptionKey)) key.copy(encryptionKey)
29 31
     else if (encryptionKey && typeof encryptionKey === 'string') key.write(encryptionKey, 'hex')
30
-    else sodium.randombytes_buf(key)
32
+    else return encoder.encryptionKey()
31 33
     return key
32 34
   }
33 35
 
@@ -100,44 +102,7 @@ class Crypto {
100 102
   }
101 103
 
102 104
   encoder (encryptionKey, opts = {}) {
103
-    var can = Buffer.isBuffer(encryptionKey) &&
104
-      (encryptionKey.length === sodium.crypto_secretbox_KEYBYTES)
105
-
106
-    assert(can, 'cobox-crypto: key must be a buffer of length ' + sodium.crypto_secretbox_KEYBYTES)
107
-
108
-    opts.valueEncoding = this._resolveStringEncoder(opts.valueEncoding)
109
-
110
-    return {
111
-      encode (message, buffer, offset) {
112
-        // Run originally provided encoder if any
113
-        if (opts.valueEncoding && typeof opts.valueEncoding.encode === 'function') {
114
-          message = opts.valueEncoding.encode(message, buffer, offset)
115
-        }
116
-        if (!Buffer.isBuffer(message)) message = Buffer.from(message, 'utf-8')
117
-        var ciphertext = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
118
-        var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
119
-        sodium.randombytes_buf(nonce)
120
-        sodium.crypto_secretbox_easy(ciphertext, message, nonce, encryptionKey)
121
-        zero(message)
122
-        return Buffer.concat([nonce, ciphertext])
123
-      },
124
-
125
-      decode (buffer, start, end) {
126
-        const nonce = buffer.slice(0, sodium.crypto_secretbox_NONCEBYTES)
127
-        const messageWithMAC = buffer.slice(sodium.crypto_secretbox_NONCEBYTES)
128
-        var message = Buffer.alloc(messageWithMAC.length - sodium.crypto_secretbox_MACBYTES)
129
-        if (!sodium.crypto_secretbox_open_easy(message, messageWithMAC, nonce, encryptionKey)) {
130
-          throw new Error('Decryption failed!')
131
-        } else {
132
-          // Run originally provided encoder if any
133
-          if (opts.valueEncoding && typeof opts.valueEncoding.decode === 'function') {
134
-            return opts.valueEncoding.decode(message, start, end)
135
-          } else {
136
-            return message
137
-          }
138
-        }
139
-      }
140
-    }
105
+    return encoder(encryptionKey, opts)
141 106
   }
142 107
 
143 108
   boxKeypair (seed) {

+ 1
- 0
package.json View File

@@ -13,6 +13,7 @@
13 13
   "dependencies": {
14 14
     "assert": "^2.0.0",
15 15
     "bip39": "^3.0.2",
16
+    "crypto-encoder": "^1.0.1",
16 17
     "hypercore-crypto": "^1.0.0",
17 18
     "sodium-native": "^2.4.2"
18 19
   },