|
@@ -1,6 +1,6 @@
|
1
|
1
|
# cobox-crypto
|
2
|
2
|
|
3
|
|
-The crypto primitives used in cobox, extracted into a separate module
|
|
3
|
+Crypto primitives used in cobox
|
4
|
4
|
|
5
|
5
|
```
|
6
|
6
|
npm install cobox-crypto
|
|
@@ -9,22 +9,25 @@ npm install cobox-crypto
|
9
|
9
|
## Usage
|
10
|
10
|
|
11
|
11
|
```
|
12
|
|
-const Crypto = require('cobox-crypto')
|
13
|
|
-const crypto = Crypto()
|
14
|
|
-
|
15
|
|
-const accessKey = crypto.accessKey()
|
|
12
|
+const crypto = require('cobox-crypto')
|
16
|
13
|
```
|
17
|
14
|
|
18
|
15
|
## API
|
19
|
16
|
|
20
|
17
|
```js
|
21
|
|
-keyPair = crypto.keyPair()
|
|
18
|
+const address = crypto.address()
|
|
19
|
+```
|
|
20
|
+
|
|
21
|
+Returns a `ed25519` random 32 byte buffer
|
|
22
|
+
|
|
23
|
+```js
|
|
24
|
+const keyPair = crypto.keyPair()
|
22
|
25
|
```
|
23
|
26
|
|
24
|
27
|
Returns an `ed25519` keypair that can used for tree signing.
|
25
|
28
|
|
26
|
29
|
```js
|
27
|
|
-const symKey = crypto.symmetricKey()
|
|
30
|
+const encKey = crypto.encryptionKey()
|
28
|
31
|
```
|
29
|
32
|
|
30
|
33
|
Returns an `ed25519` symmetric key used for shared secret encryption
|
|
@@ -34,53 +37,37 @@ const accessKey = crypto.accessKey()
|
34
|
37
|
|
35
|
38
|
// OR
|
36
|
39
|
|
37
|
|
-const accessKey = crypto.pack(pubKey, symKey)
|
|
40
|
+const accessKey = crypto.pack(address, encKey)
|
38
|
41
|
```
|
39
|
42
|
|
40
|
|
-Returns an access key, which consists of an `ed25519` public key, packed together with an `ed25519` symmetric key
|
|
43
|
+Returns an access key, which consists of an `ed25519` address, packed together with an `ed25519` symmetric key
|
41
|
44
|
|
42
|
45
|
```js
|
43
|
46
|
const keys = crypto.unpack(key)
|
44
|
47
|
```
|
45
|
48
|
|
46
|
|
-Returns an object containing a public key, and a shared secret if accessible. Public key alone is used for blind replication. The shared secret can then be used for decryption.
|
|
49
|
+Returns an object containing an address, and a shared secret if accessible. Address alone is used for blind replication. The shared secret can then be used for decryption.
|
47
|
50
|
|
48
|
51
|
```js
|
49
|
|
-const valueEncoding = crypto.encoder(encryptionKey, {})
|
|
52
|
+const { publicKey, secretKey } = crypto.boxKeyPair(seed)
|
50
|
53
|
```
|
51
|
54
|
|
52
|
|
-Returns a message encoder used for encrypting messages in hypercore. Can be passed to hypercore doing the following:
|
|
55
|
+Returns an `ed25519` private box keypair used for identification, message signing and encryption
|
53
|
56
|
|
54
|
57
|
```js
|
55
|
|
-const accessKey = crypto.accessKey
|
56
|
|
-const keys = crypto.unpack(accessKey)
|
57
|
|
-var feed = hypercore(storage, keys.publicKey, {
|
58
|
|
- valueEncoding: crypto.encoder(keys.symmetricKey, { valueEncoding: 'utf-8' })
|
59
|
|
-})
|
60
|
|
-
|
61
|
|
-feed.ready(() => {
|
62
|
|
- feed.append("this is going to be encrypted", (err, seq) => {
|
63
|
|
- // do other stuff...
|
64
|
|
- })
|
65
|
|
-})
|
|
58
|
+const boxed = box(publicKey, message, [context])
|
66
|
59
|
```
|
67
|
60
|
|
68
|
|
-```js
|
69
|
|
-const { publicKey, secretKey } = boxKeypair(seed)
|
70
|
|
-```
|
71
|
|
-
|
72
|
|
-```js
|
73
|
|
-const boxed = box(publicKey, message, [contextMessage])
|
74
|
|
-```
|
75
|
61
|
Encrypts a message to a given public key and returns it as a buffer
|
76
|
62
|
- `publicKey` buffer or hex encoded string
|
77
|
63
|
- `message` buffer or hex encoded string of any length
|
78
|
|
-- `contextMessage`, if passed, will be hashed in to the shared secret. Should be a buffer or hex encoded string.
|
|
64
|
+- `context`, if passed, will be hashed in to the shared secret. Should be a buffer or hex encoded string.
|
79
|
65
|
|
80
|
66
|
```js
|
81
|
|
-const unboxed = unbox(cipherText, keypair, [contextMessage])
|
|
67
|
+const unboxed = unbox(cipherText, keypair, [context])
|
82
|
68
|
```
|
|
69
|
+
|
83
|
70
|
Decrypts a message using the given keypair.
|
84
|
71
|
- `cipherText` the encrypted message given as a buffer.
|
85
|
72
|
- `keypair` an object of the form `{ publicKey, secretKey }` both of which should be buffers or hex encoded strings.
|
86
|
|
-- `contextMessage`, if given, will be hashed into the shared secret. Should be a buffer or hex encoded string.
|
|
73
|
+- `context`, if given, will be hashed into the shared secret. Should be a buffer or hex encoded string.
|