Browse Source

Merge branch 'integrate-with-cobox-core' of CoBox/cobox-crypto into master

kyphae 3 years ago
parent
commit
2d9fb146c5
2 changed files with 17 additions and 0 deletions
  1. 7
    0
      index.js
  2. 10
    0
      test/index.test.js

+ 7
- 0
index.js View File

@@ -38,6 +38,13 @@ class Crypto {
38 38
     return { publicKey, symmetricKey }
39 39
   }
40 40
 
41
+  isKey (key) {
42
+    if (!(key instanceof Buffer || typeof key === 'string')) return false
43
+    const length = Buffer.from(key, 'hex').length
44
+    return length === sodium.crypto_sign_PUBLICKEYBYTES ||
45
+      length === sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_secretbox_KEYBYTES
46
+  }
47
+
41 48
   isAccessKey (key) {
42 49
     try { var keys = this.unpack(key) }
43 50
     catch (err) { return false }

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

@@ -57,6 +57,16 @@ describe('key generation', (context) => {
57 57
     next()
58 58
   })
59 59
 
60
+  context('check a key is a valid key', (assert, next) => {
61
+    const accessKey = crypto.accessKey()
62
+    assert.ok(crypto.isKey(accessKey), '64 byte buffer is a valid read/write key')
63
+    assert.ok(crypto.isKey(accessKey.toString('hex')), '64 byte string is a valid read/write key')
64
+    const blindKey = crypto.keyPair().publicKey
65
+    assert.ok(crypto.isKey(blindKey), '32 byte public key is a valid blind replication key')
66
+    assert.ok(crypto.isKey(blindKey.toString('hex')), '32 byte string is a valid blind replication key')
67
+    next()
68
+  })
69
+
60 70
   context('check a key is a valid blind replication / public key', (assert, next) => {
61 71
     const accessKey = crypto.accessKey()
62 72
     assert.same(false, crypto.isBlindKey(accessKey), 'access key is not a valid blind replication key')