Browse Source

use bufferAlloc

Kieran Gibb 2 years ago
parent
commit
02d8786ba7
No known key found for this signature in database
1 changed files with 4 additions and 7 deletions
  1. 4
    7
      index.js

+ 4
- 7
index.js View File

@@ -1,9 +1,6 @@
1 1
 const sodium = require('sodium-native')
2 2
 const crypto = require('hypercore-crypto')
3 3
 const assert = require('assert')
4
-
5
-// TODO: discussion around whether to use sodium.sodium_malloc() or bufferAlloc()
6
-// Research needs to be done about secure buffers
7 4
 const bufferAlloc = require('buffer-alloc-unsafe')
8 5
 
9 6
 module.exports = () => new Crypto()
@@ -15,10 +12,10 @@ class Crypto {
15 12
     return key
16 13
   }
17 14
 
18
-  keyPair (masterKey, id, conText = 'cobox') {
19
-    const context = sodium.sodium_malloc(sodium.crypto_hash_sha256_BYTES)
20
-    sodium.crypto_hash_sha256(context, Buffer.from(conText))
21
-    const seed = sodium.sodium_malloc(sodium.crypto_kdf_KEYBYTES)
15
+  keyPair (masterKey, id, ctxt = 'cobox') {
16
+    const context = bufferAlloc(sodium.crypto_hash_sha256_BYTES)
17
+    sodium.crypto_hash_sha256(context, Buffer.from(ctxt))
18
+    const seed = bufferAlloc(sodium.crypto_kdf_KEYBYTES)
22 19
     sodium.crypto_kdf_derive_from_key(seed, id, context, masterKey)
23 20
     return crypto.keyPair(seed)
24 21
   }