mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-07-20 23:42:16 +00:00
Compare commits
75 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e383da5d44 | ||
|
9b687f380c | ||
|
82abede64a | ||
|
a56ab406e4 | ||
|
d753941c6e | ||
|
6022eb0838 | ||
|
1c6d0912cd | ||
|
e18804c31e | ||
|
133a2c8833 | ||
|
fad0865b2b | ||
|
4edef07182 | ||
|
19c6ce7c06 | ||
|
3fa76a874f | ||
|
6e0fd6f257 | ||
|
c91d9b61c8 | ||
|
76eeb5aa18 | ||
|
843b5e33d6 | ||
|
308ac7cd1a | ||
|
6c8f978ea4 | ||
|
b93b410357 | ||
|
99a5245fa3 | ||
|
34856d58ab | ||
|
d59c6af7c0 | ||
|
1683bf1dc8 | ||
|
a4edf8b99b | ||
|
27524354af | ||
|
a1f054ca8e | ||
|
5c61c89391 | ||
|
c138a04d2d | ||
|
7c913c0769 | ||
|
1a2d468369 | ||
|
98bc0bbc5f | ||
|
c45bdf602e | ||
|
e92bab1736 | ||
|
c57d1e4d4f | ||
|
04682acd86 | ||
|
fba4833aef | ||
|
2842df7944 | ||
|
74c0d28f86 | ||
|
cf7ed6fa49 | ||
|
c1ffa41697 | ||
|
933119445f | ||
|
a4e6f9dd83 | ||
|
e252db300c | ||
|
98b37d49c4 | ||
|
f979fcd3c2 | ||
|
0af48bbebc | ||
|
3f9d8d557c | ||
|
91a3b50ac9 | ||
|
148d16ab25 | ||
|
6d15450438 | ||
|
ebe1cecdeb | ||
|
904cfb27bd | ||
|
7790beb207 | ||
|
6308461f0f | ||
|
95a0f1f0c2 | ||
|
0c64122342 | ||
|
ce5044b4d7 | ||
|
1daf429a74 | ||
|
22e95bc8a4 | ||
|
9994023490 | ||
|
393fa17512 | ||
|
3cc26d167f | ||
|
a7c2567ba2 | ||
|
69edfff49b | ||
|
9a0e2585d8 | ||
|
12630dc514 | ||
|
b088bab80f | ||
|
cecadba14d | ||
|
6ebc408f73 | ||
|
08c5df5e79 | ||
|
1cd614237a | ||
|
9795ac0722 | ||
|
73a5258876 | ||
|
c35a988331 |
13
.aegir.js
13
.aegir.js
@@ -1,13 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
webpack: {
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
'node-forge': path.resolve(__dirname, 'vendor/forge.bundle.js')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -31,5 +31,4 @@ build
|
|||||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
lib
|
|
||||||
dist
|
dist
|
||||||
|
18
.travis.yml
18
.travis.yml
@@ -1,9 +1,16 @@
|
|||||||
sudo: false
|
sudo: false
|
||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
|
||||||
- 4
|
matrix:
|
||||||
- 5
|
include:
|
||||||
- stable
|
- node_js: 4
|
||||||
|
env: CXX=g++-4.8
|
||||||
|
- node_js: 6
|
||||||
|
env:
|
||||||
|
- SAUCE=true
|
||||||
|
- CXX=g++-4.8
|
||||||
|
- node_js: stable
|
||||||
|
env: CXX=g++-4.8
|
||||||
|
|
||||||
# Make sure we have new NPM.
|
# Make sure we have new NPM.
|
||||||
before_install:
|
before_install:
|
||||||
@@ -22,9 +29,6 @@ before_script:
|
|||||||
after_success:
|
after_success:
|
||||||
- npm run coverage-publish
|
- npm run coverage-publish
|
||||||
|
|
||||||
env:
|
|
||||||
- CXX=g++-4.8
|
|
||||||
|
|
||||||
addons:
|
addons:
|
||||||
firefox: 'latest'
|
firefox: 'latest'
|
||||||
apt:
|
apt:
|
||||||
|
126
README.md
126
README.md
@@ -9,11 +9,16 @@
|
|||||||
[](https://circleci.com/gh/libp2p/js-libp2p-crypto)
|
[](https://circleci.com/gh/libp2p/js-libp2p-crypto)
|
||||||
[](https://david-dm.org/libp2p/js-libp2p-crypto)
|
[](https://david-dm.org/libp2p/js-libp2p-crypto)
|
||||||
[](https://github.com/feross/standard)
|
[](https://github.com/feross/standard)
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
[](https://saucelabs.com/u/ipfs-js-
|
||||||
|
libp2p-crypto)
|
||||||
|
|
||||||
> Crypto primitives for libp2p in JavaScript
|
> Crypto primitives for libp2p in JavaScript
|
||||||
|
|
||||||
This repo contains the JavaScript implementation of the crypto primitives
|
This repo contains the JavaScript implementation of the crypto primitives
|
||||||
needed for libp2p. This is based on this [go implementation](https://github.com/ipfs/go-libp2p-crypto).
|
needed for libp2p. This is based on this [go implementation](https://github.com/libp2p/go-libp2p-crypto).
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
@@ -21,13 +26,22 @@ needed for libp2p. This is based on this [go implementation](https://github.com/
|
|||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Example](#example)
|
- [Example](#example)
|
||||||
- [API](#api)
|
- [API](#api)
|
||||||
- [`generateKeyPair(type, bits)`](#generatekeypairtype-bits)
|
- [`hmac`](#hmac)
|
||||||
- [`generateEphemeralKeyPair(curve)`](#generateephemeralkeypaircurve)
|
- [`create(hash, secret, callback)`](#createhash-secret-callback)
|
||||||
- [`keyStretcher(cipherType, hashType, secret)`](#keystretcherciphertype-hashtype-secret)
|
- [`digest(data, callback)`](#digestdata-callback)
|
||||||
- [`marshalPublicKey(key[, type])`](#marshalpublickeykey-type)
|
- [`aes`](#aes)
|
||||||
|
- [`create(key, iv, callback)`](#createkey-iv-callback)
|
||||||
|
- [`encrypt(data, callback)`](#encryptdata-callback)
|
||||||
|
- [`encrypt(data, callback)`](#encryptdata-callback)
|
||||||
|
- [`webcrypto`](#webcrypto)
|
||||||
|
- [`keys`](#keys)
|
||||||
|
- [`generateKeyPair(type, bits, callback)`](#generatekeypairtype-bits-callback)
|
||||||
|
- [`generateEphemeralKeyPair(curve, callback)`](#generateephemeralkeypaircurve-callback)
|
||||||
|
- [`keyStretcher(cipherType, hashType, secret, callback)`](#keystretcherciphertype-hashtype-secret-callback)
|
||||||
|
- [`marshalPublicKey(key[, type], callback)`](#marshalpublickeykey-type-callback)
|
||||||
- [`unmarshalPublicKey(buf)`](#unmarshalpublickeybuf)
|
- [`unmarshalPublicKey(buf)`](#unmarshalpublickeybuf)
|
||||||
- [`marshalPrivateKey(key[, type])`](#marshalprivatekeykey-type)
|
- [`marshalPrivateKey(key[, type])`](#marshalprivatekeykey-type)
|
||||||
- [`unmarshalPrivateKey(buf)`](#unmarshalprivatekeybuf)
|
- [`unmarshalPrivateKey(buf, callback)`](#unmarshalprivatekeybuf-callback)
|
||||||
- [Contribute](#contribute)
|
- [Contribute](#contribute)
|
||||||
- [License](#license)
|
- [License](#license)
|
||||||
|
|
||||||
@@ -44,27 +58,86 @@ npm install --save libp2p-crypto
|
|||||||
```js
|
```js
|
||||||
const crypto = require('libp2p-crypto')
|
const crypto = require('libp2p-crypto')
|
||||||
|
|
||||||
var keyPair = crypto.generateKeyPair('RSA', 2048)
|
crypto.generateKeyPair('RSA', 2048, (err, key) => {
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### `generateKeyPair(type, bits)`
|
### `hmac`
|
||||||
|
|
||||||
- `type: String`, only `'RSA'` is currently supported
|
Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as defined in U.S. Federal Information Processing Standards Publication 198. An HMAC is a cryptographic hash that uses a key to sign a message. The receiver verifies the hash by recomputing it using the same key.
|
||||||
- `bits: Number`
|
|
||||||
|
#### `create(hash, secret, callback)`
|
||||||
|
|
||||||
|
- `hash: String`
|
||||||
|
- `secret: Buffer`
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
|
##### `digest(data, callback)`
|
||||||
|
|
||||||
|
- `data: Buffer`
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
|
### `aes`
|
||||||
|
Expoes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
|
||||||
|
|
||||||
|
This uses `CTR` mode.
|
||||||
|
|
||||||
|
#### `create(key, iv, callback)`
|
||||||
|
|
||||||
|
- `key: Buffer` The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used.
|
||||||
|
- `iv: Buffer` Must have length `16`.
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
|
##### `encrypt(data, callback)`
|
||||||
|
|
||||||
|
- `data: Buffer`
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
|
##### `encrypt(data, callback)`
|
||||||
|
|
||||||
|
- `data: Buffer`
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
|
|
||||||
|
### `webcrypto`
|
||||||
|
|
||||||
|
Depending on the environment this is either an instance of [node-webcrypto-ossl](https://github.com/PeculiarVentures/node-webcrypto-ossl) or the result of `window.crypto`.
|
||||||
|
|
||||||
|
### `keys`
|
||||||
|
|
||||||
|
#### Supported Key Types
|
||||||
|
|
||||||
|
The [`generateKeyPair`](#generatekeypairtype-bits-callback), [`marshalPublicKey`](#marshalpublickeykey-type-callback), and
|
||||||
|
[`marshalPrivateKey`](#marshalprivatekeykey-type) functions accept a string `type` argument.
|
||||||
|
|
||||||
|
Currently the `'RSA'` and `'ed25519'` types are supported, although ed25519 keys support only signing and
|
||||||
|
verification of messages. For encryption / decryption support, RSA keys should be used.
|
||||||
|
|
||||||
|
Installing the [libp2p-crypto-secp256k1](https://github.com/libp2p/js-libp2p-crypto-secp256k1) module adds support for the
|
||||||
|
`'secp256k1'` type, which supports ECDSA signatures using the secp256k1 elliptic curve popularized by Bitcoin. This module
|
||||||
|
is not installed by default, and should be explicitly depended on if your project requires secp256k1 support.
|
||||||
|
|
||||||
|
### `generateKeyPair(type, bits, callback)`
|
||||||
|
|
||||||
|
- `type: String`, see [Supported Key Types](#supported-key-types) above.
|
||||||
|
- `bits: Number` Minimum of 1024
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
Generates a keypair of the given type and bitsize.
|
Generates a keypair of the given type and bitsize.
|
||||||
|
|
||||||
### `generateEphemeralKeyPair(curve)`
|
### `generateEphemeralKeyPair(curve, callback)`
|
||||||
|
|
||||||
- `curve: String`, one of `'P-256'`, `'P-384'`, `'P-521'` is currently supported
|
- `curve: String`, one of `'P-256'`, `'P-384'`, `'P-521'` is currently supported
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
Generates an ephemeral public key and returns a function that will compute the shared secret key.
|
Generates an ephemeral public key and returns a function that will compute the shared secret key.
|
||||||
|
|
||||||
Focuses only on ECDH now, but can be made more general in the future.
|
Focuses only on ECDH now, but can be made more general in the future.
|
||||||
|
|
||||||
Returns an object of the form
|
Calls back with an object of the form
|
||||||
|
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
key: Buffer,
|
key: Buffer,
|
||||||
@@ -72,15 +145,16 @@ Returns an object of the form
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### `keyStretcher(cipherType, hashType, secret)`
|
### `keyStretcher(cipherType, hashType, secret, callback)`
|
||||||
|
|
||||||
- `cipherType: String`, one of `'AES-128'`, `'AES-256'`, `'Blowfish'`
|
- `cipherType: String`, one of `'AES-128'`, `'AES-256'`, `'Blowfish'`
|
||||||
- `hashType: String`, one of `'SHA1'`, `SHA256`, `SHA512`
|
- `hashType: String`, one of `'SHA1'`, `SHA256`, `SHA512`
|
||||||
- `secret: Buffer`
|
- `secret: Buffer`
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
Generates a set of keys for each party by stretching the shared key.
|
Generates a set of keys for each party by stretching the shared key.
|
||||||
|
|
||||||
Returns an object of the form
|
Calls back with an object of the form
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
k1: {
|
k1: {
|
||||||
@@ -95,10 +169,11 @@ Returns an object of the form
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
### `marshalPublicKey(key[, type])`
|
|
||||||
|
|
||||||
- `key: crypto.rsa.RsaPublicKey`
|
### `marshalPublicKey(key[, type], callback)`
|
||||||
- `type: String`, only `'RSA'` is currently supported
|
|
||||||
|
- `key: keys.rsa.RsaPublicKey | keys.ed25519.Ed25519PublicKey | require('libp2p-crypto-secp256k1').Secp256k1PublicKey`
|
||||||
|
- `type: String`, see [Supported Key Types](#supported-key-types) above.
|
||||||
|
|
||||||
Converts a public key object into a protobuf serialized public key.
|
Converts a public key object into a protobuf serialized public key.
|
||||||
|
|
||||||
@@ -110,20 +185,27 @@ Converts a protobuf serialized public key into its representative object.
|
|||||||
|
|
||||||
### `marshalPrivateKey(key[, type])`
|
### `marshalPrivateKey(key[, type])`
|
||||||
|
|
||||||
- `key: crypto.rsa.RsaPrivateKey`
|
- `key: keys.rsa.RsaPrivateKey | keys.ed25519.Ed25519PrivateKey | require('libp2p-crypto-secp256k1').Secp256k1PrivateKey`
|
||||||
- `type: String`, only `'RSA'` is currently supported
|
- `type: String`, see [Supported Key Types](#supported-key-types) above.
|
||||||
|
|
||||||
Converts a private key object into a protobuf serialized private key.
|
Converts a private key object into a protobuf serialized private key.
|
||||||
|
|
||||||
### `unmarshalPrivateKey(buf)`
|
### `unmarshalPrivateKey(buf, callback)`
|
||||||
|
|
||||||
- `buf: Buffer`
|
- `buf: Buffer`
|
||||||
|
- `callback: Function`
|
||||||
|
|
||||||
Converts a protobuf serialized private key into its representative object.
|
Converts a protobuf serialized private key into its representative object.
|
||||||
|
|
||||||
|
### `randomBytes(number)`
|
||||||
|
|
||||||
|
- `number: Number`
|
||||||
|
|
||||||
|
Generates a Buffer with length `number` populated by random bytes.
|
||||||
|
|
||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-libp2p-crypto/issues)!
|
Feel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-crypto/issues)!
|
||||||
|
|
||||||
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
||||||
|
|
||||||
|
37
benchmarks/ephemeral-keys.js
Normal file
37
benchmarks/ephemeral-keys.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Benchmark = require('benchmark')
|
||||||
|
const crypto = require('../src')
|
||||||
|
|
||||||
|
const suite = new Benchmark.Suite('ephemeral-keys')
|
||||||
|
|
||||||
|
const secrets = []
|
||||||
|
const curves = ['P-256', 'P-384', 'P-521']
|
||||||
|
|
||||||
|
curves.forEach((curve) => {
|
||||||
|
suite.add(`ephemeral key with secrect ${curve}`, (d) => {
|
||||||
|
crypto.generateEphemeralKeyPair('P-256', (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
res.genSharedKey(res.key, (err, secret) => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
secrets.push(secret)
|
||||||
|
|
||||||
|
d.resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}, {
|
||||||
|
defer: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
suite
|
||||||
|
.on('cycle', (event) => {
|
||||||
|
console.log(String(event.target))
|
||||||
|
})
|
||||||
|
.run({
|
||||||
|
async: true
|
||||||
|
})
|
49
benchmarks/key-stretcher.js
Normal file
49
benchmarks/key-stretcher.js
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Benchmark = require('benchmark')
|
||||||
|
const async = require('async')
|
||||||
|
|
||||||
|
const crypto = require('../src')
|
||||||
|
|
||||||
|
const suite = new Benchmark.Suite('key-stretcher')
|
||||||
|
|
||||||
|
const keys = []
|
||||||
|
|
||||||
|
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
||||||
|
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
(cb) => crypto.generateEphemeralKeyPair('P-256', cb),
|
||||||
|
(res, cb) => res.genSharedKey(res.key, cb)
|
||||||
|
], (err, secret) => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
ciphers.forEach((cipher) => hashes.forEach((hash) => {
|
||||||
|
setup(cipher, hash, secret)
|
||||||
|
}))
|
||||||
|
|
||||||
|
suite
|
||||||
|
.on('cycle', (event) => {
|
||||||
|
console.log(String(event.target))
|
||||||
|
})
|
||||||
|
.run({
|
||||||
|
async: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
function setup (cipher, hash, secret) {
|
||||||
|
suite.add(`keyStretcher ${cipher} ${hash}`, (d) => {
|
||||||
|
crypto.keyStretcher(cipher, hash, secret, (err, k) => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
keys.push(k)
|
||||||
|
d.resolve()
|
||||||
|
})
|
||||||
|
}, {
|
||||||
|
defer: true
|
||||||
|
})
|
||||||
|
}
|
52
benchmarks/rsa.js
Normal file
52
benchmarks/rsa.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Benchmark = require('benchmark')
|
||||||
|
const crypto = require('../src')
|
||||||
|
|
||||||
|
const suite = new Benchmark.Suite('rsa')
|
||||||
|
|
||||||
|
const keys = []
|
||||||
|
const bits = [1024, 2048, 4096]
|
||||||
|
|
||||||
|
bits.forEach((bit) => {
|
||||||
|
suite.add(`generateKeyPair ${bit}bits`, (d) => {
|
||||||
|
crypto.generateKeyPair('RSA', bit, (err, key) => {
|
||||||
|
if (err) throw err
|
||||||
|
keys.push(key)
|
||||||
|
d.resolve()
|
||||||
|
})
|
||||||
|
}, {
|
||||||
|
defer: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
suite.add('sign and verify', (d) => {
|
||||||
|
const key = keys[0]
|
||||||
|
const text = key.genSecret()
|
||||||
|
|
||||||
|
key.sign(text, (err, sig) => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
key.public.verify(text, sig, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
if (res !== true) {
|
||||||
|
throw new Error('failed to verify')
|
||||||
|
}
|
||||||
|
d.resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}, {
|
||||||
|
defer: true
|
||||||
|
})
|
||||||
|
|
||||||
|
suite
|
||||||
|
.on('cycle', (event) => {
|
||||||
|
console.log(String(event.target))
|
||||||
|
})
|
||||||
|
.run({
|
||||||
|
async: true
|
||||||
|
})
|
61
package.json
61
package.json
@@ -1,14 +1,22 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-crypto",
|
"name": "libp2p-crypto",
|
||||||
"version": "0.6.1",
|
"version": "0.8.2",
|
||||||
"description": "Crypto primitives for libp2p",
|
"description": "Crypto primitives for libp2p",
|
||||||
"main": "lib/index.js",
|
"main": "src/index.js",
|
||||||
"jsnext:main": "src/index.js",
|
"browser": {
|
||||||
|
"node-webcrypto-ossl": false,
|
||||||
|
"./src/crypto/webcrypto.js": "./src/crypto/webcrypto-browser.js",
|
||||||
|
"./src/crypto/hmac.js": "./src/crypto/hmac-browser.js",
|
||||||
|
"./src/crypto/ecdh.js": "./src/crypto/ecdh-browser.js",
|
||||||
|
"./src/crypto/ciphers.js": "./src/crypto/ciphers-browser.js",
|
||||||
|
"./src/crypto/rsa.js": "./src/crypto/rsa-browser.js"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "aegir-lint",
|
"lint": "aegir-lint",
|
||||||
"build": "aegir-build",
|
"build": "aegir-build",
|
||||||
"test": "aegir-test",
|
"test": "npm run test:node && npm run test:no-webcrypto && npm run test:browser",
|
||||||
"test:node": "aegir-test --env node",
|
"test:node": "aegir-test --env node",
|
||||||
|
"test:no-webcrypto": "NO_WEBCRYPTO=true aegir-test --env node",
|
||||||
"test:browser": "aegir-test --env browser",
|
"test:browser": "aegir-test --env browser",
|
||||||
"release": "aegir-release",
|
"release": "aegir-release",
|
||||||
"release-minor": "aegir-release --type minor",
|
"release-minor": "aegir-release --type minor",
|
||||||
@@ -25,35 +33,58 @@
|
|||||||
"author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>",
|
"author": "Friedel Ziegelmayer <dignifiedqurie@gmail.com>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"elliptic": "^6.2.3",
|
"multihashing-async": "~0.4.2",
|
||||||
"multihashing": "^0.2.1",
|
"asn1.js": "^4.9.1",
|
||||||
"node-forge": "^0.6.39",
|
"async": "^2.1.4",
|
||||||
"protocol-buffers": "^3.1.6"
|
"browserify-aes": "^1.0.6",
|
||||||
|
"keypair": "^1.0.1",
|
||||||
|
"nodeify": "^1.0.0",
|
||||||
|
"pem-jwk": "^1.5.1",
|
||||||
|
"protocol-buffers": "^3.2.1",
|
||||||
|
"rsa-pem-to-jwk": "^1.1.3",
|
||||||
|
"safe-buffer": "^5.0.1",
|
||||||
|
"tweetnacl": "^0.14.5",
|
||||||
|
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^8.0.0",
|
"aegir": "^10.0.0",
|
||||||
|
"benchmark": "^2.1.3",
|
||||||
|
"browserify-optional": "^1.0.0",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"pre-commit": "^1.1.3"
|
"pre-commit": "^1.2.2"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"node-webcrypto-ossl": "^1.0.17"
|
||||||
},
|
},
|
||||||
"pre-commit": [
|
"pre-commit": [
|
||||||
"lint",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^4.0.0"
|
"node": ">=4.0.0",
|
||||||
|
"npm": ">=3.0.0"
|
||||||
|
},
|
||||||
|
"browserify": {
|
||||||
|
"transform": [
|
||||||
|
"browserify-optional"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/ipfs/js-libp2p-crypto.git"
|
"url": "https://github.com/libp2p/js-libp2p-crypto.git"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/ipfs/js-libp2p-crypto/issues"
|
"url": "https://github.com/libp2p/js-libp2p-crypto/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/ipfs/js-libp2p-crypto",
|
"homepage": "https://github.com/libp2p/js-libp2p-crypto",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
|
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"greenkeeperio-bot <support@greenkeeper.io>"
|
"Tom Swindell <t.swindell@rubyx.co.uk>",
|
||||||
|
"Yusef Napora <yusef@napora.org>",
|
||||||
|
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||||
|
"nikuda <nikuda@gmail.com>"
|
||||||
]
|
]
|
||||||
}
|
}
|
8
src/crypto.js
Normal file
8
src/crypto.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
exports.webcrypto = require('./crypto/webcrypto')()
|
||||||
|
exports.hmac = require('./crypto/hmac')
|
||||||
|
exports.ecdh = require('./crypto/ecdh')
|
||||||
|
exports.aes = require('./crypto/aes')
|
||||||
|
exports.rsa = require('./crypto/rsa')
|
||||||
|
exports.ed25519 = require('./crypto/ed25519')
|
@@ -1,13 +0,0 @@
|
|||||||
enum KeyType {
|
|
||||||
RSA = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
message PublicKey {
|
|
||||||
required KeyType Type = 1;
|
|
||||||
required bytes Data = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
message PrivateKey {
|
|
||||||
required KeyType Type = 1;
|
|
||||||
required bytes Data = 2;
|
|
||||||
}
|
|
17
src/crypto.proto.js
Normal file
17
src/crypto.proto.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = `enum KeyType {
|
||||||
|
RSA = 0;
|
||||||
|
Ed25519 = 1;
|
||||||
|
Secp256k1 = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PublicKey {
|
||||||
|
required KeyType Type = 1;
|
||||||
|
required bytes Data = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PrivateKey {
|
||||||
|
required KeyType Type = 1;
|
||||||
|
required bytes Data = 2;
|
||||||
|
}`
|
30
src/crypto/aes.js
Normal file
30
src/crypto/aes.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const ciphers = require('./ciphers')
|
||||||
|
|
||||||
|
const CIPHER_MODES = {
|
||||||
|
16: 'aes-128-ctr',
|
||||||
|
32: 'aes-256-ctr'
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.create = function (key, iv, callback) {
|
||||||
|
const mode = CIPHER_MODES[key.length]
|
||||||
|
if (!mode) {
|
||||||
|
return callback(new Error('Invalid key length'))
|
||||||
|
}
|
||||||
|
|
||||||
|
const cipher = ciphers.createCipheriv(mode, key, iv)
|
||||||
|
const decipher = ciphers.createDecipheriv(mode, key, iv)
|
||||||
|
|
||||||
|
const res = {
|
||||||
|
encrypt (data, cb) {
|
||||||
|
cb(null, cipher.update(data))
|
||||||
|
},
|
||||||
|
|
||||||
|
decrypt (data, cb) {
|
||||||
|
cb(null, decipher.update(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, res)
|
||||||
|
}
|
8
src/crypto/ciphers-browser.js
Normal file
8
src/crypto/ciphers-browser.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const crypto = require('browserify-aes')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createCipheriv: crypto.createCipheriv,
|
||||||
|
createDecipheriv: crypto.createDecipheriv
|
||||||
|
}
|
8
src/crypto/ciphers.js
Normal file
8
src/crypto/ciphers.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const crypto = require('crypto')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createCipheriv: crypto.createCipheriv,
|
||||||
|
createDecipheriv: crypto.createDecipheriv
|
||||||
|
}
|
130
src/crypto/ecdh-browser.js
Normal file
130
src/crypto/ecdh-browser.js
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const crypto = require('./webcrypto')()
|
||||||
|
const nodeify = require('nodeify')
|
||||||
|
const BN = require('asn1.js').bignum
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
const util = require('./util')
|
||||||
|
const toBase64 = util.toBase64
|
||||||
|
const toBn = util.toBn
|
||||||
|
|
||||||
|
const bits = {
|
||||||
|
'P-256': 256,
|
||||||
|
'P-384': 384,
|
||||||
|
'P-521': 521
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.generateEphmeralKeyPair = function (curve, callback) {
|
||||||
|
nodeify(crypto.subtle.generateKey(
|
||||||
|
{
|
||||||
|
name: 'ECDH',
|
||||||
|
namedCurve: curve
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
['deriveBits']
|
||||||
|
).then((pair) => {
|
||||||
|
// forcePrivate is used for testing only
|
||||||
|
const genSharedKey = (theirPub, forcePrivate, cb) => {
|
||||||
|
if (typeof forcePrivate === 'function') {
|
||||||
|
cb = forcePrivate
|
||||||
|
forcePrivate = undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
let privateKey
|
||||||
|
|
||||||
|
if (forcePrivate) {
|
||||||
|
privateKey = crypto.subtle.importKey(
|
||||||
|
'jwk',
|
||||||
|
unmarshalPrivateKey(curve, forcePrivate),
|
||||||
|
{
|
||||||
|
name: 'ECDH',
|
||||||
|
namedCurve: curve
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
['deriveBits']
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
privateKey = Promise.resolve(pair.privateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
const keys = Promise.all([
|
||||||
|
crypto.subtle.importKey(
|
||||||
|
'jwk',
|
||||||
|
unmarshalPublicKey(curve, theirPub),
|
||||||
|
{
|
||||||
|
name: 'ECDH',
|
||||||
|
namedCurve: curve
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
[]
|
||||||
|
),
|
||||||
|
privateKey
|
||||||
|
])
|
||||||
|
|
||||||
|
nodeify(keys.then((keys) => crypto.subtle.deriveBits(
|
||||||
|
{
|
||||||
|
name: 'ECDH',
|
||||||
|
namedCurve: curve,
|
||||||
|
public: keys[0]
|
||||||
|
},
|
||||||
|
keys[1],
|
||||||
|
bits[curve]
|
||||||
|
)).then((bits) => Buffer.from(bits)), cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
return crypto.subtle.exportKey(
|
||||||
|
'jwk',
|
||||||
|
pair.publicKey
|
||||||
|
).then((publicKey) => {
|
||||||
|
return {
|
||||||
|
key: marshalPublicKey(publicKey),
|
||||||
|
genSharedKey
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
const curveLengths = {
|
||||||
|
'P-256': 32,
|
||||||
|
'P-384': 48,
|
||||||
|
'P-521': 66
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal converts a jwk encodec ECDH public key into the
|
||||||
|
// form specified in section 4.3.6 of ANSI X9.62. (This is the format
|
||||||
|
// go-ipfs uses)
|
||||||
|
function marshalPublicKey (jwk) {
|
||||||
|
const byteLen = curveLengths[jwk.crv]
|
||||||
|
|
||||||
|
return Buffer.concat([
|
||||||
|
Buffer.from([4]), // uncompressed point
|
||||||
|
toBn(jwk.x).toArrayLike(Buffer, 'be', byteLen),
|
||||||
|
toBn(jwk.y).toArrayLike(Buffer, 'be', byteLen)
|
||||||
|
], 1 + byteLen * 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal converts a point, serialized by Marshal, into an jwk encoded key
|
||||||
|
function unmarshalPublicKey (curve, key) {
|
||||||
|
const byteLen = curveLengths[curve]
|
||||||
|
|
||||||
|
if (!key.slice(0, 1).equals(Buffer.from([4]))) {
|
||||||
|
throw new Error('Invalid key format')
|
||||||
|
}
|
||||||
|
const x = new BN(key.slice(1, byteLen + 1))
|
||||||
|
const y = new BN(key.slice(1 + byteLen))
|
||||||
|
|
||||||
|
return {
|
||||||
|
kty: 'EC',
|
||||||
|
crv: curve,
|
||||||
|
x: toBase64(x),
|
||||||
|
y: toBase64(y),
|
||||||
|
ext: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unmarshalPrivateKey (curve, key) {
|
||||||
|
const result = unmarshalPublicKey(curve, key.public)
|
||||||
|
result.d = toBase64(new BN(key.private))
|
||||||
|
return result
|
||||||
|
}
|
41
src/crypto/ecdh.js
Normal file
41
src/crypto/ecdh.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const crypto = require('crypto')
|
||||||
|
const setImmediate = require('async/setImmediate')
|
||||||
|
|
||||||
|
const curves = {
|
||||||
|
'P-256': 'prime256v1',
|
||||||
|
'P-384': 'secp384r1',
|
||||||
|
'P-521': 'secp521r1'
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.generateEphmeralKeyPair = function (curve, callback) {
|
||||||
|
if (!curves[curve]) {
|
||||||
|
return callback(new Error(`Unkown curve: ${curve}`))
|
||||||
|
}
|
||||||
|
const ecdh = crypto.createECDH(curves[curve])
|
||||||
|
ecdh.generateKeys()
|
||||||
|
|
||||||
|
setImmediate(() => callback(null, {
|
||||||
|
key: ecdh.getPublicKey(),
|
||||||
|
genSharedKey (theirPub, forcePrivate, cb) {
|
||||||
|
if (typeof forcePrivate === 'function') {
|
||||||
|
cb = forcePrivate
|
||||||
|
forcePrivate = null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (forcePrivate) {
|
||||||
|
ecdh.setPrivateKey(forcePrivate.private)
|
||||||
|
}
|
||||||
|
|
||||||
|
let secret
|
||||||
|
try {
|
||||||
|
secret = ecdh.computeSecret(theirPub)
|
||||||
|
} catch (err) {
|
||||||
|
return cb(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
setImmediate(() => cb(null, secret))
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
35
src/crypto/ed25519.js
Normal file
35
src/crypto/ed25519.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const nacl = require('tweetnacl')
|
||||||
|
const setImmediate = require('async/setImmediate')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
exports.publicKeyLength = nacl.sign.publicKeyLength
|
||||||
|
exports.privateKeyLength = nacl.sign.secretKeyLength
|
||||||
|
|
||||||
|
exports.generateKey = function (callback) {
|
||||||
|
const done = (err, res) => setImmediate(() => {
|
||||||
|
callback(err, res)
|
||||||
|
})
|
||||||
|
|
||||||
|
let keys
|
||||||
|
try {
|
||||||
|
keys = nacl.sign.keyPair()
|
||||||
|
} catch (err) {
|
||||||
|
done(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
done(null, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.hashAndSign = function (key, msg, callback) {
|
||||||
|
setImmediate(() => {
|
||||||
|
callback(null, Buffer.from(nacl.sign.detached(msg, key)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.hashAndVerify = function (key, sig, msg, callback) {
|
||||||
|
setImmediate(() => {
|
||||||
|
callback(null, nacl.sign.detached.verify(msg, sig, key))
|
||||||
|
})
|
||||||
|
}
|
39
src/crypto/hmac-browser.js
Normal file
39
src/crypto/hmac-browser.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const nodeify = require('nodeify')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
const crypto = require('./webcrypto')()
|
||||||
|
const lengths = require('./hmac-lengths')
|
||||||
|
|
||||||
|
const hashTypes = {
|
||||||
|
SHA1: 'SHA-1',
|
||||||
|
SHA256: 'SHA-256',
|
||||||
|
SHA512: 'SHA-512'
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.create = function (hashType, secret, callback) {
|
||||||
|
const hash = hashTypes[hashType]
|
||||||
|
|
||||||
|
nodeify(crypto.subtle.importKey(
|
||||||
|
'raw',
|
||||||
|
secret,
|
||||||
|
{
|
||||||
|
name: 'HMAC',
|
||||||
|
hash: {name: hash}
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
['sign']
|
||||||
|
).then((key) => {
|
||||||
|
return {
|
||||||
|
digest (data, cb) {
|
||||||
|
nodeify(crypto.subtle.sign(
|
||||||
|
{name: 'HMAC'},
|
||||||
|
key,
|
||||||
|
data
|
||||||
|
).then((raw) => Buffer.from(raw)), cb)
|
||||||
|
},
|
||||||
|
length: lengths[hashType]
|
||||||
|
}
|
||||||
|
}), callback)
|
||||||
|
}
|
7
src/crypto/hmac-lengths.js
Normal file
7
src/crypto/hmac-lengths.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
SHA1: 20,
|
||||||
|
SHA256: 32,
|
||||||
|
SHA512: 64
|
||||||
|
}
|
24
src/crypto/hmac.js
Normal file
24
src/crypto/hmac.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const crypto = require('crypto')
|
||||||
|
|
||||||
|
const lengths = require('./hmac-lengths')
|
||||||
|
|
||||||
|
exports.create = function (hash, secret, callback) {
|
||||||
|
const res = {
|
||||||
|
digest (data, cb) {
|
||||||
|
const hmac = genFresh()
|
||||||
|
hmac.update(data)
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
cb(null, hmac.digest())
|
||||||
|
})
|
||||||
|
},
|
||||||
|
length: lengths[hash]
|
||||||
|
}
|
||||||
|
|
||||||
|
function genFresh () {
|
||||||
|
return crypto.createHmac(hash.toLowerCase(), secret)
|
||||||
|
}
|
||||||
|
callback(null, res)
|
||||||
|
}
|
120
src/crypto/rsa-browser.js
Normal file
120
src/crypto/rsa-browser.js
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const nodeify = require('nodeify')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
const crypto = require('./webcrypto')()
|
||||||
|
|
||||||
|
exports.utils = require('./rsa-utils')
|
||||||
|
|
||||||
|
exports.generateKey = function (bits, callback) {
|
||||||
|
nodeify(crypto.subtle.generateKey(
|
||||||
|
{
|
||||||
|
name: 'RSASSA-PKCS1-v1_5',
|
||||||
|
modulusLength: bits,
|
||||||
|
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
|
||||||
|
hash: {name: 'SHA-256'}
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
['sign', 'verify']
|
||||||
|
)
|
||||||
|
.then(exportKey)
|
||||||
|
.then((keys) => ({
|
||||||
|
privateKey: keys[0],
|
||||||
|
publicKey: keys[1]
|
||||||
|
})), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes a jwk key
|
||||||
|
exports.unmarshalPrivateKey = function (key, callback) {
|
||||||
|
const privateKey = crypto.subtle.importKey(
|
||||||
|
'jwk',
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
name: 'RSASSA-PKCS1-v1_5',
|
||||||
|
hash: {name: 'SHA-256'}
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
['sign']
|
||||||
|
)
|
||||||
|
|
||||||
|
nodeify(Promise.all([
|
||||||
|
privateKey,
|
||||||
|
derivePublicFromPrivate(key)
|
||||||
|
]).then((keys) => exportKey({
|
||||||
|
privateKey: keys[0],
|
||||||
|
publicKey: keys[1]
|
||||||
|
})).then((keys) => ({
|
||||||
|
privateKey: keys[0],
|
||||||
|
publicKey: keys[1]
|
||||||
|
})), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.getRandomValues = function (arr) {
|
||||||
|
return Buffer.from(crypto.getRandomValues(arr))
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.hashAndSign = function (key, msg, callback) {
|
||||||
|
nodeify(crypto.subtle.importKey(
|
||||||
|
'jwk',
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
name: 'RSASSA-PKCS1-v1_5',
|
||||||
|
hash: {name: 'SHA-256'}
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
['sign']
|
||||||
|
).then((privateKey) => {
|
||||||
|
return crypto.subtle.sign(
|
||||||
|
{name: 'RSASSA-PKCS1-v1_5'},
|
||||||
|
privateKey,
|
||||||
|
Uint8Array.from(msg)
|
||||||
|
)
|
||||||
|
}).then((sig) => Buffer.from(sig)), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.hashAndVerify = function (key, sig, msg, callback) {
|
||||||
|
nodeify(crypto.subtle.importKey(
|
||||||
|
'jwk',
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
name: 'RSASSA-PKCS1-v1_5',
|
||||||
|
hash: {name: 'SHA-256'}
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
['verify']
|
||||||
|
).then((publicKey) => {
|
||||||
|
return crypto.subtle.verify(
|
||||||
|
{name: 'RSASSA-PKCS1-v1_5'},
|
||||||
|
publicKey,
|
||||||
|
sig,
|
||||||
|
msg
|
||||||
|
)
|
||||||
|
}), callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
function exportKey (pair) {
|
||||||
|
return Promise.all([
|
||||||
|
crypto.subtle.exportKey('jwk', pair.privateKey),
|
||||||
|
crypto.subtle.exportKey('jwk', pair.publicKey)
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
function derivePublicFromPrivate (jwKey) {
|
||||||
|
return crypto.subtle.importKey(
|
||||||
|
'jwk',
|
||||||
|
{
|
||||||
|
kty: jwKey.kty,
|
||||||
|
n: jwKey.n,
|
||||||
|
e: jwKey.e,
|
||||||
|
alg: jwKey.alg,
|
||||||
|
kid: jwKey.kid
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'RSASSA-PKCS1-v1_5',
|
||||||
|
hash: {name: 'SHA-256'}
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
['verify']
|
||||||
|
)
|
||||||
|
}
|
114
src/crypto/rsa-utils.js
Normal file
114
src/crypto/rsa-utils.js
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const asn1 = require('asn1.js')
|
||||||
|
|
||||||
|
const util = require('./util')
|
||||||
|
const toBase64 = util.toBase64
|
||||||
|
const toBn = util.toBn
|
||||||
|
|
||||||
|
const RSAPrivateKey = asn1.define('RSAPrivateKey', function () {
|
||||||
|
this.seq().obj(
|
||||||
|
this.key('version').int(),
|
||||||
|
this.key('modulus').int(),
|
||||||
|
this.key('publicExponent').int(),
|
||||||
|
this.key('privateExponent').int(),
|
||||||
|
this.key('prime1').int(),
|
||||||
|
this.key('prime2').int(),
|
||||||
|
this.key('exponent1').int(),
|
||||||
|
this.key('exponent2').int(),
|
||||||
|
this.key('coefficient').int()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const AlgorithmIdentifier = asn1.define('AlgorithmIdentifier', function () {
|
||||||
|
this.seq().obj(
|
||||||
|
this.key('algorithm').objid({
|
||||||
|
'1.2.840.113549.1.1.1': 'rsa'
|
||||||
|
}),
|
||||||
|
this.key('none').optional().null_(),
|
||||||
|
this.key('curve').optional().objid(),
|
||||||
|
this.key('params').optional().seq().obj(
|
||||||
|
this.key('p').int(),
|
||||||
|
this.key('q').int(),
|
||||||
|
this.key('g').int()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const PublicKey = asn1.define('RSAPublicKey', function () {
|
||||||
|
this.seq().obj(
|
||||||
|
this.key('algorithm').use(AlgorithmIdentifier),
|
||||||
|
this.key('subjectPublicKey').bitstr()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
const RSAPublicKey = asn1.define('RSAPublicKey', function () {
|
||||||
|
this.seq().obj(
|
||||||
|
this.key('modulus').int(),
|
||||||
|
this.key('publicExponent').int()
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Convert a PKCS#1 in ASN1 DER format to a JWK key
|
||||||
|
exports.pkcs1ToJwk = function (bytes) {
|
||||||
|
const asn1 = RSAPrivateKey.decode(bytes, 'der')
|
||||||
|
|
||||||
|
return {
|
||||||
|
kty: 'RSA',
|
||||||
|
n: toBase64(asn1.modulus),
|
||||||
|
e: toBase64(asn1.publicExponent),
|
||||||
|
d: toBase64(asn1.privateExponent),
|
||||||
|
p: toBase64(asn1.prime1),
|
||||||
|
q: toBase64(asn1.prime2),
|
||||||
|
dp: toBase64(asn1.exponent1),
|
||||||
|
dq: toBase64(asn1.exponent2),
|
||||||
|
qi: toBase64(asn1.coefficient),
|
||||||
|
alg: 'RS256',
|
||||||
|
kid: '2011-04-29'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert a JWK key into PKCS#1 in ASN1 DER format
|
||||||
|
exports.jwkToPkcs1 = function (jwk) {
|
||||||
|
return RSAPrivateKey.encode({
|
||||||
|
version: 0,
|
||||||
|
modulus: toBn(jwk.n),
|
||||||
|
publicExponent: toBn(jwk.e),
|
||||||
|
privateExponent: toBn(jwk.d),
|
||||||
|
prime1: toBn(jwk.p),
|
||||||
|
prime2: toBn(jwk.q),
|
||||||
|
exponent1: toBn(jwk.dp),
|
||||||
|
exponent2: toBn(jwk.dq),
|
||||||
|
coefficient: toBn(jwk.qi)
|
||||||
|
}, 'der')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert a PKCIX in ASN1 DER format to a JWK key
|
||||||
|
exports.pkixToJwk = function (bytes) {
|
||||||
|
const ndata = PublicKey.decode(bytes, 'der')
|
||||||
|
const asn1 = RSAPublicKey.decode(ndata.subjectPublicKey.data, 'der')
|
||||||
|
|
||||||
|
return {
|
||||||
|
kty: 'RSA',
|
||||||
|
n: toBase64(asn1.modulus),
|
||||||
|
e: toBase64(asn1.publicExponent),
|
||||||
|
alg: 'RS256',
|
||||||
|
kid: '2011-04-29'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert a JWK key to PKCIX in ASN1 DER format
|
||||||
|
exports.jwkToPkix = function (jwk) {
|
||||||
|
return PublicKey.encode({
|
||||||
|
algorithm: {
|
||||||
|
algorithm: 'rsa',
|
||||||
|
none: null
|
||||||
|
},
|
||||||
|
subjectPublicKey: {
|
||||||
|
data: RSAPublicKey.encode({
|
||||||
|
modulus: toBn(jwk.n),
|
||||||
|
publicExponent: toBn(jwk.e)
|
||||||
|
}, 'der')
|
||||||
|
}
|
||||||
|
}, 'der')
|
||||||
|
}
|
80
src/crypto/rsa.js
Normal file
80
src/crypto/rsa.js
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
// Node.js land
|
||||||
|
// First we look if node-webrypto-ossl is available
|
||||||
|
// otherwise we fall back to using keypair + node core
|
||||||
|
|
||||||
|
let webcrypto
|
||||||
|
try {
|
||||||
|
webcrypto = require('node-webcrypto-ossl')
|
||||||
|
} catch (err) {
|
||||||
|
// not available, use the code below
|
||||||
|
}
|
||||||
|
|
||||||
|
if (webcrypto && !process.env.NO_WEBCRYPTO) {
|
||||||
|
module.exports = require('./rsa-browser')
|
||||||
|
} else {
|
||||||
|
const crypto = require('crypto')
|
||||||
|
const keypair = require('keypair')
|
||||||
|
const setImmediate = require('async/setImmediate')
|
||||||
|
const pemToJwk = require('pem-jwk').pem2jwk
|
||||||
|
const jwkToPem = require('pem-jwk').jwk2pem
|
||||||
|
|
||||||
|
exports.utils = require('./rsa-utils')
|
||||||
|
|
||||||
|
exports.generateKey = function (bits, callback) {
|
||||||
|
const done = (err, res) => setImmediate(() => {
|
||||||
|
callback(err, res)
|
||||||
|
})
|
||||||
|
|
||||||
|
let key
|
||||||
|
try {
|
||||||
|
key = keypair({
|
||||||
|
bits: bits
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
done(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
done(null, {
|
||||||
|
privateKey: pemToJwk(key.private),
|
||||||
|
publicKey: pemToJwk(key.public)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes a jwk key
|
||||||
|
exports.unmarshalPrivateKey = function (key, callback) {
|
||||||
|
callback(null, {
|
||||||
|
privateKey: key,
|
||||||
|
publicKey: {
|
||||||
|
kty: key.kty,
|
||||||
|
n: key.n,
|
||||||
|
e: key.e
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.getRandomValues = function (arr) {
|
||||||
|
return crypto.randomBytes(arr.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.hashAndSign = function (key, msg, callback) {
|
||||||
|
const sign = crypto.createSign('RSA-SHA256')
|
||||||
|
|
||||||
|
sign.update(msg)
|
||||||
|
setImmediate(() => {
|
||||||
|
callback(null, sign.sign(jwkToPem(key)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.hashAndVerify = function (key, sig, msg, callback) {
|
||||||
|
const verify = crypto.createVerify('RSA-SHA256')
|
||||||
|
|
||||||
|
verify.update(msg)
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
callback(null, verify.verify(jwkToPem(key), sig))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
20
src/crypto/util.js
Normal file
20
src/crypto/util.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const BN = require('asn1.js').bignum
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
// Convert a BN.js instance to a base64 encoded string without padding
|
||||||
|
// Adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C
|
||||||
|
exports.toBase64 = function toBase64 (bn) {
|
||||||
|
let s = bn.toArrayLike(Buffer, 'be').toString('base64')
|
||||||
|
|
||||||
|
return s
|
||||||
|
.replace(/(=*)$/, '') // Remove any trailing '='s
|
||||||
|
.replace(/\+/g, '-') // 62nd char of encoding
|
||||||
|
.replace(/\//g, '_') // 63rd char of encoding
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert a base64 encoded string to a BN.js instance
|
||||||
|
exports.toBn = function toBn (str) {
|
||||||
|
return new BN(Buffer.from(str, 'base64'))
|
||||||
|
}
|
24
src/crypto/webcrypto-browser.js
Normal file
24
src/crypto/webcrypto-browser.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/* global self */
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = function getWebCrypto () {
|
||||||
|
if (typeof self !== 'undefined') {
|
||||||
|
// This is only a shim for interfaces, not for functionality
|
||||||
|
require('webcrypto-shim')(self)
|
||||||
|
|
||||||
|
if (self.crypto) {
|
||||||
|
return self.crypto
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof self !== 'undefined') {
|
||||||
|
require('webcrypto-shim')(self)
|
||||||
|
|
||||||
|
if (self.crypto) {
|
||||||
|
return self.crypto
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Please use an environment with crypto support')
|
||||||
|
}
|
11
src/crypto/webcrypto.js
Normal file
11
src/crypto/webcrypto.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = function getWebCrypto () {
|
||||||
|
try {
|
||||||
|
const WebCrypto = require('node-webcrypto-ossl')
|
||||||
|
const webCrypto = new WebCrypto()
|
||||||
|
return webCrypto
|
||||||
|
} catch (err) {
|
||||||
|
// fallback to other things
|
||||||
|
}
|
||||||
|
}
|
@@ -1,36 +1,11 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const EC = require('elliptic').ec
|
const crypto = require('./crypto')
|
||||||
|
|
||||||
const curveMap = {
|
|
||||||
'P-256': 'p256',
|
|
||||||
'P-384': 'p384',
|
|
||||||
'P-521': 'p521'
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generates an ephemeral public key and returns a function that will compute
|
// Generates an ephemeral public key and returns a function that will compute
|
||||||
// the shared secret key.
|
// the shared secret key.
|
||||||
//
|
//
|
||||||
// Focuses only on ECDH now, but can be made more general in the future.
|
// Focuses only on ECDH now, but can be made more general in the future.
|
||||||
module.exports = (curveName) => {
|
module.exports = (curve, callback) => {
|
||||||
const curve = curveMap[curveName]
|
crypto.ecdh.generateEphmeralKeyPair(curve, callback)
|
||||||
if (!curve) {
|
|
||||||
throw new Error('unsupported curve passed')
|
|
||||||
}
|
|
||||||
|
|
||||||
const ec = new EC(curve)
|
|
||||||
|
|
||||||
const priv = ec.genKeyPair()
|
|
||||||
|
|
||||||
// forcePrivate is used for testing only
|
|
||||||
const genSharedKey = (theirPub, forcePrivate) => {
|
|
||||||
const pub = ec.keyFromPublic(theirPub, 'hex')
|
|
||||||
const p = forcePrivate || priv
|
|
||||||
return p.derive(pub.getPublic()).toBuffer('be')
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
key: new Buffer(priv.getPublic('hex'), 'hex'),
|
|
||||||
genSharedKey
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
71
src/index.js
71
src/index.js
@@ -1,24 +1,33 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const protobuf = require('protocol-buffers')
|
const protobuf = require('protocol-buffers')
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
|
||||||
const pbm = protobuf(fs.readFileSync(path.join(__dirname, './crypto.proto')))
|
|
||||||
|
|
||||||
exports.utils = require('./utils')
|
const pbm = protobuf(require('./crypto.proto'))
|
||||||
|
const c = require('./crypto')
|
||||||
|
|
||||||
|
exports.protobuf = pbm
|
||||||
|
|
||||||
|
exports.hmac = c.hmac
|
||||||
|
exports.aes = c.aes
|
||||||
|
exports.webcrypto = c.webcrypto
|
||||||
|
|
||||||
const keys = exports.keys = require('./keys')
|
const keys = exports.keys = require('./keys')
|
||||||
|
function isValidKeyType (keyType) {
|
||||||
|
const key = keys[keyType.toLowerCase()]
|
||||||
|
return key !== undefined
|
||||||
|
}
|
||||||
|
|
||||||
exports.keyStretcher = require('./key-stretcher')
|
exports.keyStretcher = require('./key-stretcher')
|
||||||
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
|
exports.generateEphemeralKeyPair = require('./ephemeral-keys')
|
||||||
|
|
||||||
// Generates a keypair of the given type and bitsize
|
// Generates a keypair of the given type and bitsize
|
||||||
exports.generateKeyPair = (type, bits) => {
|
exports.generateKeyPair = (type, bits, cb) => {
|
||||||
let key = keys[type.toLowerCase()]
|
let key = keys[type.toLowerCase()]
|
||||||
if (!key) {
|
if (!key) {
|
||||||
throw new Error('invalid or unsupported key type')
|
return cb(new Error('invalid or unsupported key type'))
|
||||||
}
|
}
|
||||||
|
|
||||||
return key.generateKeyPair(bits)
|
key.generateKeyPair(bits, cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts a protobuf serialized public key into its
|
// Converts a protobuf serialized public key into its
|
||||||
@@ -29,6 +38,14 @@ exports.unmarshalPublicKey = (buf) => {
|
|||||||
switch (decoded.Type) {
|
switch (decoded.Type) {
|
||||||
case pbm.KeyType.RSA:
|
case pbm.KeyType.RSA:
|
||||||
return keys.rsa.unmarshalRsaPublicKey(decoded.Data)
|
return keys.rsa.unmarshalRsaPublicKey(decoded.Data)
|
||||||
|
case pbm.KeyType.Ed25519:
|
||||||
|
return keys.ed25519.unmarshalEd25519PublicKey(decoded.Data)
|
||||||
|
case pbm.KeyType.Secp256k1:
|
||||||
|
if (keys.secp256k1) {
|
||||||
|
return keys.secp256k1.unmarshalSecp256k1PublicKey(decoded.Data)
|
||||||
|
} else {
|
||||||
|
throw new Error('secp256k1 support requires libp2p-crypto-secp256k1 package')
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid or unsupported key type')
|
throw new Error('invalid or unsupported key type')
|
||||||
}
|
}
|
||||||
@@ -37,42 +54,48 @@ exports.unmarshalPublicKey = (buf) => {
|
|||||||
// Converts a public key object into a protobuf serialized public key
|
// Converts a public key object into a protobuf serialized public key
|
||||||
exports.marshalPublicKey = (key, type) => {
|
exports.marshalPublicKey = (key, type) => {
|
||||||
type = (type || 'rsa').toLowerCase()
|
type = (type || 'rsa').toLowerCase()
|
||||||
|
if (!isValidKeyType(type)) {
|
||||||
// for now only rsa is supported
|
|
||||||
if (type !== 'rsa') {
|
|
||||||
throw new Error('invalid or unsupported key type')
|
throw new Error('invalid or unsupported key type')
|
||||||
}
|
}
|
||||||
|
|
||||||
return pbm.PublicKey.encode({
|
return key.bytes
|
||||||
Type: pbm.KeyType.RSA,
|
|
||||||
Data: key.marshal()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts a protobuf serialized private key into its
|
// Converts a protobuf serialized private key into its
|
||||||
// representative object
|
// representative object
|
||||||
exports.unmarshalPrivateKey = (buf) => {
|
exports.unmarshalPrivateKey = (buf, callback) => {
|
||||||
const decoded = pbm.PrivateKey.decode(buf)
|
const decoded = pbm.PrivateKey.decode(buf)
|
||||||
|
|
||||||
switch (decoded.Type) {
|
switch (decoded.Type) {
|
||||||
case pbm.KeyType.RSA:
|
case pbm.KeyType.RSA:
|
||||||
return keys.rsa.unmarshalRsaPrivateKey(decoded.Data)
|
return keys.rsa.unmarshalRsaPrivateKey(decoded.Data, callback)
|
||||||
|
case pbm.KeyType.Ed25519:
|
||||||
|
return keys.ed25519.unmarshalEd25519PrivateKey(decoded.Data, callback)
|
||||||
|
case pbm.KeyType.Secp256k1:
|
||||||
|
if (keys.secp256k1) {
|
||||||
|
return keys.secp256k1.unmarshalSecp256k1PrivateKey(decoded.Data, callback)
|
||||||
|
} else {
|
||||||
|
return callback(new Error('secp256k1 support requires libp2p-crypto-secp256k1 package'))
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid or unsupported key type')
|
callback(new Error('invalid or unsupported key type'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts a private key object into a protobuf serialized private key
|
// Converts a private key object into a protobuf serialized private key
|
||||||
exports.marshalPrivateKey = (key, type) => {
|
exports.marshalPrivateKey = (key, type) => {
|
||||||
type = (type || 'rsa').toLowerCase()
|
type = (type || 'rsa').toLowerCase()
|
||||||
|
if (!isValidKeyType(type)) {
|
||||||
// for now only rsa is supported
|
|
||||||
if (type !== 'rsa') {
|
|
||||||
throw new Error('invalid or unsupported key type')
|
throw new Error('invalid or unsupported key type')
|
||||||
}
|
}
|
||||||
|
|
||||||
return pbm.PrivateKey.encode({
|
return key.bytes
|
||||||
Type: pbm.KeyType.RSA,
|
}
|
||||||
Data: key.marshal()
|
|
||||||
})
|
exports.randomBytes = (number) => {
|
||||||
|
if (!number || typeof number !== 'number') {
|
||||||
|
throw new Error('first argument must be a Number bigger than 0')
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.rsa.getRandomValues(new Uint8Array(number))
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,8 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const forge = require('node-forge')
|
const crypto = require('./crypto')
|
||||||
const createBuffer = forge.util.createBuffer
|
const whilst = require('async/whilst')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
const cipherMap = {
|
const cipherMap = {
|
||||||
'AES-128': {
|
'AES-128': {
|
||||||
@@ -18,78 +19,91 @@ const cipherMap = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const hashMap = {
|
|
||||||
SHA1: 'sha1',
|
|
||||||
SHA256: 'sha256',
|
|
||||||
// workaround for https://github.com/digitalbazaar/forge/issues/401
|
|
||||||
SHA512: forge.md.sha512.create()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generates a set of keys for each party by stretching the shared key.
|
// Generates a set of keys for each party by stretching the shared key.
|
||||||
// (myIV, theirIV, myCipherKey, theirCipherKey, myMACKey, theirMACKey)
|
// (myIV, theirIV, myCipherKey, theirCipherKey, myMACKey, theirMACKey)
|
||||||
module.exports = (cipherType, hashType, secret) => {
|
module.exports = (cipherType, hash, secret, callback) => {
|
||||||
const cipher = cipherMap[cipherType]
|
const cipher = cipherMap[cipherType]
|
||||||
const hash = hashMap[hashType]
|
|
||||||
|
|
||||||
if (!cipher) {
|
if (!cipher) {
|
||||||
throw new Error('unkown cipherType passed')
|
return callback(new Error('unkown cipherType passed'))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
throw new Error('unkown hashType passed')
|
return callback(new Error('unkown hashType passed'))
|
||||||
}
|
|
||||||
|
|
||||||
if (Buffer.isBuffer(secret)) {
|
|
||||||
secret = createBuffer(secret.toString('binary'))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cipherKeySize = cipher.keySize
|
const cipherKeySize = cipher.keySize
|
||||||
const ivSize = cipher.ivSize
|
const ivSize = cipher.ivSize
|
||||||
const hmacKeySize = 20
|
const hmacKeySize = 20
|
||||||
const seed = 'key expansion'
|
const seed = Buffer.from('key expansion')
|
||||||
const resultLength = 2 * (ivSize + cipherKeySize + hmacKeySize)
|
const resultLength = 2 * (ivSize + cipherKeySize + hmacKeySize)
|
||||||
|
|
||||||
const m = forge.hmac.create()
|
crypto.hmac.create(hash, secret, (err, m) => {
|
||||||
m.start(hash, secret)
|
if (err) {
|
||||||
m.update(seed)
|
return callback(err)
|
||||||
|
|
||||||
let a = m.digest().bytes()
|
|
||||||
const result = createBuffer()
|
|
||||||
|
|
||||||
let j = 0
|
|
||||||
for (; j < resultLength;) {
|
|
||||||
m.start(hash, secret)
|
|
||||||
m.update(a)
|
|
||||||
m.update(seed)
|
|
||||||
|
|
||||||
const b = createBuffer(m.digest(), 'raw')
|
|
||||||
let todo = b.length()
|
|
||||||
|
|
||||||
if (j + todo > resultLength) {
|
|
||||||
todo = resultLength - j
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result.putBytes(b.getBytes(todo))
|
m.digest(seed, (err, a) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err)
|
||||||
|
}
|
||||||
|
|
||||||
j += todo
|
let result = []
|
||||||
|
let j = 0
|
||||||
|
|
||||||
m.start(hash, secret)
|
whilst(
|
||||||
m.update(a)
|
() => j < resultLength,
|
||||||
a = m.digest().bytes()
|
stretch,
|
||||||
}
|
finish
|
||||||
|
)
|
||||||
|
|
||||||
const half = resultLength / 2
|
function stretch (cb) {
|
||||||
const r1 = createBuffer(result.getBytes(half))
|
m.digest(Buffer.concat([a, seed]), (err, b) => {
|
||||||
const r2 = createBuffer(result.getBytes())
|
if (err) {
|
||||||
|
return cb(err)
|
||||||
|
}
|
||||||
|
|
||||||
const createKey = (res) => ({
|
let todo = b.length
|
||||||
iv: new Buffer(res.getBytes(ivSize), 'binary'),
|
|
||||||
cipherKey: new Buffer(res.getBytes(cipherKeySize), 'binary'),
|
if (j + todo > resultLength) {
|
||||||
macKey: new Buffer(res.getBytes(), 'binary')
|
todo = resultLength - j
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push(b)
|
||||||
|
|
||||||
|
j += todo
|
||||||
|
|
||||||
|
m.digest(a, (err, _a) => {
|
||||||
|
if (err) {
|
||||||
|
return cb(err)
|
||||||
|
}
|
||||||
|
a = _a
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function finish (err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const half = resultLength / 2
|
||||||
|
const resultBuffer = Buffer.concat(result)
|
||||||
|
const r1 = resultBuffer.slice(0, half)
|
||||||
|
const r2 = resultBuffer.slice(half, resultLength)
|
||||||
|
|
||||||
|
const createKey = (res) => ({
|
||||||
|
iv: res.slice(0, ivSize),
|
||||||
|
cipherKey: res.slice(ivSize, ivSize + cipherKeySize),
|
||||||
|
macKey: res.slice(ivSize + cipherKeySize)
|
||||||
|
})
|
||||||
|
|
||||||
|
callback(null, {
|
||||||
|
k1: createKey(r1),
|
||||||
|
k2: createKey(r2)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
|
||||||
k1: createKey(r1),
|
|
||||||
k2: createKey(r2)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
142
src/keys/ed25519.js
Normal file
142
src/keys/ed25519.js
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const multihashing = require('multihashing-async')
|
||||||
|
const protobuf = require('protocol-buffers')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
const crypto = require('../crypto').ed25519
|
||||||
|
const pbm = protobuf(require('../crypto.proto'))
|
||||||
|
|
||||||
|
class Ed25519PublicKey {
|
||||||
|
constructor (key) {
|
||||||
|
this._key = ensureKey(key, crypto.publicKeyLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
verify (data, sig, callback) {
|
||||||
|
ensure(callback)
|
||||||
|
crypto.hashAndVerify(this._key, sig, data, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
marshal () {
|
||||||
|
return Buffer.from(this._key)
|
||||||
|
}
|
||||||
|
|
||||||
|
get bytes () {
|
||||||
|
return pbm.PublicKey.encode({
|
||||||
|
Type: pbm.KeyType.Ed25519,
|
||||||
|
Data: this.marshal()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
equals (key) {
|
||||||
|
return this.bytes.equals(key.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
hash (callback) {
|
||||||
|
ensure(callback)
|
||||||
|
multihashing(this.bytes, 'sha2-256', callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ed25519PrivateKey {
|
||||||
|
// key - 64 byte Uint8Array or Buffer containing private key
|
||||||
|
// publicKey - 32 byte Uint8Array or Buffer containing public key
|
||||||
|
constructor (key, publicKey) {
|
||||||
|
this._key = ensureKey(key, crypto.privateKeyLength)
|
||||||
|
this._publicKey = ensureKey(publicKey, crypto.publicKeyLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
sign (message, callback) {
|
||||||
|
ensure(callback)
|
||||||
|
crypto.hashAndSign(this._key, message, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
get public () {
|
||||||
|
if (!this._publicKey) {
|
||||||
|
throw new Error('public key not provided')
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Ed25519PublicKey(this._publicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
marshal () {
|
||||||
|
return Buffer.concat([Buffer.from(this._key), Buffer.from(this._publicKey)])
|
||||||
|
}
|
||||||
|
|
||||||
|
get bytes () {
|
||||||
|
return pbm.PrivateKey.encode({
|
||||||
|
Type: pbm.KeyType.Ed25519,
|
||||||
|
Data: this.marshal()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
equals (key) {
|
||||||
|
return this.bytes.equals(key.bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
hash (callback) {
|
||||||
|
ensure(callback)
|
||||||
|
multihashing(this.bytes, 'sha2-256', callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unmarshalEd25519PrivateKey (bytes, callback) {
|
||||||
|
try {
|
||||||
|
bytes = ensureKey(bytes, crypto.privateKeyLength + crypto.publicKeyLength)
|
||||||
|
} catch (err) {
|
||||||
|
return callback(err)
|
||||||
|
}
|
||||||
|
const privateKeyBytes = bytes.slice(0, crypto.privateKeyLength)
|
||||||
|
const publicKeyBytes = bytes.slice(crypto.privateKeyLength, bytes.length)
|
||||||
|
callback(null, new Ed25519PrivateKey(privateKeyBytes, publicKeyBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
function unmarshalEd25519PublicKey (bytes) {
|
||||||
|
bytes = ensureKey(bytes, crypto.publicKeyLength)
|
||||||
|
return new Ed25519PublicKey(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateKeyPair (_bits, cb) {
|
||||||
|
if (cb === undefined && typeof _bits === 'function') {
|
||||||
|
cb = _bits
|
||||||
|
}
|
||||||
|
|
||||||
|
crypto.generateKey((err, keys) => {
|
||||||
|
if (err) {
|
||||||
|
return cb(err)
|
||||||
|
}
|
||||||
|
let privkey
|
||||||
|
try {
|
||||||
|
privkey = new Ed25519PrivateKey(keys.secretKey, keys.publicKey)
|
||||||
|
} catch (err) {
|
||||||
|
cb(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(null, privkey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensure (cb) {
|
||||||
|
if (typeof cb !== 'function') {
|
||||||
|
throw new Error('callback is required')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureKey (key, length) {
|
||||||
|
if (Buffer.isBuffer(key)) {
|
||||||
|
key = new Uint8Array(key)
|
||||||
|
}
|
||||||
|
if (!(key instanceof Uint8Array) || key.length !== length) {
|
||||||
|
throw new Error('Key must be a Uint8Array or Buffer of length ' + length)
|
||||||
|
}
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
Ed25519PublicKey,
|
||||||
|
Ed25519PrivateKey,
|
||||||
|
unmarshalEd25519PrivateKey,
|
||||||
|
unmarshalEd25519PublicKey,
|
||||||
|
generateKeyPair
|
||||||
|
}
|
@@ -1,5 +1,11 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
rsa: require('./rsa')
|
rsa: require('./rsa'),
|
||||||
|
ed25519: require('./ed25519')
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
module.exports.secp256k1 = require('libp2p-crypto-secp256k1')
|
||||||
|
} catch (err) {
|
||||||
}
|
}
|
||||||
|
110
src/keys/rsa.js
110
src/keys/rsa.js
@@ -1,35 +1,23 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const forge = require('node-forge')
|
const multihashing = require('multihashing-async')
|
||||||
const protobuf = require('protocol-buffers')
|
const protobuf = require('protocol-buffers')
|
||||||
const fs = require('fs')
|
|
||||||
const path = require('path')
|
|
||||||
|
|
||||||
const utils = require('../utils')
|
const crypto = require('../crypto').rsa
|
||||||
|
const pbm = protobuf(require('../crypto.proto'))
|
||||||
const pki = forge.pki
|
|
||||||
const rsa = pki.rsa
|
|
||||||
|
|
||||||
const pbm = protobuf(fs.readFileSync(path.join(__dirname, '../crypto.proto')))
|
|
||||||
|
|
||||||
class RsaPublicKey {
|
class RsaPublicKey {
|
||||||
constructor (k) {
|
constructor (key) {
|
||||||
this._key = k
|
this._key = key
|
||||||
}
|
}
|
||||||
|
|
||||||
verify (data, sig) {
|
verify (data, sig, callback) {
|
||||||
const md = forge.md.sha256.create()
|
ensure(callback)
|
||||||
if (Buffer.isBuffer(data)) {
|
crypto.hashAndVerify(this._key, sig, data, callback)
|
||||||
md.update(data.toString('binary'), 'binary')
|
|
||||||
} else {
|
|
||||||
md.update(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
return this._key.verify(md.digest().bytes(), sig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
marshal () {
|
marshal () {
|
||||||
return new Buffer(forge.asn1.toDer(pki.publicKeyToAsn1(this._key)).bytes(), 'binary')
|
return crypto.utils.jwkToPkix(this._key)
|
||||||
}
|
}
|
||||||
|
|
||||||
get bytes () {
|
get bytes () {
|
||||||
@@ -47,34 +35,27 @@ class RsaPublicKey {
|
|||||||
return this.bytes.equals(key.bytes)
|
return this.bytes.equals(key.bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash () {
|
hash (callback) {
|
||||||
return utils.keyHash(this.bytes)
|
ensure(callback)
|
||||||
|
multihashing(this.bytes, 'sha2-256', callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RsaPrivateKey {
|
class RsaPrivateKey {
|
||||||
constructor (privKey, pubKey) {
|
// key - Object of the jwk format
|
||||||
this._privateKey = privKey
|
// publicKey - Buffer of the spki format
|
||||||
if (pubKey) {
|
constructor (key, publicKey) {
|
||||||
this._publicKey = pubKey
|
this._key = key
|
||||||
} else {
|
this._publicKey = publicKey
|
||||||
this._publicKey = forge.pki.setRsaPublicKey(privKey.n, privKey.e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
genSecret () {
|
genSecret () {
|
||||||
return forge.random.getBytesSync(16)
|
return crypto.getRandomValues(new Uint8Array(16))
|
||||||
}
|
}
|
||||||
|
|
||||||
sign (message) {
|
sign (message, callback) {
|
||||||
const md = forge.md.sha256.create()
|
ensure(callback)
|
||||||
if (Buffer.isBuffer(message)) {
|
crypto.hashAndSign(this._key, message, callback)
|
||||||
md.update(message.toString('binary'), 'binary')
|
|
||||||
} else {
|
|
||||||
md.update(message)
|
|
||||||
}
|
|
||||||
const raw = this._privateKey.sign(md, 'RSASSA-PKCS1-V1_5')
|
|
||||||
return new Buffer(raw, 'binary')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get public () {
|
get public () {
|
||||||
@@ -85,12 +66,12 @@ class RsaPrivateKey {
|
|||||||
return new RsaPublicKey(this._publicKey)
|
return new RsaPublicKey(this._publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
decrypt (bytes) {
|
decrypt (msg, callback) {
|
||||||
return this._privateKey.decrypt(bytes, 'RSAES-PKCS1-V1_5')
|
crypto.decrypt(this._key, msg, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
marshal () {
|
marshal () {
|
||||||
return new Buffer(forge.asn1.toDer(pki.privateKeyToAsn1(this._privateKey)).bytes(), 'binary')
|
return crypto.utils.jwkToPkcs1(this._key)
|
||||||
}
|
}
|
||||||
|
|
||||||
get bytes () {
|
get bytes () {
|
||||||
@@ -104,32 +85,43 @@ class RsaPrivateKey {
|
|||||||
return this.bytes.equals(key.bytes)
|
return this.bytes.equals(key.bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash () {
|
hash (callback) {
|
||||||
return utils.keyHash(this.bytes)
|
ensure(callback)
|
||||||
|
multihashing(this.bytes, 'sha2-256', callback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function unmarshalRsaPrivateKey (bytes) {
|
function unmarshalRsaPrivateKey (bytes, callback) {
|
||||||
if (Buffer.isBuffer(bytes)) {
|
const jwk = crypto.utils.pkcs1ToJwk(bytes)
|
||||||
bytes = forge.util.createBuffer(bytes.toString('binary'))
|
crypto.unmarshalPrivateKey(jwk, (err, keys) => {
|
||||||
}
|
if (err) {
|
||||||
const key = pki.privateKeyFromAsn1(forge.asn1.fromDer(bytes))
|
return callback(err)
|
||||||
|
}
|
||||||
|
|
||||||
return new RsaPrivateKey(key)
|
callback(null, new RsaPrivateKey(keys.privateKey, keys.publicKey))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function unmarshalRsaPublicKey (bytes) {
|
function unmarshalRsaPublicKey (bytes) {
|
||||||
if (Buffer.isBuffer(bytes)) {
|
const jwk = crypto.utils.pkixToJwk(bytes)
|
||||||
bytes = forge.util.createBuffer(bytes.toString('binary'))
|
|
||||||
}
|
|
||||||
const key = pki.publicKeyFromAsn1(forge.asn1.fromDer(bytes))
|
|
||||||
|
|
||||||
return new RsaPublicKey(key)
|
return new RsaPublicKey(jwk)
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateKeyPair (bits) {
|
function generateKeyPair (bits, cb) {
|
||||||
const p = rsa.generateKeyPair({bits})
|
crypto.generateKey(bits, (err, keys) => {
|
||||||
return new RsaPrivateKey(p.privateKey, p.publicKey)
|
if (err) {
|
||||||
|
return cb(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(null, new RsaPrivateKey(keys.privateKey, keys.publicKey))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensure (cb) {
|
||||||
|
if (typeof cb !== 'function') {
|
||||||
|
throw new Error('callback is required')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@@ -1,8 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
const multihashing = require('multihashing')
|
|
||||||
|
|
||||||
// Hashes a key
|
|
||||||
exports.keyHash = (bytes) => {
|
|
||||||
return multihashing(bytes, 'sha2-256')
|
|
||||||
}
|
|
153
stats.md
Normal file
153
stats.md
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
# Stats
|
||||||
|
|
||||||
|
## Size
|
||||||
|
|
||||||
|
| | non-minified | minified |
|
||||||
|
|-------|--------------|----------|
|
||||||
|
|before | `1.8M` | `949K` |
|
||||||
|
|after | `606K` | `382K` |
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
|
||||||
|
### RSA
|
||||||
|
|
||||||
|
#### Before
|
||||||
|
|
||||||
|
##### Node `6.6.0`
|
||||||
|
|
||||||
|
```
|
||||||
|
generateKeyPair 1024bits x 3.51 ops/sec ±29.45% (22 runs sampled)
|
||||||
|
generateKeyPair 2048bits x 0.17 ops/sec ±145.40% (5 runs sampled)
|
||||||
|
generateKeyPair 4096bits x 0.02 ops/sec ±96.53% (5 runs sampled)
|
||||||
|
sign and verify x 95.98 ops/sec ±1.51% (71 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Browser (Chrome `53.0.2785.116`)
|
||||||
|
|
||||||
|
```
|
||||||
|
generateKeyPair 1024bits x 3.56 ops/sec ±27.16% (23 runs sampled)
|
||||||
|
generateKeyPair 2048bits x 0.49 ops/sec ±69.32% (8 runs sampled)
|
||||||
|
generateKeyPair 4096bits x 0.03 ops/sec ±77.11% (5 runs sampled)
|
||||||
|
sign and verify x 109 ops/sec ±2.00% (53 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### After
|
||||||
|
|
||||||
|
##### Node `6.6.0`
|
||||||
|
|
||||||
|
```
|
||||||
|
generateKeyPair 1024bits x 42.45 ops/sec ±9.87% (52 runs sampled)
|
||||||
|
generateKeyPair 2048bits x 7.46 ops/sec ±23.80% (16 runs sampled)
|
||||||
|
generateKeyPair 4096bits x 1.50 ops/sec ±58.59% (13 runs sampled)
|
||||||
|
sign and verify x 1,080 ops/sec ±2.23% (74 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Browser (Chrome `53.0.2785.116`)
|
||||||
|
|
||||||
|
```
|
||||||
|
generateKeyPair 1024bits x 5.89 ops/sec ±18.94% (19 runs sampled)
|
||||||
|
generateKeyPair 2048bits x 1.32 ops/sec ±36.84% (10 runs sampled)
|
||||||
|
generateKeyPair 4096bits x 0.20 ops/sec ±62.49% (5 runs sampled)
|
||||||
|
sign and verify x 608 ops/sec ±6.75% (56 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Key Stretcher
|
||||||
|
|
||||||
|
|
||||||
|
#### Before
|
||||||
|
|
||||||
|
##### Node `6.6.0`
|
||||||
|
|
||||||
|
```
|
||||||
|
keyStretcher AES-128 SHA1 x 3,863 ops/sec ±3.80% (70 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA256 x 3,862 ops/sec ±5.33% (64 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA512 x 3,369 ops/sec ±1.73% (73 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA1 x 3,008 ops/sec ±4.81% (67 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA256 x 2,900 ops/sec ±7.01% (64 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA512 x 2,553 ops/sec ±4.45% (73 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA1 x 28,045 ops/sec ±7.32% (61 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA256 x 18,860 ops/sec ±5.36% (67 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA512 x 12,142 ops/sec ±12.44% (72 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Browser (Chrome `53.0.2785.116`)
|
||||||
|
|
||||||
|
```
|
||||||
|
keyStretcher AES-128 SHA1 x 4,168 ops/sec ±4.08% (49 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA256 x 4,239 ops/sec ±6.36% (48 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA512 x 3,600 ops/sec ±5.15% (51 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA1 x 3,009 ops/sec ±6.82% (48 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA256 x 3,086 ops/sec ±9.56% (19 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA512 x 2,470 ops/sec ±2.22% (54 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA1 x 7,143 ops/sec ±15.17% (9 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA256 x 17,846 ops/sec ±4.74% (46 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA512 x 7,726 ops/sec ±1.81% (50 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### After
|
||||||
|
|
||||||
|
##### Node `6.6.0`
|
||||||
|
|
||||||
|
```
|
||||||
|
keyStretcher AES-128 SHA1 x 6,680 ops/sec ±3.62% (65 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA256 x 8,124 ops/sec ±4.37% (66 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA512 x 11,683 ops/sec ±4.56% (66 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA1 x 5,531 ops/sec ±4.69% (68 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA256 x 6,725 ops/sec ±4.87% (66 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA512 x 9,042 ops/sec ±3.87% (64 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA1 x 40,757 ops/sec ±5.38% (60 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA256 x 41,845 ops/sec ±4.89% (64 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA512 x 42,345 ops/sec ±4.86% (63 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Browser (Chrome `53.0.2785.116`)
|
||||||
|
|
||||||
|
```
|
||||||
|
keyStretcher AES-128 SHA1 x 479 ops/sec ±2.12% (54 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA256 x 668 ops/sec ±2.02% (53 runs sampled)
|
||||||
|
keyStretcher AES-128 SHA512 x 1,112 ops/sec ±1.61% (54 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA1 x 460 ops/sec ±1.37% (54 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA256 x 596 ops/sec ±1.56% (54 runs sampled)
|
||||||
|
keyStretcher AES-256 SHA512 x 808 ops/sec ±3.27% (52 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA1 x 3,015 ops/sec ±3.51% (52 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA256 x 2,755 ops/sec ±3.82% (53 runs sampled)
|
||||||
|
keyStretcher Blowfish SHA512 x 2,955 ops/sec ±5.35% (51 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Ephemeral Keys
|
||||||
|
|
||||||
|
#### Before
|
||||||
|
|
||||||
|
##### Node `6.6.0`
|
||||||
|
|
||||||
|
```
|
||||||
|
ephemeral key with secrect P-256 x 89.93 ops/sec ±39.45% (72 runs sampled)
|
||||||
|
ephemeral key with secrect P-384 x 110 ops/sec ±1.28% (71 runs sampled)
|
||||||
|
ephemeral key with secrect P-521 x 112 ops/sec ±1.70% (72 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Browser (Chrome `53.0.2785.116`)
|
||||||
|
|
||||||
|
```
|
||||||
|
ephemeral key with secrect P-256 x 6.27 ops/sec ±15.89% (35 runs sampled)
|
||||||
|
ephemeral key with secrect P-384 x 6.84 ops/sec ±1.21% (35 runs sampled)
|
||||||
|
ephemeral key with secrect P-521 x 6.60 ops/sec ±1.84% (34 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### After
|
||||||
|
|
||||||
|
##### Node `6.6.0`
|
||||||
|
|
||||||
|
```
|
||||||
|
ephemeral key with secrect P-256 x 555 ops/sec ±1.61% (75 runs sampled)
|
||||||
|
ephemeral key with secrect P-384 x 547 ops/sec ±4.40% (68 runs sampled)
|
||||||
|
ephemeral key with secrect P-521 x 583 ops/sec ±4.84% (72 runs sampled)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Browser (Chrome `53.0.2785.116`)
|
||||||
|
|
||||||
|
```
|
||||||
|
ephemeral key with secrect P-256 x 796 ops/sec ±2.36% (53 runs sampled)
|
||||||
|
ephemeral key with secrect P-384 x 788 ops/sec ±2.66% (53 runs sampled)
|
||||||
|
ephemeral key with secrect P-521 x 808 ops/sec ±1.83% (54 runs sampled)
|
||||||
|
```
|
116
test/aes.spec.js
Normal file
116
test/aes.spec.js
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const series = require('async/series')
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
const crypto = require('../src')
|
||||||
|
const fixtures = require('./fixtures/aes')
|
||||||
|
const goFixtures = require('./fixtures/go-aes')
|
||||||
|
|
||||||
|
const bytes = {
|
||||||
|
16: 'AES-128',
|
||||||
|
32: 'AES-256'
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('AES-CTR', () => {
|
||||||
|
Object.keys(bytes).forEach((byte) => {
|
||||||
|
it(`${bytes[byte]} - encrypt and decrypt`, (done) => {
|
||||||
|
const key = Buffer.alloc(parseInt(byte, 10))
|
||||||
|
key.fill(5)
|
||||||
|
|
||||||
|
const iv = Buffer.alloc(16)
|
||||||
|
iv.fill(1)
|
||||||
|
|
||||||
|
crypto.aes.create(key, iv, (err, cipher) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
|
||||||
|
series([
|
||||||
|
encryptAndDecrypt(cipher),
|
||||||
|
encryptAndDecrypt(cipher),
|
||||||
|
encryptAndDecrypt(cipher),
|
||||||
|
encryptAndDecrypt(cipher),
|
||||||
|
encryptAndDecrypt(cipher)
|
||||||
|
], done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Object.keys(bytes).forEach((byte) => {
|
||||||
|
it(`${bytes[byte]} - fixed - encrypt and decrypt`, (done) => {
|
||||||
|
const key = Buffer.alloc(parseInt(byte, 10))
|
||||||
|
key.fill(5)
|
||||||
|
|
||||||
|
const iv = Buffer.alloc(16)
|
||||||
|
iv.fill(1)
|
||||||
|
|
||||||
|
crypto.aes.create(key, iv, (err, cipher) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
|
||||||
|
series(fixtures[byte].inputs.map((rawIn, i) => (cb) => {
|
||||||
|
const input = Buffer.from(rawIn)
|
||||||
|
const output = Buffer.from(fixtures[byte].outputs[i])
|
||||||
|
cipher.encrypt(input, (err, res) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(res).to.have.length(output.length)
|
||||||
|
expect(res).to.be.eql(output)
|
||||||
|
cipher.decrypt(res, (err, res) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(res).to.be.eql(input)
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}), done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Object.keys(bytes).forEach((byte) => {
|
||||||
|
if (!goFixtures[byte]) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
it(`${bytes[byte]} - go interop - encrypt and decrypt`, (done) => {
|
||||||
|
const key = Buffer.alloc(parseInt(byte, 10))
|
||||||
|
key.fill(5)
|
||||||
|
|
||||||
|
const iv = Buffer.alloc(16)
|
||||||
|
iv.fill(1)
|
||||||
|
|
||||||
|
crypto.aes.create(key, iv, (err, cipher) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
|
||||||
|
series(goFixtures[byte].inputs.map((rawIn, i) => (cb) => {
|
||||||
|
const input = Buffer.from(rawIn)
|
||||||
|
const output = Buffer.from(goFixtures[byte].outputs[i])
|
||||||
|
cipher.encrypt(input, (err, res) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(res).to.have.length(output.length)
|
||||||
|
expect(res).to.be.eql(output)
|
||||||
|
cipher.decrypt(res, (err, res) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(res).to.be.eql(input)
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}), done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
function encryptAndDecrypt (cipher) {
|
||||||
|
const data = Buffer.alloc(100)
|
||||||
|
data.fill(Math.ceil(Math.random() * 100))
|
||||||
|
return (cb) => {
|
||||||
|
cipher.encrypt(data, (err, res) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
cipher.decrypt(res, (err, res) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(res).to.be.eql(data)
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
195
test/ed25519.spec.js
Normal file
195
test/ed25519.spec.js
Normal file
@@ -0,0 +1,195 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
const crypto = require('../src')
|
||||||
|
const ed25519 = crypto.keys.ed25519
|
||||||
|
const fixtures = require('./fixtures/go-key-ed25519')
|
||||||
|
|
||||||
|
describe('ed25519', () => {
|
||||||
|
let key
|
||||||
|
before((done) => {
|
||||||
|
crypto.generateKeyPair('Ed25519', 512, (err, _key) => {
|
||||||
|
if (err) return done(err)
|
||||||
|
key = _key
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('generates a valid key', (done) => {
|
||||||
|
expect(
|
||||||
|
key
|
||||||
|
).to.be.an.instanceof(
|
||||||
|
ed25519.Ed25519PrivateKey
|
||||||
|
)
|
||||||
|
|
||||||
|
key.hash((err, digest) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(digest).to.have.length(34)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('signs', (done) => {
|
||||||
|
const text = crypto.randomBytes(512)
|
||||||
|
|
||||||
|
key.sign(text, (err, sig) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key.public.verify(text, sig, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('encoding', (done) => {
|
||||||
|
const keyMarshal = key.marshal()
|
||||||
|
ed25519.unmarshalEd25519PrivateKey(keyMarshal, (err, key2) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
const keyMarshal2 = key2.marshal()
|
||||||
|
|
||||||
|
expect(
|
||||||
|
keyMarshal
|
||||||
|
).to.be.eql(
|
||||||
|
keyMarshal2
|
||||||
|
)
|
||||||
|
|
||||||
|
const pk = key.public
|
||||||
|
const pkMarshal = pk.marshal()
|
||||||
|
const pk2 = ed25519.unmarshalEd25519PublicKey(pkMarshal)
|
||||||
|
const pkMarshal2 = pk2.marshal()
|
||||||
|
|
||||||
|
expect(
|
||||||
|
pkMarshal
|
||||||
|
).to.be.eql(
|
||||||
|
pkMarshal2
|
||||||
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('key equals', () => {
|
||||||
|
it('equals itself', () => {
|
||||||
|
expect(
|
||||||
|
key.equals(key)
|
||||||
|
).to.be.eql(
|
||||||
|
true
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.public.equals(key.public)
|
||||||
|
).to.be.eql(
|
||||||
|
true
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('not equals other key', (done) => {
|
||||||
|
crypto.generateKeyPair('Ed25519', 512, (err, key2) => {
|
||||||
|
if (err) return done(err)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.equals(key2)
|
||||||
|
).to.be.eql(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key2.equals(key)
|
||||||
|
).to.be.eql(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key.public.equals(key2.public)
|
||||||
|
).to.be.eql(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
key2.public.equals(key.public)
|
||||||
|
).to.be.eql(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('sign and verify', (done) => {
|
||||||
|
const data = Buffer.from('hello world')
|
||||||
|
key.sign(data, (err, sig) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key.public.verify(data, sig, (err, valid) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
expect(valid).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails to verify for different data', (done) => {
|
||||||
|
const data = Buffer.from('hello world')
|
||||||
|
key.sign(data, (err, sig) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
key.public.verify(Buffer.from('hello'), sig, (err, valid) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
expect(valid).to.be.eql(false)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('go interop', () => {
|
||||||
|
let privateKey
|
||||||
|
before((done) => {
|
||||||
|
crypto.unmarshalPrivateKey(fixtures.verify.privateKey, (err, key) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
privateKey = key
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('verifies with data from go', (done) => {
|
||||||
|
const key = crypto.unmarshalPublicKey(fixtures.verify.publicKey)
|
||||||
|
|
||||||
|
key.verify(fixtures.verify.data, fixtures.verify.signature, (err, ok) => {
|
||||||
|
if (err) throw err
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(ok).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('generates the same signature as go', (done) => {
|
||||||
|
privateKey.sign(fixtures.verify.data, (err, sig) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(sig).to.deep.equal(fixtures.verify.signature)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@@ -1,44 +1,75 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
const EC = require('elliptic').ec
|
const parallel = require('async/parallel')
|
||||||
|
|
||||||
const crypto = require('../src')
|
|
||||||
const fixtures = require('./fixtures/go-elliptic-key')
|
const fixtures = require('./fixtures/go-elliptic-key')
|
||||||
|
const crypto = require('../src')
|
||||||
|
|
||||||
|
const curves = ['P-256', 'P-384'] // 'P-521' fails in tests :( no clue why
|
||||||
|
const lengths = {
|
||||||
|
'P-256': 65,
|
||||||
|
'P-384': 97,
|
||||||
|
'P-521': 133
|
||||||
|
}
|
||||||
|
const secretLengths = {
|
||||||
|
'P-256': 32,
|
||||||
|
'P-384': 48,
|
||||||
|
'P-521': 66
|
||||||
|
}
|
||||||
|
|
||||||
describe('generateEphemeralKeyPair', () => {
|
describe('generateEphemeralKeyPair', () => {
|
||||||
it('returns a function that generates a shared secret', () => {
|
curves.forEach((curve) => {
|
||||||
const res = crypto.generateEphemeralKeyPair('P-256')
|
it(`generate and shared key ${curve}`, (done) => {
|
||||||
const ourPublic = '044374add0df35706db7dade25f3959fc051d2ef5166f8a6a0aa632d0ab41cdb4d30e1a064e121ac56155235a6b8d4c5d8fe35e019f507f4e2ff1445e229d7af43'
|
parallel([
|
||||||
|
(cb) => crypto.generateEphemeralKeyPair(curve, cb),
|
||||||
|
(cb) => crypto.generateEphemeralKeyPair(curve, cb)
|
||||||
|
], (err, keys) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(keys[0].key).to.have.length(lengths[curve])
|
||||||
|
expect(keys[1].key).to.have.length(lengths[curve])
|
||||||
|
|
||||||
expect(
|
keys[0].genSharedKey(keys[1].key, (err, shared) => {
|
||||||
res.genSharedKey(ourPublic)
|
expect(err).to.not.exist
|
||||||
).to.have.length(32)
|
expect(shared).to.have.length(secretLengths[curve])
|
||||||
|
done()
|
||||||
expect(
|
})
|
||||||
res.key
|
})
|
||||||
).to.exist
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('go interop', () => {
|
describe('go interop', () => {
|
||||||
it('generates a shared secret', () => {
|
it('generates a shared secret', (done) => {
|
||||||
const curve = fixtures.curve
|
const curve = fixtures.curve
|
||||||
const ec = new EC(fixtures.curveJs)
|
|
||||||
const bobPrivate = ec.keyFromPrivate(fixtures.bob.private, 'binary')
|
|
||||||
|
|
||||||
const alice = crypto.generateEphemeralKeyPair(curve)
|
parallel([
|
||||||
const bob = {
|
(cb) => crypto.generateEphemeralKeyPair(curve, cb),
|
||||||
key: fixtures.bob.public,
|
(cb) => crypto.generateEphemeralKeyPair(curve, cb)
|
||||||
// this is using bobs private key from go ipfs
|
], (err, res) => {
|
||||||
// instead of alices
|
expect(err).to.not.exist
|
||||||
genSharedKey: (key) => alice.genSharedKey(key, bobPrivate)
|
const alice = res[0]
|
||||||
}
|
const bob = res[1]
|
||||||
|
bob.key = fixtures.bob.public
|
||||||
|
|
||||||
const s1 = alice.genSharedKey(bob.key)
|
parallel([
|
||||||
const s2 = bob.genSharedKey(alice.key)
|
(cb) => alice.genSharedKey(bob.key, cb),
|
||||||
|
(cb) => bob.genSharedKey(alice.key, fixtures.bob, cb)
|
||||||
|
], (err, secrets) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
|
||||||
expect(s1.equals(s2)).to.be.eql(true)
|
expect(
|
||||||
|
secrets[0]
|
||||||
|
).to.be.eql(
|
||||||
|
secrets[1]
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(secrets[0]).to.have.length(32)
|
||||||
|
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
27
test/fixtures/aes.js
vendored
Normal file
27
test/fixtures/aes.js
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const fixes16 = [
|
||||||
|
require('./fix1.json'),
|
||||||
|
require('./fix2.json'),
|
||||||
|
require('./fix3.json'),
|
||||||
|
require('./fix4.json'),
|
||||||
|
require('./fix5.json')
|
||||||
|
]
|
||||||
|
const fixes32 = [
|
||||||
|
require('./fix6.json'),
|
||||||
|
require('./fix7.json'),
|
||||||
|
require('./fix8.json'),
|
||||||
|
require('./fix9.json'),
|
||||||
|
require('./fix10.json')
|
||||||
|
]
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
16: {
|
||||||
|
inputs: fixes16.map((f) => f.input),
|
||||||
|
outputs: fixes16.map((f) => f.output)
|
||||||
|
},
|
||||||
|
32: {
|
||||||
|
inputs: fixes32.map((f) => f.input),
|
||||||
|
outputs: fixes32.map((f) => f.output)
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix1.json
vendored
Normal file
212
test/fixtures/fix1.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47,
|
||||||
|
47
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
121,
|
||||||
|
104,
|
||||||
|
0,
|
||||||
|
151,
|
||||||
|
57,
|
||||||
|
137,
|
||||||
|
22,
|
||||||
|
239,
|
||||||
|
234,
|
||||||
|
151,
|
||||||
|
58,
|
||||||
|
15,
|
||||||
|
100,
|
||||||
|
22,
|
||||||
|
228,
|
||||||
|
110,
|
||||||
|
85,
|
||||||
|
248,
|
||||||
|
249,
|
||||||
|
15,
|
||||||
|
145,
|
||||||
|
128,
|
||||||
|
223,
|
||||||
|
25,
|
||||||
|
192,
|
||||||
|
175,
|
||||||
|
132,
|
||||||
|
169,
|
||||||
|
98,
|
||||||
|
203,
|
||||||
|
231,
|
||||||
|
106,
|
||||||
|
224,
|
||||||
|
102,
|
||||||
|
206,
|
||||||
|
244,
|
||||||
|
29,
|
||||||
|
213,
|
||||||
|
36,
|
||||||
|
2,
|
||||||
|
26,
|
||||||
|
213,
|
||||||
|
94,
|
||||||
|
29,
|
||||||
|
134,
|
||||||
|
219,
|
||||||
|
136,
|
||||||
|
73,
|
||||||
|
212,
|
||||||
|
176,
|
||||||
|
33,
|
||||||
|
95,
|
||||||
|
198,
|
||||||
|
91,
|
||||||
|
148,
|
||||||
|
139,
|
||||||
|
132,
|
||||||
|
252,
|
||||||
|
182,
|
||||||
|
115,
|
||||||
|
116,
|
||||||
|
160,
|
||||||
|
146,
|
||||||
|
194,
|
||||||
|
0,
|
||||||
|
97,
|
||||||
|
181,
|
||||||
|
0,
|
||||||
|
193,
|
||||||
|
149,
|
||||||
|
21,
|
||||||
|
51,
|
||||||
|
248,
|
||||||
|
97,
|
||||||
|
32,
|
||||||
|
62,
|
||||||
|
86,
|
||||||
|
153,
|
||||||
|
238,
|
||||||
|
67,
|
||||||
|
38,
|
||||||
|
34,
|
||||||
|
55,
|
||||||
|
143,
|
||||||
|
70,
|
||||||
|
193,
|
||||||
|
99,
|
||||||
|
107,
|
||||||
|
31,
|
||||||
|
67,
|
||||||
|
90,
|
||||||
|
97,
|
||||||
|
55,
|
||||||
|
63,
|
||||||
|
69,
|
||||||
|
203,
|
||||||
|
33,
|
||||||
|
233,
|
||||||
|
74,
|
||||||
|
237
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix10.json
vendored
Normal file
212
test/fixtures/fix10.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72,
|
||||||
|
72
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
201,
|
||||||
|
224,
|
||||||
|
56,
|
||||||
|
64,
|
||||||
|
167,
|
||||||
|
224,
|
||||||
|
124,
|
||||||
|
49,
|
||||||
|
19,
|
||||||
|
51,
|
||||||
|
208,
|
||||||
|
226,
|
||||||
|
216,
|
||||||
|
50,
|
||||||
|
12,
|
||||||
|
73,
|
||||||
|
15,
|
||||||
|
255,
|
||||||
|
156,
|
||||||
|
108,
|
||||||
|
98,
|
||||||
|
179,
|
||||||
|
144,
|
||||||
|
110,
|
||||||
|
33,
|
||||||
|
151,
|
||||||
|
43,
|
||||||
|
64,
|
||||||
|
227,
|
||||||
|
153,
|
||||||
|
120,
|
||||||
|
39,
|
||||||
|
21,
|
||||||
|
151,
|
||||||
|
41,
|
||||||
|
61,
|
||||||
|
245,
|
||||||
|
20,
|
||||||
|
31,
|
||||||
|
23,
|
||||||
|
109,
|
||||||
|
213,
|
||||||
|
109,
|
||||||
|
55,
|
||||||
|
35,
|
||||||
|
40,
|
||||||
|
122,
|
||||||
|
109,
|
||||||
|
41,
|
||||||
|
10,
|
||||||
|
32,
|
||||||
|
176,
|
||||||
|
25,
|
||||||
|
184,
|
||||||
|
91,
|
||||||
|
176,
|
||||||
|
177,
|
||||||
|
134,
|
||||||
|
138,
|
||||||
|
252,
|
||||||
|
160,
|
||||||
|
2,
|
||||||
|
108,
|
||||||
|
43,
|
||||||
|
222,
|
||||||
|
239,
|
||||||
|
174,
|
||||||
|
2,
|
||||||
|
145,
|
||||||
|
74,
|
||||||
|
34,
|
||||||
|
131,
|
||||||
|
237,
|
||||||
|
214,
|
||||||
|
235,
|
||||||
|
102,
|
||||||
|
26,
|
||||||
|
204,
|
||||||
|
124,
|
||||||
|
64,
|
||||||
|
101,
|
||||||
|
134,
|
||||||
|
186,
|
||||||
|
45,
|
||||||
|
55,
|
||||||
|
29,
|
||||||
|
52,
|
||||||
|
55,
|
||||||
|
171,
|
||||||
|
95,
|
||||||
|
172,
|
||||||
|
86,
|
||||||
|
242,
|
||||||
|
101,
|
||||||
|
141,
|
||||||
|
153,
|
||||||
|
222,
|
||||||
|
161,
|
||||||
|
128,
|
||||||
|
83
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix2.json
vendored
Normal file
212
test/fixtures/fix2.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24,
|
||||||
|
24
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
109,
|
||||||
|
172,
|
||||||
|
127,
|
||||||
|
179,
|
||||||
|
110,
|
||||||
|
222,
|
||||||
|
79,
|
||||||
|
219,
|
||||||
|
87,
|
||||||
|
76,
|
||||||
|
70,
|
||||||
|
204,
|
||||||
|
166,
|
||||||
|
110,
|
||||||
|
184,
|
||||||
|
229,
|
||||||
|
90,
|
||||||
|
49,
|
||||||
|
160,
|
||||||
|
252,
|
||||||
|
78,
|
||||||
|
58,
|
||||||
|
73,
|
||||||
|
51,
|
||||||
|
152,
|
||||||
|
218,
|
||||||
|
3,
|
||||||
|
200,
|
||||||
|
10,
|
||||||
|
124,
|
||||||
|
152,
|
||||||
|
117,
|
||||||
|
137,
|
||||||
|
40,
|
||||||
|
23,
|
||||||
|
127,
|
||||||
|
56,
|
||||||
|
57,
|
||||||
|
203,
|
||||||
|
31,
|
||||||
|
101,
|
||||||
|
227,
|
||||||
|
31,
|
||||||
|
198,
|
||||||
|
223,
|
||||||
|
86,
|
||||||
|
98,
|
||||||
|
120,
|
||||||
|
100,
|
||||||
|
86,
|
||||||
|
116,
|
||||||
|
144,
|
||||||
|
142,
|
||||||
|
127,
|
||||||
|
68,
|
||||||
|
175,
|
||||||
|
249,
|
||||||
|
232,
|
||||||
|
22,
|
||||||
|
83,
|
||||||
|
22,
|
||||||
|
68,
|
||||||
|
60,
|
||||||
|
230,
|
||||||
|
146,
|
||||||
|
22,
|
||||||
|
153,
|
||||||
|
193,
|
||||||
|
67,
|
||||||
|
5,
|
||||||
|
51,
|
||||||
|
253,
|
||||||
|
239,
|
||||||
|
210,
|
||||||
|
80,
|
||||||
|
31,
|
||||||
|
254,
|
||||||
|
103,
|
||||||
|
185,
|
||||||
|
145,
|
||||||
|
123,
|
||||||
|
99,
|
||||||
|
205,
|
||||||
|
175,
|
||||||
|
156,
|
||||||
|
144,
|
||||||
|
191,
|
||||||
|
67,
|
||||||
|
31,
|
||||||
|
236,
|
||||||
|
43,
|
||||||
|
98,
|
||||||
|
197,
|
||||||
|
235,
|
||||||
|
31,
|
||||||
|
50,
|
||||||
|
69,
|
||||||
|
228,
|
||||||
|
100,
|
||||||
|
64
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix3.json
vendored
Normal file
212
test/fixtures/fix3.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7,
|
||||||
|
7
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
191,
|
||||||
|
93,
|
||||||
|
126,
|
||||||
|
191,
|
||||||
|
180,
|
||||||
|
42,
|
||||||
|
118,
|
||||||
|
144,
|
||||||
|
28,
|
||||||
|
188,
|
||||||
|
211,
|
||||||
|
191,
|
||||||
|
211,
|
||||||
|
130,
|
||||||
|
170,
|
||||||
|
153,
|
||||||
|
134,
|
||||||
|
240,
|
||||||
|
179,
|
||||||
|
83,
|
||||||
|
75,
|
||||||
|
23,
|
||||||
|
42,
|
||||||
|
68,
|
||||||
|
158,
|
||||||
|
200,
|
||||||
|
123,
|
||||||
|
155,
|
||||||
|
57,
|
||||||
|
169,
|
||||||
|
152,
|
||||||
|
133,
|
||||||
|
33,
|
||||||
|
114,
|
||||||
|
90,
|
||||||
|
29,
|
||||||
|
131,
|
||||||
|
91,
|
||||||
|
70,
|
||||||
|
105,
|
||||||
|
83,
|
||||||
|
45,
|
||||||
|
40,
|
||||||
|
47,
|
||||||
|
77,
|
||||||
|
96,
|
||||||
|
97,
|
||||||
|
8,
|
||||||
|
40,
|
||||||
|
1,
|
||||||
|
110,
|
||||||
|
245,
|
||||||
|
106,
|
||||||
|
172,
|
||||||
|
152,
|
||||||
|
146,
|
||||||
|
5,
|
||||||
|
114,
|
||||||
|
132,
|
||||||
|
0,
|
||||||
|
179,
|
||||||
|
31,
|
||||||
|
44,
|
||||||
|
78,
|
||||||
|
19,
|
||||||
|
109,
|
||||||
|
92,
|
||||||
|
199,
|
||||||
|
226,
|
||||||
|
36,
|
||||||
|
12,
|
||||||
|
74,
|
||||||
|
180,
|
||||||
|
241,
|
||||||
|
224,
|
||||||
|
107,
|
||||||
|
83,
|
||||||
|
13,
|
||||||
|
167,
|
||||||
|
27,
|
||||||
|
251,
|
||||||
|
101,
|
||||||
|
193,
|
||||||
|
98,
|
||||||
|
49,
|
||||||
|
90,
|
||||||
|
225,
|
||||||
|
197,
|
||||||
|
75,
|
||||||
|
213,
|
||||||
|
144,
|
||||||
|
52,
|
||||||
|
235,
|
||||||
|
130,
|
||||||
|
92,
|
||||||
|
247,
|
||||||
|
219,
|
||||||
|
139,
|
||||||
|
209,
|
||||||
|
132
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix4.json
vendored
Normal file
212
test/fixtures/fix4.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25,
|
||||||
|
25
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
191,
|
||||||
|
110,
|
||||||
|
53,
|
||||||
|
190,
|
||||||
|
250,
|
||||||
|
44,
|
||||||
|
190,
|
||||||
|
136,
|
||||||
|
22,
|
||||||
|
209,
|
||||||
|
131,
|
||||||
|
200,
|
||||||
|
112,
|
||||||
|
31,
|
||||||
|
61,
|
||||||
|
183,
|
||||||
|
247,
|
||||||
|
95,
|
||||||
|
4,
|
||||||
|
249,
|
||||||
|
69,
|
||||||
|
147,
|
||||||
|
238,
|
||||||
|
74,
|
||||||
|
18,
|
||||||
|
71,
|
||||||
|
197,
|
||||||
|
115,
|
||||||
|
141,
|
||||||
|
226,
|
||||||
|
102,
|
||||||
|
92,
|
||||||
|
91,
|
||||||
|
128,
|
||||||
|
181,
|
||||||
|
172,
|
||||||
|
67,
|
||||||
|
109,
|
||||||
|
17,
|
||||||
|
165,
|
||||||
|
52,
|
||||||
|
10,
|
||||||
|
55,
|
||||||
|
31,
|
||||||
|
55,
|
||||||
|
225,
|
||||||
|
253,
|
||||||
|
140,
|
||||||
|
154,
|
||||||
|
35,
|
||||||
|
104,
|
||||||
|
62,
|
||||||
|
119,
|
||||||
|
103,
|
||||||
|
197,
|
||||||
|
152,
|
||||||
|
125,
|
||||||
|
134,
|
||||||
|
140,
|
||||||
|
181,
|
||||||
|
170,
|
||||||
|
76,
|
||||||
|
75,
|
||||||
|
114,
|
||||||
|
195,
|
||||||
|
188,
|
||||||
|
68,
|
||||||
|
197,
|
||||||
|
28,
|
||||||
|
47,
|
||||||
|
116,
|
||||||
|
82,
|
||||||
|
34,
|
||||||
|
128,
|
||||||
|
232,
|
||||||
|
122,
|
||||||
|
14,
|
||||||
|
229,
|
||||||
|
122,
|
||||||
|
161,
|
||||||
|
36,
|
||||||
|
212,
|
||||||
|
161,
|
||||||
|
164,
|
||||||
|
145,
|
||||||
|
86,
|
||||||
|
215,
|
||||||
|
233,
|
||||||
|
222,
|
||||||
|
50,
|
||||||
|
143,
|
||||||
|
89,
|
||||||
|
131,
|
||||||
|
32,
|
||||||
|
130,
|
||||||
|
196,
|
||||||
|
109,
|
||||||
|
36,
|
||||||
|
204,
|
||||||
|
254
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix5.json
vendored
Normal file
212
test/fixtures/fix5.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48,
|
||||||
|
48
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
97,
|
||||||
|
142,
|
||||||
|
8,
|
||||||
|
37,
|
||||||
|
69,
|
||||||
|
173,
|
||||||
|
124,
|
||||||
|
180,
|
||||||
|
97,
|
||||||
|
70,
|
||||||
|
45,
|
||||||
|
91,
|
||||||
|
196,
|
||||||
|
126,
|
||||||
|
184,
|
||||||
|
135,
|
||||||
|
213,
|
||||||
|
104,
|
||||||
|
171,
|
||||||
|
89,
|
||||||
|
231,
|
||||||
|
63,
|
||||||
|
43,
|
||||||
|
42,
|
||||||
|
7,
|
||||||
|
245,
|
||||||
|
75,
|
||||||
|
165,
|
||||||
|
205,
|
||||||
|
182,
|
||||||
|
24,
|
||||||
|
50,
|
||||||
|
170,
|
||||||
|
217,
|
||||||
|
128,
|
||||||
|
112,
|
||||||
|
114,
|
||||||
|
215,
|
||||||
|
209,
|
||||||
|
145,
|
||||||
|
135,
|
||||||
|
235,
|
||||||
|
179,
|
||||||
|
212,
|
||||||
|
5,
|
||||||
|
81,
|
||||||
|
142,
|
||||||
|
199,
|
||||||
|
53,
|
||||||
|
221,
|
||||||
|
39,
|
||||||
|
239,
|
||||||
|
167,
|
||||||
|
21,
|
||||||
|
237,
|
||||||
|
168,
|
||||||
|
145,
|
||||||
|
249,
|
||||||
|
250,
|
||||||
|
108,
|
||||||
|
2,
|
||||||
|
247,
|
||||||
|
89,
|
||||||
|
73,
|
||||||
|
228,
|
||||||
|
227,
|
||||||
|
255,
|
||||||
|
155,
|
||||||
|
121,
|
||||||
|
157,
|
||||||
|
205,
|
||||||
|
96,
|
||||||
|
43,
|
||||||
|
32,
|
||||||
|
112,
|
||||||
|
209,
|
||||||
|
173,
|
||||||
|
96,
|
||||||
|
143,
|
||||||
|
43,
|
||||||
|
220,
|
||||||
|
140,
|
||||||
|
26,
|
||||||
|
205,
|
||||||
|
34,
|
||||||
|
34,
|
||||||
|
53,
|
||||||
|
157,
|
||||||
|
41,
|
||||||
|
167,
|
||||||
|
125,
|
||||||
|
235,
|
||||||
|
243,
|
||||||
|
85,
|
||||||
|
13,
|
||||||
|
14,
|
||||||
|
93,
|
||||||
|
109,
|
||||||
|
233,
|
||||||
|
186
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix6.json
vendored
Normal file
212
test/fixtures/fix6.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68,
|
||||||
|
68
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
198,
|
||||||
|
59,
|
||||||
|
91,
|
||||||
|
107,
|
||||||
|
222,
|
||||||
|
214,
|
||||||
|
31,
|
||||||
|
230,
|
||||||
|
112,
|
||||||
|
1,
|
||||||
|
199,
|
||||||
|
174,
|
||||||
|
17,
|
||||||
|
224,
|
||||||
|
194,
|
||||||
|
225,
|
||||||
|
138,
|
||||||
|
65,
|
||||||
|
58,
|
||||||
|
160,
|
||||||
|
31,
|
||||||
|
249,
|
||||||
|
102,
|
||||||
|
16,
|
||||||
|
74,
|
||||||
|
46,
|
||||||
|
85,
|
||||||
|
30,
|
||||||
|
232,
|
||||||
|
249,
|
||||||
|
120,
|
||||||
|
108,
|
||||||
|
38,
|
||||||
|
165,
|
||||||
|
105,
|
||||||
|
27,
|
||||||
|
12,
|
||||||
|
98,
|
||||||
|
98,
|
||||||
|
108,
|
||||||
|
130,
|
||||||
|
163,
|
||||||
|
16,
|
||||||
|
137,
|
||||||
|
116,
|
||||||
|
104,
|
||||||
|
153,
|
||||||
|
188,
|
||||||
|
5,
|
||||||
|
251,
|
||||||
|
163,
|
||||||
|
244,
|
||||||
|
0,
|
||||||
|
58,
|
||||||
|
1,
|
||||||
|
207,
|
||||||
|
25,
|
||||||
|
78,
|
||||||
|
188,
|
||||||
|
210,
|
||||||
|
205,
|
||||||
|
221,
|
||||||
|
48,
|
||||||
|
132,
|
||||||
|
72,
|
||||||
|
209,
|
||||||
|
11,
|
||||||
|
67,
|
||||||
|
14,
|
||||||
|
42,
|
||||||
|
218,
|
||||||
|
71,
|
||||||
|
148,
|
||||||
|
252,
|
||||||
|
126,
|
||||||
|
183,
|
||||||
|
60,
|
||||||
|
32,
|
||||||
|
93,
|
||||||
|
36,
|
||||||
|
125,
|
||||||
|
103,
|
||||||
|
191,
|
||||||
|
117,
|
||||||
|
82,
|
||||||
|
241,
|
||||||
|
190,
|
||||||
|
176,
|
||||||
|
1,
|
||||||
|
129,
|
||||||
|
118,
|
||||||
|
112,
|
||||||
|
139,
|
||||||
|
153,
|
||||||
|
178,
|
||||||
|
56,
|
||||||
|
61,
|
||||||
|
91,
|
||||||
|
52,
|
||||||
|
198
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix7.json
vendored
Normal file
212
test/fixtures/fix7.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
206,
|
||||||
|
241,
|
||||||
|
99,
|
||||||
|
120,
|
||||||
|
248,
|
||||||
|
163,
|
||||||
|
57,
|
||||||
|
30,
|
||||||
|
216,
|
||||||
|
86,
|
||||||
|
255,
|
||||||
|
192,
|
||||||
|
89,
|
||||||
|
193,
|
||||||
|
176,
|
||||||
|
17,
|
||||||
|
78,
|
||||||
|
62,
|
||||||
|
80,
|
||||||
|
149,
|
||||||
|
189,
|
||||||
|
235,
|
||||||
|
27,
|
||||||
|
157,
|
||||||
|
175,
|
||||||
|
248,
|
||||||
|
7,
|
||||||
|
19,
|
||||||
|
222,
|
||||||
|
64,
|
||||||
|
111,
|
||||||
|
199,
|
||||||
|
204,
|
||||||
|
163,
|
||||||
|
26,
|
||||||
|
16,
|
||||||
|
95,
|
||||||
|
221,
|
||||||
|
70,
|
||||||
|
32,
|
||||||
|
239,
|
||||||
|
4,
|
||||||
|
58,
|
||||||
|
162,
|
||||||
|
253,
|
||||||
|
237,
|
||||||
|
8,
|
||||||
|
51,
|
||||||
|
94,
|
||||||
|
3,
|
||||||
|
165,
|
||||||
|
186,
|
||||||
|
223,
|
||||||
|
210,
|
||||||
|
116,
|
||||||
|
101,
|
||||||
|
228,
|
||||||
|
82,
|
||||||
|
103,
|
||||||
|
76,
|
||||||
|
74,
|
||||||
|
44,
|
||||||
|
51,
|
||||||
|
117,
|
||||||
|
189,
|
||||||
|
140,
|
||||||
|
132,
|
||||||
|
230,
|
||||||
|
188,
|
||||||
|
243,
|
||||||
|
24,
|
||||||
|
158,
|
||||||
|
149,
|
||||||
|
93,
|
||||||
|
147,
|
||||||
|
226,
|
||||||
|
113,
|
||||||
|
195,
|
||||||
|
31,
|
||||||
|
24,
|
||||||
|
9,
|
||||||
|
19,
|
||||||
|
27,
|
||||||
|
132,
|
||||||
|
180,
|
||||||
|
152,
|
||||||
|
26,
|
||||||
|
65,
|
||||||
|
125,
|
||||||
|
144,
|
||||||
|
86,
|
||||||
|
167,
|
||||||
|
41,
|
||||||
|
144,
|
||||||
|
158,
|
||||||
|
204,
|
||||||
|
76,
|
||||||
|
46,
|
||||||
|
181,
|
||||||
|
110
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix8.json
vendored
Normal file
212
test/fixtures/fix8.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75,
|
||||||
|
75
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
153,
|
||||||
|
161,
|
||||||
|
201,
|
||||||
|
66,
|
||||||
|
87,
|
||||||
|
46,
|
||||||
|
242,
|
||||||
|
87,
|
||||||
|
136,
|
||||||
|
238,
|
||||||
|
151,
|
||||||
|
198,
|
||||||
|
129,
|
||||||
|
122,
|
||||||
|
62,
|
||||||
|
205,
|
||||||
|
142,
|
||||||
|
252,
|
||||||
|
109,
|
||||||
|
187,
|
||||||
|
238,
|
||||||
|
183,
|
||||||
|
63,
|
||||||
|
95,
|
||||||
|
240,
|
||||||
|
97,
|
||||||
|
220,
|
||||||
|
209,
|
||||||
|
37,
|
||||||
|
144,
|
||||||
|
237,
|
||||||
|
84,
|
||||||
|
251,
|
||||||
|
149,
|
||||||
|
152,
|
||||||
|
222,
|
||||||
|
89,
|
||||||
|
200,
|
||||||
|
208,
|
||||||
|
119,
|
||||||
|
213,
|
||||||
|
38,
|
||||||
|
65,
|
||||||
|
19,
|
||||||
|
2,
|
||||||
|
252,
|
||||||
|
193,
|
||||||
|
125,
|
||||||
|
94,
|
||||||
|
76,
|
||||||
|
182,
|
||||||
|
198,
|
||||||
|
243,
|
||||||
|
121,
|
||||||
|
253,
|
||||||
|
16,
|
||||||
|
45,
|
||||||
|
254,
|
||||||
|
82,
|
||||||
|
47,
|
||||||
|
146,
|
||||||
|
206,
|
||||||
|
41,
|
||||||
|
105,
|
||||||
|
254,
|
||||||
|
237,
|
||||||
|
236,
|
||||||
|
141,
|
||||||
|
118,
|
||||||
|
214,
|
||||||
|
197,
|
||||||
|
228,
|
||||||
|
59,
|
||||||
|
125,
|
||||||
|
45,
|
||||||
|
200,
|
||||||
|
61,
|
||||||
|
24,
|
||||||
|
110,
|
||||||
|
26,
|
||||||
|
235,
|
||||||
|
92,
|
||||||
|
175,
|
||||||
|
255,
|
||||||
|
85,
|
||||||
|
119,
|
||||||
|
57,
|
||||||
|
160,
|
||||||
|
145,
|
||||||
|
107,
|
||||||
|
206,
|
||||||
|
28,
|
||||||
|
62,
|
||||||
|
10,
|
||||||
|
166,
|
||||||
|
89,
|
||||||
|
7,
|
||||||
|
20,
|
||||||
|
246,
|
||||||
|
243
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
212
test/fixtures/fix9.json
vendored
Normal file
212
test/fixtures/fix9.json
vendored
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
{
|
||||||
|
"input": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35,
|
||||||
|
35
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"output": {
|
||||||
|
"type": "Buffer",
|
||||||
|
"data": [
|
||||||
|
79,
|
||||||
|
215,
|
||||||
|
22,
|
||||||
|
29,
|
||||||
|
132,
|
||||||
|
130,
|
||||||
|
214,
|
||||||
|
248,
|
||||||
|
237,
|
||||||
|
124,
|
||||||
|
234,
|
||||||
|
54,
|
||||||
|
215,
|
||||||
|
78,
|
||||||
|
0,
|
||||||
|
211,
|
||||||
|
246,
|
||||||
|
222,
|
||||||
|
54,
|
||||||
|
91,
|
||||||
|
53,
|
||||||
|
2,
|
||||||
|
49,
|
||||||
|
40,
|
||||||
|
0,
|
||||||
|
202,
|
||||||
|
188,
|
||||||
|
25,
|
||||||
|
184,
|
||||||
|
121,
|
||||||
|
164,
|
||||||
|
235,
|
||||||
|
189,
|
||||||
|
97,
|
||||||
|
14,
|
||||||
|
0,
|
||||||
|
83,
|
||||||
|
166,
|
||||||
|
88,
|
||||||
|
62,
|
||||||
|
55,
|
||||||
|
49,
|
||||||
|
79,
|
||||||
|
153,
|
||||||
|
136,
|
||||||
|
193,
|
||||||
|
133,
|
||||||
|
206,
|
||||||
|
172,
|
||||||
|
99,
|
||||||
|
207,
|
||||||
|
73,
|
||||||
|
246,
|
||||||
|
216,
|
||||||
|
192,
|
||||||
|
107,
|
||||||
|
50,
|
||||||
|
206,
|
||||||
|
167,
|
||||||
|
242,
|
||||||
|
25,
|
||||||
|
180,
|
||||||
|
63,
|
||||||
|
184,
|
||||||
|
201,
|
||||||
|
61,
|
||||||
|
90,
|
||||||
|
242,
|
||||||
|
223,
|
||||||
|
192,
|
||||||
|
13,
|
||||||
|
140,
|
||||||
|
31,
|
||||||
|
240,
|
||||||
|
112,
|
||||||
|
157,
|
||||||
|
250,
|
||||||
|
90,
|
||||||
|
142,
|
||||||
|
3,
|
||||||
|
40,
|
||||||
|
12,
|
||||||
|
106,
|
||||||
|
63,
|
||||||
|
73,
|
||||||
|
42,
|
||||||
|
79,
|
||||||
|
82,
|
||||||
|
20,
|
||||||
|
41,
|
||||||
|
187,
|
||||||
|
173,
|
||||||
|
23,
|
||||||
|
85,
|
||||||
|
59,
|
||||||
|
253,
|
||||||
|
212,
|
||||||
|
191,
|
||||||
|
109,
|
||||||
|
46
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
20
test/fixtures/go-aes.js
vendored
Normal file
20
test/fixtures/go-aes.js
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
16: {
|
||||||
|
inputs: [
|
||||||
|
[47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47],
|
||||||
|
[24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24],
|
||||||
|
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
|
||||||
|
[25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25],
|
||||||
|
[48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48]
|
||||||
|
],
|
||||||
|
outputs: [
|
||||||
|
[121, 104, 0, 151, 57, 137, 22, 239, 234, 151, 58, 15, 100, 22, 228, 110, 85, 248, 249, 15, 145, 128, 223, 25, 192, 175, 132, 169, 98, 203, 231, 106, 224, 102, 206, 244, 29, 213, 36, 2, 26, 213, 94, 29, 134, 219, 136, 73, 212, 176, 33, 95, 198, 91, 148, 139, 132, 252, 182, 115, 116, 160, 146, 194, 0, 97, 181, 0, 193, 149, 21, 51, 248, 97, 32, 62, 86, 153, 238, 67, 38, 34, 55, 143, 70, 193, 99, 107, 31, 67, 90, 97, 55, 63, 69, 203, 33, 233, 74, 237],
|
||||||
|
[109, 172, 127, 179, 110, 222, 79, 219, 87, 76, 70, 204, 166, 110, 184, 229, 90, 49, 160, 252, 78, 58, 73, 51, 152, 218, 3, 200, 10, 124, 152, 117, 137, 40, 23, 127, 56, 57, 203, 31, 101, 227, 31, 198, 223, 86, 98, 120, 100, 86, 116, 144, 142, 127, 68, 175, 249, 232, 22, 83, 22, 68, 60, 230, 146, 22, 153, 193, 67, 5, 51, 253, 239, 210, 80, 31, 254, 103, 185, 145, 123, 99, 205, 175, 156, 144, 191, 67, 31, 236, 43, 98, 197, 235, 31, 50, 69, 228, 100, 64],
|
||||||
|
[191, 93, 126, 191, 180, 42, 118, 144, 28, 188, 211, 191, 211, 130, 170, 153, 134, 240, 179, 83, 75, 23, 42, 68, 158, 200, 123, 155, 57, 169, 152, 133, 33, 114, 90, 29, 131, 91, 70, 105, 83, 45, 40, 47, 77, 96, 97, 8, 40, 1, 110, 245, 106, 172, 152, 146, 5, 114, 132, 0, 179, 31, 44, 78, 19, 109, 92, 199, 226, 36, 12, 74, 180, 241, 224, 107, 83, 13, 167, 27, 251, 101, 193, 98, 49, 90, 225, 197, 75, 213, 144, 52, 235, 130, 92, 247, 219, 139, 209, 132],
|
||||||
|
[191, 110, 53, 190, 250, 44, 190, 136, 22, 209, 131, 200, 112, 31, 61, 183, 247, 95, 4, 249, 69, 147, 238, 74, 18, 71, 197, 115, 141, 226, 102, 92, 91, 128, 181, 172, 67, 109, 17, 165, 52, 10, 55, 31, 55, 225, 253, 140, 154, 35, 104, 62, 119, 103, 197, 152, 125, 134, 140, 181, 170, 76, 75, 114, 195, 188, 68, 197, 28, 47, 116, 82, 34, 128, 232, 122, 14, 229, 122, 161, 36, 212, 161, 164, 145, 86, 215, 233, 222, 50, 143, 89, 131, 32, 130, 196, 109, 36, 204, 254],
|
||||||
|
[97, 142, 8, 37, 69, 173, 124, 180, 97, 70, 45, 91, 196, 126, 184, 135, 213, 104, 171, 89, 231, 63, 43, 42, 7, 245, 75, 165, 205, 182, 24, 50, 170, 217, 128, 112, 114, 215, 209, 145, 135, 235, 179, 212, 5, 81, 142, 199, 53, 221, 39, 239, 167, 21, 237, 168, 145, 249, 250, 108, 2, 247, 89, 73, 228, 227, 255, 155, 121, 157, 205, 96, 43, 32, 112, 209, 173, 96, 143, 43, 220, 140, 26, 205, 34, 34, 53, 157, 41, 167, 125, 235, 243, 85, 13, 14, 93, 109, 233, 186]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
13
test/fixtures/go-elliptic-key.js
vendored
13
test/fixtures/go-elliptic-key.js
vendored
@@ -1,14 +1,15 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
curve: 'P-256',
|
curve: 'P-256',
|
||||||
curveJs: 'p256',
|
|
||||||
bob: {
|
bob: {
|
||||||
private: [
|
private: Buffer.from([
|
||||||
231, 236, 69, 16, 13, 92, 76, 83, 75, 40, 32, 71, 235, 187, 29, 214, 98, 231, 42, 5, 80, 89, 58, 175, 8, 95, 86, 50, 44, 214, 4, 172
|
181, 217, 162, 151, 225, 36, 53, 253, 107, 66, 27, 27, 232, 72, 0, 0, 103, 167, 84, 62, 203, 91, 97, 137, 131, 193, 230, 126, 98, 242, 216, 170
|
||||||
],
|
]),
|
||||||
public: new Buffer([
|
public: Buffer.from([
|
||||||
4, 160, 169, 215, 85, 152, 11, 209, 69, 105, 17, 51, 49, 83, 214, 171, 157, 73, 165, 85, 28, 196, 161, 234, 87, 149, 139, 76, 123, 37, 174, 194, 67, 167, 18, 34, 164, 35, 171, 164, 238, 141, 199, 206, 86, 130, 183, 88, 63, 121, 110, 150, 229, 10, 213, 176, 181, 1, 98, 20, 246, 85, 212, 200, 229
|
4, 53, 59, 128, 56, 162, 250, 72, 141, 206, 117, 232, 57, 96, 39, 39, 247, 7, 27, 57, 251, 232, 120, 186, 21, 239, 176, 139, 195, 129, 125, 85, 11, 188, 191, 32, 227, 0, 6, 163, 101, 68, 208, 1, 43, 131, 124, 112, 102, 91, 104, 79, 16, 119, 152, 208, 4, 147, 155, 83, 20, 146, 104, 55, 90
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
test/fixtures/go-key-ed25519.js
vendored
Normal file
30
test/fixtures/go-key-ed25519.js
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
// These were generated in a gore (https://github.com/motemen/gore) repl session:
|
||||||
|
//
|
||||||
|
// :import github.com/libp2p/go-libp2p-crypto
|
||||||
|
// :import crypto/rand
|
||||||
|
// priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
|
||||||
|
// pubkeyBytes, err := pub.Bytes()
|
||||||
|
// privkeyBytes, err := priv.Bytes()
|
||||||
|
// data := []byte("hello! and welcome to some awesome crypto primitives")
|
||||||
|
// sig, err := priv.Sign(data)
|
||||||
|
//
|
||||||
|
// :import io/ioutil
|
||||||
|
// ioutil.WriteFile("/tmp/pubkey_go.bin", pubkeyBytes, 0644)
|
||||||
|
// // etc..
|
||||||
|
//
|
||||||
|
// Then loaded into a node repl and dumped to arrays with:
|
||||||
|
//
|
||||||
|
// var pubkey = Array.from(fs.readFileSync('/tmp/pubkey_go.bin'))
|
||||||
|
// console.log(JSON.stringify(pubkey))
|
||||||
|
verify: {
|
||||||
|
privateKey: Buffer.from([8, 1, 18, 96, 201, 208, 1, 110, 176, 16, 230, 37, 66, 184, 149, 252, 78, 56, 206, 136, 2, 38, 118, 152, 226, 197, 117, 200, 54, 189, 156, 218, 184, 7, 118, 57, 233, 49, 221, 97, 164, 158, 241, 129, 73, 166, 225, 255, 193, 118, 22, 84, 55, 15, 249, 168, 225, 180, 198, 191, 14, 75, 187, 243, 150, 91, 232, 37, 233, 49, 221, 97, 164, 158, 241, 129, 73, 166, 225, 255, 193, 118, 22, 84, 55, 15, 249, 168, 225, 180, 198, 191, 14, 75, 187, 243, 150, 91, 232, 37]),
|
||||||
|
publicKey: Buffer.from([8, 1, 18, 32, 233, 49, 221, 97, 164, 158, 241, 129, 73, 166, 225, 255, 193, 118, 22, 84, 55, 15, 249, 168, 225, 180, 198, 191, 14, 75, 187, 243, 150, 91, 232, 37]),
|
||||||
|
data: Buffer.from([104, 101, 108, 108, 111, 33, 32, 97, 110, 100, 32, 119, 101, 108, 99, 111, 109, 101, 32, 116, 111, 32, 115, 111, 109, 101, 32, 97, 119, 101, 115, 111, 109, 101, 32, 99, 114, 121, 112, 116, 111, 32, 112, 114, 105, 109, 105, 116, 105, 118, 101, 115]),
|
||||||
|
signature: Buffer.from([7, 230, 175, 164, 228, 58, 78, 208, 62, 243, 73, 142, 83, 195, 176, 217, 166, 62, 41, 165, 168, 164, 75, 179, 163, 86, 102, 32, 18, 84, 150, 237, 39, 207, 213, 20, 134, 237, 50, 41, 176, 183, 229, 133, 38, 255, 42, 228, 68, 186, 100, 14, 175, 156, 243, 118, 125, 125, 120, 212, 124, 103, 252, 12])
|
||||||
|
}
|
||||||
|
}
|
21
test/fixtures/go-key-rsa.js
vendored
21
test/fixtures/go-key-rsa.js
vendored
@@ -1,20 +1,33 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
private: {
|
private: {
|
||||||
hash: new Buffer([
|
hash: Buffer.from([
|
||||||
18, 32, 168, 125, 165, 65, 34, 157, 209, 4, 24, 158, 80, 196, 125, 86, 103, 0, 228, 145, 109, 252, 153, 7, 189, 9, 16, 37, 239, 36, 48, 78, 214, 212
|
18, 32, 168, 125, 165, 65, 34, 157, 209, 4, 24, 158, 80, 196, 125, 86, 103, 0, 228, 145, 109, 252, 153, 7, 189, 9, 16, 37, 239, 36, 48, 78, 214, 212
|
||||||
]),
|
]),
|
||||||
key: new Buffer([
|
key: Buffer.from([
|
||||||
8, 0, 18, 192, 2, 48, 130, 1, 60, 2, 1, 0, 2, 65, 0, 230, 157, 160, 242, 74, 222, 87, 0, 77, 180, 91, 175, 217, 166, 2, 95, 193, 239, 195, 140, 224, 57, 84, 207, 46, 172, 113, 196, 20, 133, 117, 205, 45, 7, 224, 41, 40, 195, 254, 124, 14, 84, 223, 147, 67, 198, 48, 36, 53, 161, 112, 46, 153, 90, 19, 123, 94, 247, 5, 116, 1, 238, 32, 15, 2, 3, 1, 0, 1, 2, 65, 0, 191, 59, 140, 255, 254, 23, 123, 91, 148, 19, 240, 71, 213, 26, 181, 51, 68, 181, 150, 153, 214, 65, 148, 83, 45, 103, 239, 250, 225, 237, 125, 173, 111, 244, 37, 124, 87, 178, 86, 10, 14, 207, 63, 105, 213, 37, 81, 23, 230, 4, 222, 179, 144, 40, 252, 163, 190, 7, 241, 221, 28, 54, 225, 209, 2, 33, 0, 235, 132, 229, 150, 99, 182, 176, 194, 198, 65, 210, 160, 184, 70, 82, 49, 235, 199, 14, 11, 92, 66, 237, 45, 220, 72, 235, 1, 244, 145, 205, 57, 2, 33, 0, 250, 171, 146, 180, 188, 194, 14, 152, 52, 64, 38, 52, 158, 86, 46, 109, 66, 100, 122, 43, 88, 167, 143, 98, 104, 143, 160, 60, 171, 185, 31, 135, 2, 33, 0, 206, 47, 255, 203, 100, 170, 137, 31, 75, 240, 78, 84, 212, 95, 4, 16, 158, 73, 27, 27, 136, 255, 50, 163, 166, 169, 211, 204, 87, 111, 217, 201, 2, 33, 0, 177, 51, 194, 213, 3, 175, 7, 84, 47, 115, 189, 206, 106, 180, 47, 195, 203, 48, 110, 112, 224, 14, 43, 189, 124, 127, 51, 222, 79, 226, 225, 87, 2, 32, 67, 23, 190, 222, 106, 22, 115, 139, 217, 244, 178, 53, 153, 99, 5, 176, 72, 77, 193, 61, 67, 134, 37, 238, 69, 66, 159, 28, 39, 5, 238, 125
|
8, 0, 18, 192, 2, 48, 130, 1, 60, 2, 1, 0, 2, 65, 0, 230, 157, 160, 242, 74, 222, 87, 0, 77, 180, 91, 175, 217, 166, 2, 95, 193, 239, 195, 140, 224, 57, 84, 207, 46, 172, 113, 196, 20, 133, 117, 205, 45, 7, 224, 41, 40, 195, 254, 124, 14, 84, 223, 147, 67, 198, 48, 36, 53, 161, 112, 46, 153, 90, 19, 123, 94, 247, 5, 116, 1, 238, 32, 15, 2, 3, 1, 0, 1, 2, 65, 0, 191, 59, 140, 255, 254, 23, 123, 91, 148, 19, 240, 71, 213, 26, 181, 51, 68, 181, 150, 153, 214, 65, 148, 83, 45, 103, 239, 250, 225, 237, 125, 173, 111, 244, 37, 124, 87, 178, 86, 10, 14, 207, 63, 105, 213, 37, 81, 23, 230, 4, 222, 179, 144, 40, 252, 163, 190, 7, 241, 221, 28, 54, 225, 209, 2, 33, 0, 235, 132, 229, 150, 99, 182, 176, 194, 198, 65, 210, 160, 184, 70, 82, 49, 235, 199, 14, 11, 92, 66, 237, 45, 220, 72, 235, 1, 244, 145, 205, 57, 2, 33, 0, 250, 171, 146, 180, 188, 194, 14, 152, 52, 64, 38, 52, 158, 86, 46, 109, 66, 100, 122, 43, 88, 167, 143, 98, 104, 143, 160, 60, 171, 185, 31, 135, 2, 33, 0, 206, 47, 255, 203, 100, 170, 137, 31, 75, 240, 78, 84, 212, 95, 4, 16, 158, 73, 27, 27, 136, 255, 50, 163, 166, 169, 211, 204, 87, 111, 217, 201, 2, 33, 0, 177, 51, 194, 213, 3, 175, 7, 84, 47, 115, 189, 206, 106, 180, 47, 195, 203, 48, 110, 112, 224, 14, 43, 189, 124, 127, 51, 222, 79, 226, 225, 87, 2, 32, 67, 23, 190, 222, 106, 22, 115, 139, 217, 244, 178, 53, 153, 99, 5, 176, 72, 77, 193, 61, 67, 134, 37, 238, 69, 66, 159, 28, 39, 5, 238, 125
|
||||||
])
|
])
|
||||||
},
|
},
|
||||||
public: {
|
public: {
|
||||||
hash: new Buffer([
|
hash: Buffer.from([
|
||||||
18, 32, 112, 151, 163, 167, 204, 243, 175, 123, 208, 162, 90, 84, 199, 174, 202, 110, 0, 119, 27, 202, 7, 149, 161, 251, 215, 168, 163, 54, 93, 54, 195, 20
|
18, 32, 112, 151, 163, 167, 204, 243, 175, 123, 208, 162, 90, 84, 199, 174, 202, 110, 0, 119, 27, 202, 7, 149, 161, 251, 215, 168, 163, 54, 93, 54, 195, 20
|
||||||
]),
|
]),
|
||||||
key: new Buffer([
|
key: Buffer.from([
|
||||||
8, 0, 18, 94, 48, 92, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 75, 0, 48, 72, 2, 65, 0, 230, 157, 160, 242, 74, 222, 87, 0, 77, 180, 91, 175, 217, 166, 2, 95, 193, 239, 195, 140, 224, 57, 84, 207, 46, 172, 113, 196, 20, 133, 117, 205, 45, 7, 224, 41, 40, 195, 254, 124, 14, 84, 223, 147, 67, 198, 48, 36, 53, 161, 112, 46, 153, 90, 19, 123, 94, 247, 5, 116, 1, 238, 32, 15, 2, 3, 1, 0, 1
|
8, 0, 18, 94, 48, 92, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 75, 0, 48, 72, 2, 65, 0, 230, 157, 160, 242, 74, 222, 87, 0, 77, 180, 91, 175, 217, 166, 2, 95, 193, 239, 195, 140, 224, 57, 84, 207, 46, 172, 113, 196, 20, 133, 117, 205, 45, 7, 224, 41, 40, 195, 254, 124, 14, 84, 223, 147, 67, 198, 48, 36, 53, 161, 112, 46, 153, 90, 19, 123, 94, 247, 5, 116, 1, 238, 32, 15, 2, 3, 1, 0, 1
|
||||||
])
|
])
|
||||||
|
},
|
||||||
|
verify: {
|
||||||
|
signature: Buffer.from([
|
||||||
|
3, 116, 81, 57, 91, 194, 7, 1, 230, 236, 229, 142, 36, 209, 208, 107, 47, 52, 164, 236, 139, 35, 155, 97, 43, 64, 145, 91, 19, 218, 149, 63, 99, 164, 191, 110, 145, 37, 18, 7, 98, 112, 144, 35, 29, 186, 169, 150, 165, 88, 145, 170, 197, 110, 42, 163, 188, 10, 42, 63, 34, 93, 91, 94, 199, 110, 10, 82, 238, 80, 93, 93, 77, 130, 22, 216, 229, 172, 36, 229, 82, 162, 20, 78, 19, 46, 82, 243, 43, 80, 115, 125, 145, 231, 194, 224, 30, 187, 55, 228, 74, 52, 203, 191, 254, 148, 136, 218, 62, 147, 171, 130, 251, 181, 105, 29, 238, 207, 197, 249, 61, 105, 202, 172, 160, 174, 43, 124, 115, 130, 169, 30, 76, 41, 52, 200, 2, 26, 53, 190, 43, 20, 203, 10, 217, 250, 47, 102, 92, 103, 197, 22, 108, 184, 74, 218, 82, 202, 180, 98, 13, 114, 12, 92, 1, 139, 150, 170, 8, 92, 32, 116, 168, 219, 157, 162, 28, 77, 29, 29, 74, 136, 144, 49, 173, 245, 253, 76, 167, 200, 169, 163, 7, 49, 133, 120, 99, 191, 53, 10, 66, 26, 234, 240, 139, 235, 134, 30, 55, 248, 150, 100, 242, 150, 159, 198, 44, 78, 150, 7, 133, 139, 59, 76, 3, 225, 94, 13, 89, 122, 34, 95, 95, 107, 74, 169, 171, 169, 222, 25, 191, 182, 148, 116, 66, 67, 102, 12, 193, 217, 247, 243, 148, 233, 161, 157
|
||||||
|
]),
|
||||||
|
data: Buffer.from([
|
||||||
|
10, 16, 27, 128, 228, 220, 147, 176, 53, 105, 175, 171, 32, 213, 35, 236, 203, 60, 18, 171, 2, 8, 0, 18, 166, 2, 48, 130, 1, 34, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 130, 1, 15, 0, 48, 130, 1, 10, 2, 130, 1, 1, 0, 181, 113, 138, 108, 208, 103, 166, 102, 37, 36, 204, 250, 228, 165, 44, 64, 176, 210, 205, 141, 241, 55, 200, 110, 98, 68, 85, 199, 254, 19, 86, 204, 63, 250, 167, 38, 59, 249, 146, 228, 73, 171, 63, 18, 96, 104, 191, 137, 186, 244, 255, 90, 16, 119, 195, 52, 177, 213, 254, 187, 174, 84, 174, 173, 12, 236, 53, 234, 3, 209, 82, 37, 78, 111, 214, 135, 76, 195, 9, 242, 134, 188, 153, 84, 139, 231, 51, 146, 177, 60, 12, 25, 158, 91, 215, 152, 7, 0, 84, 35, 36, 230, 227, 67, 198, 72, 50, 110, 37, 209, 98, 193, 65, 93, 173, 199, 4, 198, 102, 99, 148, 144, 224, 217, 114, 53, 144, 245, 251, 114, 211, 20, 82, 163, 123, 75, 16, 192, 106, 213, 128, 2, 11, 200, 203, 84, 41, 199, 224, 155, 171, 217, 64, 109, 116, 188, 151, 183, 173, 52, 205, 164, 93, 13, 251, 65, 182, 160, 154, 185, 239, 33, 184, 84, 159, 105, 101, 173, 194, 251, 123, 84, 92, 66, 61, 180, 45, 104, 162, 224, 214, 233, 64, 220, 165, 2, 104, 116, 150, 2, 234, 203, 112, 21, 124, 23, 48, 66, 30, 63, 30, 36, 246, 135, 203, 218, 115, 22, 189, 39, 39, 125, 205, 65, 222, 220, 77, 18, 84, 121, 161, 153, 125, 25, 139, 137, 170, 239, 150, 106, 119, 168, 216, 140, 113, 121, 26, 53, 118, 110, 53, 192, 244, 252, 145, 85, 2, 3, 1, 0, 1, 26, 17, 80, 45, 50, 53, 54, 44, 80, 45, 51, 56, 52, 44, 80, 45, 53, 50, 49, 34, 24, 65, 69, 83, 45, 50, 53, 54, 44, 65, 69, 83, 45, 49, 50, 56, 44, 66, 108, 111, 119, 102, 105, 115, 104, 42, 13, 83, 72, 65, 50, 53, 54, 44, 83, 72, 65, 53, 49, 50, 10, 16, 220, 83, 240, 105, 6, 203, 78, 83, 210, 115, 6, 106, 98, 82, 1, 161, 18, 171, 2, 8, 0, 18, 166, 2, 48, 130, 1, 34, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 130, 1, 15, 0, 48, 130, 1, 10, 2, 130, 1, 1, 0, 185, 234, 19, 191, 164, 33, 65, 94, 87, 42, 74, 83, 224, 25, 142, 44, 26, 7, 92, 242, 189, 42, 170, 197, 178, 92, 45, 240, 107, 141, 128, 59, 122, 252, 48, 140, 4, 85, 85, 203, 3, 197, 8, 127, 120, 98, 44, 169, 135, 196, 70, 137, 117, 180, 177, 134, 170, 35, 165, 88, 105, 30, 114, 138, 11, 96, 68, 99, 18, 149, 223, 166, 105, 12, 176, 77, 48, 214, 22, 236, 17, 154, 213, 209, 158, 169, 202, 5, 100, 210, 83, 90, 201, 38, 205, 246, 231, 106, 63, 86, 222, 143, 157, 173, 62, 4, 85, 232, 20, 188, 6, 209, 186, 132, 192, 117, 146, 181, 233, 26, 0, 240, 138, 206, 91, 170, 114, 137, 217, 132, 139, 242, 144, 213, 103, 101, 190, 146, 188, 250, 188, 134, 255, 70, 125, 78, 65, 136, 239, 190, 206, 139, 155, 140, 163, 233, 170, 247, 205, 87, 209, 19, 29, 173, 10, 147, 43, 28, 90, 46, 6, 197, 217, 186, 66, 68, 126, 76, 64, 184, 8, 170, 23, 79, 243, 223, 119, 133, 118, 50, 226, 44, 246, 176, 10, 161, 219, 83, 54, 68, 248, 5, 14, 177, 114, 54, 63, 11, 71, 136, 142, 56, 151, 123, 230, 61, 80, 15, 180, 42, 49, 220, 148, 99, 231, 20, 230, 220, 85, 207, 187, 37, 210, 137, 171, 125, 71, 14, 53, 100, 91, 83, 209, 50, 132, 165, 253, 25, 161, 5, 97, 164, 163, 83, 95, 53, 2, 3, 1, 0, 1, 26, 17, 80, 45, 50, 53, 54, 44, 80, 45, 51, 56, 52, 44, 80, 45, 53, 50, 49, 34, 15, 65, 69, 83, 45, 50, 53, 54, 44, 65, 69, 83, 45, 49, 50, 56, 42, 13, 83, 72, 65, 50, 53, 54, 44, 83, 72, 65, 53, 49, 50, 4, 97, 54, 203, 112, 136, 34, 231, 162, 19, 154, 131, 27, 105, 26, 121, 238, 120, 25, 203, 66, 232, 53, 198, 20, 19, 96, 119, 218, 90, 64, 170, 3, 132, 116, 1, 87, 116, 232, 165, 161, 198, 117, 167, 60, 145, 1, 253, 108, 50, 150, 117, 8, 140, 133, 48, 30, 236, 36, 84, 186, 22, 144, 87, 101
|
||||||
|
]),
|
||||||
|
publicKey: Buffer.from([
|
||||||
|
8, 0, 18, 166, 2, 48, 130, 1, 34, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 130, 1, 15, 0, 48, 130, 1, 10, 2, 130, 1, 1, 0, 181, 113, 138, 108, 208, 103, 166, 102, 37, 36, 204, 250, 228, 165, 44, 64, 176, 210, 205, 141, 241, 55, 200, 110, 98, 68, 85, 199, 254, 19, 86, 204, 63, 250, 167, 38, 59, 249, 146, 228, 73, 171, 63, 18, 96, 104, 191, 137, 186, 244, 255, 90, 16, 119, 195, 52, 177, 213, 254, 187, 174, 84, 174, 173, 12, 236, 53, 234, 3, 209, 82, 37, 78, 111, 214, 135, 76, 195, 9, 242, 134, 188, 153, 84, 139, 231, 51, 146, 177, 60, 12, 25, 158, 91, 215, 152, 7, 0, 84, 35, 36, 230, 227, 67, 198, 72, 50, 110, 37, 209, 98, 193, 65, 93, 173, 199, 4, 198, 102, 99, 148, 144, 224, 217, 114, 53, 144, 245, 251, 114, 211, 20, 82, 163, 123, 75, 16, 192, 106, 213, 128, 2, 11, 200, 203, 84, 41, 199, 224, 155, 171, 217, 64, 109, 116, 188, 151, 183, 173, 52, 205, 164, 93, 13, 251, 65, 182, 160, 154, 185, 239, 33, 184, 84, 159, 105, 101, 173, 194, 251, 123, 84, 92, 66, 61, 180, 45, 104, 162, 224, 214, 233, 64, 220, 165, 2, 104, 116, 150, 2, 234, 203, 112, 21, 124, 23, 48, 66, 30, 63, 30, 36, 246, 135, 203, 218, 115, 22, 189, 39, 39, 125, 205, 65, 222, 220, 77, 18, 84, 121, 161, 153, 125, 25, 139, 137, 170, 239, 150, 106, 119, 168, 216, 140, 113, 121, 26, 53, 118, 110, 53, 192, 244, 252, 145, 85, 2, 3, 1, 0, 1
|
||||||
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
16
test/fixtures/go-stretch-key.js
vendored
16
test/fixtures/go-stretch-key.js
vendored
@@ -1,30 +1,32 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
module.exports = [{
|
module.exports = [{
|
||||||
cipher: 'AES-256',
|
cipher: 'AES-256',
|
||||||
hash: 'SHA256',
|
hash: 'SHA256',
|
||||||
secret: new Buffer([
|
secret: Buffer.from([
|
||||||
195, 191, 209, 165, 209, 201, 127, 122, 136, 111, 31, 66, 111, 68, 38, 155, 216, 204, 46, 181, 200, 188, 170, 204, 104, 74, 239, 251, 173, 114, 222, 234
|
195, 191, 209, 165, 209, 201, 127, 122, 136, 111, 31, 66, 111, 68, 38, 155, 216, 204, 46, 181, 200, 188, 170, 204, 104, 74, 239, 251, 173, 114, 222, 234
|
||||||
]),
|
]),
|
||||||
k1: {
|
k1: {
|
||||||
iv: new Buffer([
|
iv: Buffer.from([
|
||||||
208, 132, 203, 169, 253, 52, 40, 83, 161, 91, 17, 71, 33, 136, 67, 96
|
208, 132, 203, 169, 253, 52, 40, 83, 161, 91, 17, 71, 33, 136, 67, 96
|
||||||
]),
|
]),
|
||||||
cipherKey: new Buffer([
|
cipherKey: Buffer.from([
|
||||||
156, 48, 241, 157, 92, 248, 153, 186, 114, 127, 195, 114, 106, 104, 215, 133, 35, 11, 131, 137, 123, 70, 74, 26, 15, 60, 189, 32, 67, 221, 115, 137
|
156, 48, 241, 157, 92, 248, 153, 186, 114, 127, 195, 114, 106, 104, 215, 133, 35, 11, 131, 137, 123, 70, 74, 26, 15, 60, 189, 32, 67, 221, 115, 137
|
||||||
]),
|
]),
|
||||||
macKey: new Buffer([
|
macKey: Buffer.from([
|
||||||
6, 179, 91, 245, 224, 56, 153, 120, 77, 140, 29, 5, 15, 213, 187, 65, 137, 230, 202, 120
|
6, 179, 91, 245, 224, 56, 153, 120, 77, 140, 29, 5, 15, 213, 187, 65, 137, 230, 202, 120
|
||||||
])
|
])
|
||||||
},
|
},
|
||||||
k2: {
|
k2: {
|
||||||
iv: new Buffer([
|
iv: Buffer.from([
|
||||||
236, 17, 34, 141, 90, 106, 197, 56, 197, 184, 157, 135, 91, 88, 112, 19
|
236, 17, 34, 141, 90, 106, 197, 56, 197, 184, 157, 135, 91, 88, 112, 19
|
||||||
]),
|
]),
|
||||||
cipherKey: new Buffer([
|
cipherKey: Buffer.from([
|
||||||
151, 145, 195, 219, 76, 195, 102, 109, 187, 231, 100, 150, 132, 245, 251, 130, 254, 37, 178, 55, 227, 34, 114, 39, 238, 34, 2, 193, 107, 130, 32, 87
|
151, 145, 195, 219, 76, 195, 102, 109, 187, 231, 100, 150, 132, 245, 251, 130, 254, 37, 178, 55, 227, 34, 114, 39, 238, 34, 2, 193, 107, 130, 32, 87
|
||||||
]),
|
]),
|
||||||
macKey: new Buffer([
|
macKey: Buffer.from([
|
||||||
3, 229, 77, 212, 241, 217, 23, 113, 220, 126, 38, 255, 18, 117, 108, 205, 198, 89, 1, 236
|
3, 229, 77, 212, 241, 217, 23, 113, 220, 126, 38, 255, 18, 117, 108, 205, 198, 89, 1, 236
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
11
test/fixtures/secp256k1.js
vendored
Normal file
11
test/fixtures/secp256k1.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
|
||||||
|
// protobuf marshaled key pair generated with libp2p-crypto-secp256k1
|
||||||
|
// and marshaled with libp2p-crypto.marshalPublicKey / marshalPrivateKey
|
||||||
|
pbmPrivateKey: Buffer.from('08021220e0600103010000000100000000000000be1dc82c2e000000e8d6030301000000', 'hex'),
|
||||||
|
pbmPublicKey: Buffer.from('0802122103a9a7272a726fa083abf31ba44037f8347fbc5e5d3113d62a7c6bc26752fd8ee1', 'hex')
|
||||||
|
}
|
26
test/hmac.spec.js
Normal file
26
test/hmac.spec.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
const expect = require('chai').expect
|
||||||
|
|
||||||
|
const crypto = require('../src')
|
||||||
|
|
||||||
|
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
||||||
|
|
||||||
|
describe('HMAC', () => {
|
||||||
|
hashes.forEach((hash) => {
|
||||||
|
it(`${hash} - sign and verify`, (done) => {
|
||||||
|
crypto.hmac.create(hash, Buffer.from('secret'), (err, hmac) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
|
||||||
|
hmac.digest(Buffer.from('hello world'), (err, sig) => {
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(sig).to.have.length(hmac.length)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
@@ -8,53 +9,91 @@ const fixtures = require('./fixtures/go-key-rsa')
|
|||||||
|
|
||||||
describe('libp2p-crypto', () => {
|
describe('libp2p-crypto', () => {
|
||||||
let key
|
let key
|
||||||
before(() => {
|
before((done) => {
|
||||||
key = crypto.generateKeyPair('RSA', 2048)
|
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
key = _key
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('marshalPublicKey and unmarshalPublicKey', () => {
|
it('marshalPublicKey and unmarshalPublicKey', () => {
|
||||||
const key2 = crypto.unmarshalPublicKey(crypto.marshalPublicKey(key.public))
|
const key2 = crypto.unmarshalPublicKey(crypto.marshalPublicKey(key.public))
|
||||||
|
|
||||||
expect(key2.equals(key.public)).to.be.eql(true)
|
expect(key2.equals(key.public)).to.be.eql(true)
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
crypto.marshalPublicKey(key.public, 'invalid-key-type')
|
||||||
|
}).to.throw()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('marshalPrivateKey and unmarshalPrivateKey', () => {
|
it('marshalPrivateKey and unmarshalPrivateKey', (done) => {
|
||||||
const key2 = crypto.unmarshalPrivateKey(crypto.marshalPrivateKey(key))
|
expect(() => {
|
||||||
|
crypto.marshalPrivateKey(key, 'invalid-key-type')
|
||||||
|
}).to.throw()
|
||||||
|
|
||||||
expect(key2.equals(key)).to.be.eql(true)
|
crypto.unmarshalPrivateKey(crypto.marshalPrivateKey(key), (err, key2) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(key2.equals(key)).to.be.eql(true)
|
||||||
|
expect(key2.public.equals(key.public)).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// marshalled keys seem to be slightly different
|
||||||
|
// unsure as to if this is just a difference in encoding
|
||||||
|
// or a bug
|
||||||
describe('go interop', () => {
|
describe('go interop', () => {
|
||||||
it('unmarshals private key', () => {
|
it('unmarshals private key', (done) => {
|
||||||
const key = crypto.unmarshalPrivateKey(fixtures.private.key)
|
crypto.unmarshalPrivateKey(fixtures.private.key, (err, key) => {
|
||||||
const hash = fixtures.private.hash
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
const hash = fixtures.private.hash
|
||||||
|
expect(fixtures.private.key).to.be.eql(key.bytes)
|
||||||
|
|
||||||
expect(
|
key.hash((err, digest) => {
|
||||||
key.hash()
|
if (err) {
|
||||||
).to.be.eql(
|
return done(err)
|
||||||
hash
|
}
|
||||||
)
|
|
||||||
|
expect(digest).to.be.eql(hash)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('unmarshals public key', () => {
|
it('unmarshals public key', (done) => {
|
||||||
const key = crypto.unmarshalPublicKey(fixtures.public.key)
|
const key = crypto.unmarshalPublicKey(fixtures.public.key)
|
||||||
const hash = fixtures.public.hash
|
const hash = fixtures.public.hash
|
||||||
|
|
||||||
expect(
|
expect(crypto.marshalPublicKey(key)).to.be.eql(fixtures.public.key)
|
||||||
key.hash()
|
|
||||||
).to.be.eql(
|
key.hash((err, digest) => {
|
||||||
hash
|
if (err) {
|
||||||
)
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(digest).to.be.eql(hash)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('unmarshal -> marshal, private key', () => {
|
it('unmarshal -> marshal, private key', (done) => {
|
||||||
const key = crypto.unmarshalPrivateKey(fixtures.private.key)
|
crypto.unmarshalPrivateKey(fixtures.private.key, (err, key) => {
|
||||||
const marshalled = crypto.marshalPrivateKey(key)
|
if (err) {
|
||||||
expect(
|
return done(err)
|
||||||
fixtures.private.key.equals(marshalled)
|
}
|
||||||
).to.be.eql(
|
|
||||||
true
|
const marshalled = crypto.marshalPrivateKey(key)
|
||||||
)
|
expect(marshalled).to.be.eql(fixtures.private.key)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('unmarshal -> marshal, public key', () => {
|
it('unmarshal -> marshal, public key', () => {
|
||||||
@@ -67,4 +106,19 @@ describe('libp2p-crypto', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('randomBytes', () => {
|
||||||
|
it('throws with no number passed', () => {
|
||||||
|
expect(() => {
|
||||||
|
crypto.randomBytes()
|
||||||
|
}).to.throw()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('generates different random things', () => {
|
||||||
|
const buf1 = crypto.randomBytes(10)
|
||||||
|
expect(buf1.length).to.equal(10)
|
||||||
|
const buf2 = crypto.randomBytes(10)
|
||||||
|
expect(buf1).to.not.eql(buf2)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@@ -11,15 +11,38 @@ describe('keyStretcher', () => {
|
|||||||
describe('generate', () => {
|
describe('generate', () => {
|
||||||
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
const ciphers = ['AES-128', 'AES-256', 'Blowfish']
|
||||||
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
const hashes = ['SHA1', 'SHA256', 'SHA512']
|
||||||
const res = crypto.generateEphemeralKeyPair('P-256')
|
let res
|
||||||
const secret = res.genSharedKey(res.key)
|
let secret
|
||||||
|
|
||||||
|
before((done) => {
|
||||||
|
crypto.generateEphemeralKeyPair('P-256', (err, _res) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
res = _res
|
||||||
|
res.genSharedKey(res.key, (err, _secret) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
secret = _secret
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
ciphers.forEach((cipher) => {
|
ciphers.forEach((cipher) => {
|
||||||
hashes.forEach((hash) => {
|
hashes.forEach((hash) => {
|
||||||
it(`${cipher} - ${hash}`, () => {
|
it(`${cipher} - ${hash}`, (done) => {
|
||||||
const keys = crypto.keyStretcher(cipher, hash, secret)
|
crypto.keyStretcher(cipher, hash, secret, (err, keys) => {
|
||||||
expect(keys.k1).to.exist
|
if (err) {
|
||||||
expect(keys.k2).to.exist
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(keys.k1).to.exist
|
||||||
|
expect(keys.k2).to.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -27,19 +50,24 @@ describe('keyStretcher', () => {
|
|||||||
|
|
||||||
describe('go interop', () => {
|
describe('go interop', () => {
|
||||||
fixtures.forEach((test) => {
|
fixtures.forEach((test) => {
|
||||||
it(`${test.cipher} - ${test.hash}`, () => {
|
it(`${test.cipher} - ${test.hash}`, (done) => {
|
||||||
const cipher = test.cipher
|
const cipher = test.cipher
|
||||||
const hash = test.hash
|
const hash = test.hash
|
||||||
const secret = test.secret
|
const secret = test.secret
|
||||||
const keys = crypto.keyStretcher(cipher, hash, secret)
|
crypto.keyStretcher(cipher, hash, secret, (err, keys) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
expect(keys.k1.iv).to.be.eql(test.k1.iv)
|
expect(keys.k1.iv).to.be.eql(test.k1.iv)
|
||||||
expect(keys.k1.cipherKey).to.be.eql(test.k1.cipherKey)
|
expect(keys.k1.cipherKey).to.be.eql(test.k1.cipherKey)
|
||||||
expect(keys.k1.macKey).to.be.eql(test.k1.macKey)
|
expect(keys.k1.macKey).to.be.eql(test.k1.macKey)
|
||||||
|
|
||||||
expect(keys.k2.iv).to.be.eql(test.k2.iv)
|
expect(keys.k2.iv).to.be.eql(test.k2.iv)
|
||||||
expect(keys.k2.cipherKey).to.be.eql(test.k2.cipherKey)
|
expect(keys.k2.cipherKey).to.be.eql(test.k2.cipherKey)
|
||||||
expect(keys.k2.macKey).to.be.eql(test.k2.macKey)
|
expect(keys.k2.macKey).to.be.eql(test.k2.macKey)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
191
test/rsa.spec.js
191
test/rsa.spec.js
@@ -2,63 +2,84 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const expect = require('chai').expect
|
const expect = require('chai').expect
|
||||||
|
const Buffer = require('safe-buffer').Buffer
|
||||||
|
|
||||||
const crypto = require('../src')
|
const crypto = require('../src')
|
||||||
const rsa = crypto.keys.rsa
|
const rsa = crypto.keys.rsa
|
||||||
|
const fixtures = require('./fixtures/go-key-rsa')
|
||||||
|
|
||||||
describe('RSA', () => {
|
describe('RSA', () => {
|
||||||
let key
|
let key
|
||||||
before(() => {
|
before((done) => {
|
||||||
key = crypto.generateKeyPair('RSA', 2048)
|
crypto.generateKeyPair('RSA', 2048, (err, _key) => {
|
||||||
|
if (err) return done(err)
|
||||||
|
key = _key
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('generates a valid key', () => {
|
it('generates a valid key', (done) => {
|
||||||
expect(
|
expect(
|
||||||
key
|
key
|
||||||
).to.be.an.instanceof(
|
).to.be.an.instanceof(
|
||||||
rsa.RsaPrivateKey
|
rsa.RsaPrivateKey
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
key.hash((err, digest) => {
|
||||||
key.hash()
|
if (err) {
|
||||||
).to.have.length(
|
return done(err)
|
||||||
34
|
}
|
||||||
)
|
|
||||||
|
expect(digest).to.have.length(34)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('signs', () => {
|
it('signs', (done) => {
|
||||||
const pk = key.public
|
|
||||||
const text = key.genSecret()
|
const text = key.genSecret()
|
||||||
const sig = key.sign(text)
|
|
||||||
|
|
||||||
expect(
|
key.sign(text, (err, sig) => {
|
||||||
pk.verify(text, sig)
|
if (err) {
|
||||||
).to.be.eql(
|
return done(err)
|
||||||
true
|
}
|
||||||
)
|
|
||||||
|
key.public.verify(text, sig, (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(res).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('encoding', () => {
|
it('encoding', (done) => {
|
||||||
const keyMarshal = key.marshal()
|
const keyMarshal = key.marshal()
|
||||||
const key2 = rsa.unmarshalRsaPrivateKey(keyMarshal)
|
rsa.unmarshalRsaPrivateKey(keyMarshal, (err, key2) => {
|
||||||
const keyMarshal2 = key2.marshal()
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
const keyMarshal2 = key2.marshal()
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
keyMarshal
|
keyMarshal
|
||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
keyMarshal2
|
keyMarshal2
|
||||||
)
|
)
|
||||||
|
|
||||||
const pk = key.public
|
const pk = key.public
|
||||||
const pkMarshal = pk.marshal()
|
const pkMarshal = pk.marshal()
|
||||||
const pk2 = rsa.unmarshalRsaPublicKey(pkMarshal)
|
const pk2 = rsa.unmarshalRsaPublicKey(pkMarshal)
|
||||||
const pkMarshal2 = pk2.marshal()
|
const pkMarshal2 = pk2.marshal()
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
pkMarshal
|
pkMarshal
|
||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
pkMarshal2
|
pkMarshal2
|
||||||
)
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('key equals', () => {
|
describe('key equals', () => {
|
||||||
@@ -76,54 +97,82 @@ describe('RSA', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('not equals other key', () => {
|
it('not equals other key', (done) => {
|
||||||
const key2 = crypto.generateKeyPair('RSA', 2048)
|
crypto.generateKeyPair('RSA', 2048, (err, key2) => {
|
||||||
|
if (err) return done(err)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
key.equals(key2)
|
key.equals(key2)
|
||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
key2.equals(key)
|
key2.equals(key)
|
||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
key.public.equals(key2.public)
|
key.public.equals(key2.public)
|
||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
key2.public.equals(key.public)
|
key2.public.equals(key.public)
|
||||||
).to.be.eql(
|
).to.be.eql(
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sign and verify', () => {
|
it('sign and verify', (done) => {
|
||||||
const data = new Buffer('hello world')
|
const data = Buffer.from('hello world')
|
||||||
const sig = key.sign(data)
|
key.sign(data, (err, sig) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
expect(
|
key.public.verify(data, sig, (err, valid) => {
|
||||||
key.public.verify(data, sig)
|
if (err) {
|
||||||
).to.be.eql(
|
return done(err)
|
||||||
true
|
}
|
||||||
)
|
expect(valid).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('does fails to verify for different data', () => {
|
it('fails to verify for different data', (done) => {
|
||||||
const data = new Buffer('hello world')
|
const data = Buffer.from('hello world')
|
||||||
const sig = key.sign(data)
|
key.sign(data, (err, sig) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
expect(
|
key.public.verify(Buffer.from('hello'), sig, (err, valid) => {
|
||||||
key.public.verify(new Buffer('hello'), sig)
|
if (err) {
|
||||||
).to.be.eql(
|
return done(err)
|
||||||
false
|
}
|
||||||
)
|
expect(valid).to.be.eql(false)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('go interop', () => {
|
||||||
|
it('verifies with data from go', (done) => {
|
||||||
|
const key = crypto.unmarshalPublicKey(fixtures.verify.publicKey)
|
||||||
|
|
||||||
|
key.verify(fixtures.verify.data, fixtures.verify.signature, (err, ok) => {
|
||||||
|
if (err) throw err
|
||||||
|
expect(err).to.not.exist
|
||||||
|
expect(ok).to.be.eql(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
104
test/secp256k1.spec.js
Normal file
104
test/secp256k1.spec.js
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const fixtures = require('./fixtures/secp256k1')
|
||||||
|
const crypto = require('../src')
|
||||||
|
|
||||||
|
const mockPublicKey = {
|
||||||
|
bytes: fixtures.pbmPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
const mockPrivateKey = {
|
||||||
|
bytes: fixtures.pbmPrivateKey,
|
||||||
|
public: mockPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
const mockSecp256k1Module = {
|
||||||
|
generateKeyPair (bits, callback) {
|
||||||
|
callback(null, mockPrivateKey)
|
||||||
|
},
|
||||||
|
|
||||||
|
unmarshalSecp256k1PrivateKey (buf, callback) {
|
||||||
|
callback(null, mockPrivateKey)
|
||||||
|
},
|
||||||
|
|
||||||
|
unmarshalSecp256k1PublicKey (buf) {
|
||||||
|
return mockPublicKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('with libp2p-crypto-secp256k1 module present', () => {
|
||||||
|
let key
|
||||||
|
|
||||||
|
before((done) => {
|
||||||
|
crypto.keys['secp256k1'] = mockSecp256k1Module
|
||||||
|
crypto.generateKeyPair('secp256k1', 256, (err, _key) => {
|
||||||
|
if (err) return done(err)
|
||||||
|
key = _key
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
after((done) => {
|
||||||
|
delete crypto.keys['secp256k1']
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('generates a valid key', (done) => {
|
||||||
|
expect(
|
||||||
|
key
|
||||||
|
).to.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('protobuf encoding', (done) => {
|
||||||
|
const keyMarshal = crypto.marshalPrivateKey(key)
|
||||||
|
crypto.unmarshalPrivateKey(keyMarshal, (err, key2) => {
|
||||||
|
if (err) return done(err)
|
||||||
|
const keyMarshal2 = crypto.marshalPrivateKey(key2)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
keyMarshal
|
||||||
|
).to.be.eql(
|
||||||
|
keyMarshal2
|
||||||
|
)
|
||||||
|
|
||||||
|
const pk = key.public
|
||||||
|
const pkMarshal = crypto.marshalPublicKey(pk)
|
||||||
|
const pk2 = crypto.unmarshalPublicKey(pkMarshal)
|
||||||
|
const pkMarshal2 = crypto.marshalPublicKey(pk2)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
pkMarshal
|
||||||
|
).to.be.eql(
|
||||||
|
pkMarshal2
|
||||||
|
)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('without libp2p-crypto-secp256k1 module present', () => {
|
||||||
|
it('fails to generate a secp256k1 key', (done) => {
|
||||||
|
crypto.generateKeyPair('secp256k1', 256, (err, key) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
expect(key).to.not.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails to unmarshal a secp256k1 private key', (done) => {
|
||||||
|
crypto.unmarshalPrivateKey(fixtures.pbmPrivateKey, (err, key) => {
|
||||||
|
expect(err).to.exist
|
||||||
|
expect(key).to.not.exist
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails to unmarshal a secp256k1 public key', () => {
|
||||||
|
expect(() => {
|
||||||
|
crypto.unmarshalPublicKey(fixtures.pbmPublicKey)
|
||||||
|
}).to.throw(Error)
|
||||||
|
})
|
||||||
|
})
|
21
test/util.spec.js
Normal file
21
test/util.spec.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
|
/* eslint-env mocha */
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const expect = require('chai').expect
|
||||||
|
const util = require('../src/crypto/util')
|
||||||
|
const BN = require('bn.js')
|
||||||
|
|
||||||
|
describe('Util', () => {
|
||||||
|
let bn
|
||||||
|
|
||||||
|
before((done) => {
|
||||||
|
bn = new BN('dead', 16)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('toBase64', (done) => {
|
||||||
|
expect(util.toBase64(bn)).to.be.eql('3q0')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
29552
vendor/forge.bundle.js
vendored
29552
vendor/forge.bundle.js
vendored
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user