mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-07-17 09:21:59 +00:00
Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
bf9b532067 | ||
|
5296f8a42f | ||
|
3a91ae2ed8 | ||
|
e7c11a8e01 | ||
|
9bb96dc2bf | ||
|
957fdd37e9 | ||
|
83257bc4bd | ||
|
cb7fae7fcc | ||
|
7669847c17 | ||
|
dc2793f138 | ||
|
e0b916ace9 | ||
|
0a71af7b89 | ||
|
46adafb207 | ||
|
9e977c7d44 | ||
|
f20267b962 | ||
|
1b6a070fa8 | ||
|
1471e07bf9 | ||
|
bc554d1407 | ||
|
83d2d52205 | ||
|
301c779f49 | ||
|
d552fd423a |
1
.eslintignore
Normal file
1
.eslintignore
Normal file
@@ -0,0 +1 @@
|
||||
src/keys/keys.proto.js
|
12
.gitignore
vendored
12
.gitignore
vendored
@@ -1,6 +1,10 @@
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
|
||||
**/node_modules/
|
||||
**/*.log
|
||||
test/repo-tests*
|
||||
**/bundle.js
|
||||
|
||||
# Logs
|
||||
logs
|
||||
@@ -31,4 +35,12 @@ build
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
lib
|
||||
dist
|
||||
test/test-data/go-ipfs-repo/LOCK
|
||||
test/test-data/go-ipfs-repo/LOG
|
||||
test/test-data/go-ipfs-repo/LOG.old
|
||||
|
||||
# while testing npm5
|
||||
package-lock.json
|
||||
yarn.lock
|
16
.travis.yml
16
.travis.yml
@@ -3,29 +3,21 @@ language: node_js
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- node_js: 4
|
||||
env: CXX=g++-4.8
|
||||
- node_js: 6
|
||||
env: CXX=g++-4.8
|
||||
- node_js: stable
|
||||
- node_js: 8
|
||||
env: CXX=g++-4.8
|
||||
|
||||
# Make sure we have new NPM.
|
||||
before_install:
|
||||
- npm install -g npm@4
|
||||
# - node_js: stable
|
||||
# env: CXX=g++-4.8
|
||||
|
||||
script:
|
||||
- npm run lint
|
||||
- npm test
|
||||
- npm run coverage
|
||||
- npm run test
|
||||
|
||||
before_script:
|
||||
- export DISPLAY=:99.0
|
||||
- sh -e /etc/init.d/xvfb start
|
||||
|
||||
after_success:
|
||||
- npm run coverage-publish
|
||||
|
||||
addons:
|
||||
firefox: 'latest'
|
||||
apt:
|
||||
|
61
CHANGELOG.md
61
CHANGELOG.md
@@ -1,3 +1,64 @@
|
||||
<a name="0.10.3"></a>
|
||||
## [0.10.3](https://github.com/libp2p/js-libp2p-crypto/compare/v0.10.2...v0.10.3) (2017-09-07)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* switch protocol-buffers to protons ([#110](https://github.com/libp2p/js-libp2p-crypto/issues/110)) ([3a91ae2](https://github.com/libp2p/js-libp2p-crypto/commit/3a91ae2))
|
||||
|
||||
|
||||
|
||||
<a name="0.10.2"></a>
|
||||
## [0.10.2](https://github.com/libp2p/js-libp2p-crypto/compare/v0.10.1...v0.10.2) (2017-09-06)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* use regular protocol-buffers until protobufjs is fixed ([#109](https://github.com/libp2p/js-libp2p-crypto/issues/109)) ([957fdd3](https://github.com/libp2p/js-libp2p-crypto/commit/957fdd3))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* **deps:** upgrade to aegir@12 and browserify-aes@1.0.8 ([83257bc](https://github.com/libp2p/js-libp2p-crypto/commit/83257bc))
|
||||
|
||||
|
||||
|
||||
<a name="0.10.1"></a>
|
||||
## [0.10.1](https://github.com/libp2p/js-libp2p-crypto/compare/v0.10.0...v0.10.1) (2017-09-05)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* switch to protobufjs ([#107](https://github.com/libp2p/js-libp2p-crypto/issues/107)) ([dc2793f](https://github.com/libp2p/js-libp2p-crypto/commit/dc2793f))
|
||||
|
||||
|
||||
|
||||
<a name="0.10.0"></a>
|
||||
# [0.10.0](https://github.com/libp2p/js-libp2p-crypto/compare/v0.9.4...v0.10.0) (2017-09-03)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* p2p addrs situation ([#106](https://github.com/libp2p/js-libp2p-crypto/issues/106)) ([9e977c7](https://github.com/libp2p/js-libp2p-crypto/commit/9e977c7))
|
||||
* skip nextTick in nodeify ([#103](https://github.com/libp2p/js-libp2p-crypto/issues/103)) ([f20267b](https://github.com/libp2p/js-libp2p-crypto/commit/f20267b))
|
||||
|
||||
|
||||
|
||||
<a name="0.9.4"></a>
|
||||
## [0.9.4](https://github.com/libp2p/js-libp2p-crypto/compare/v0.9.3...v0.9.4) (2017-07-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* circular circular dep -> DI ([bc554d1](https://github.com/libp2p/js-libp2p-crypto/commit/bc554d1))
|
||||
|
||||
|
||||
|
||||
<a name="0.9.3"></a>
|
||||
## [0.9.3](https://github.com/libp2p/js-libp2p-crypto/compare/v0.9.2...v0.9.3) (2017-07-22)
|
||||
|
||||
|
||||
|
||||
<a name="0.9.2"></a>
|
||||
## [0.9.2](https://github.com/libp2p/js-libp2p-crypto/compare/v0.9.1...v0.9.2) (2017-07-22)
|
||||
|
||||
|
@@ -10,7 +10,7 @@
|
||||
[](https://david-dm.org/libp2p/js-libp2p-crypto)
|
||||
[](https://github.com/feross/standard)
|
||||

|
||||

|
||||

|
||||
|
||||
> Crypto primitives for libp2p in JavaScript
|
||||
|
||||
|
@@ -22,5 +22,6 @@ curves.forEach((curve) => {
|
||||
}, { defer: true })
|
||||
})
|
||||
|
||||
suite.on('cycle', (event) => console.log(String(event.target)))
|
||||
.run({async: true})
|
||||
suite
|
||||
.on('cycle', (event) => console.log(String(event.target)))
|
||||
.run({async: true})
|
||||
|
@@ -22,8 +22,9 @@ async.waterfall([
|
||||
setup(cipher, hash, secret)
|
||||
}))
|
||||
|
||||
suite.on('cycle', (event) => console.log(String(event.target)))
|
||||
.run({async: true})
|
||||
suite
|
||||
.on('cycle', (event) => console.log(String(event.target)))
|
||||
.run({async: true})
|
||||
})
|
||||
|
||||
function setup (cipher, hash, secret) {
|
||||
|
@@ -37,5 +37,6 @@ suite.add('sign and verify', (d) => {
|
||||
defer: true
|
||||
})
|
||||
|
||||
suite.on('cycle', (event) => console.log(String(event.target)))
|
||||
.run({async: true})
|
||||
suite
|
||||
.on('cycle', (event) => console.log(String(event.target)))
|
||||
.run({async: true})
|
||||
|
@@ -2,6 +2,10 @@ machine:
|
||||
node:
|
||||
version: stable
|
||||
|
||||
post:
|
||||
test:
|
||||
- npm run coverage -- --upload
|
||||
|
||||
dependencies:
|
||||
pre:
|
||||
- google-chrome --version
|
||||
|
42
package.json
42
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p-crypto",
|
||||
"version": "0.9.2",
|
||||
"version": "0.10.3",
|
||||
"description": "Crypto primitives for libp2p",
|
||||
"main": "src/index.js",
|
||||
"browser": {
|
||||
@@ -10,16 +10,16 @@
|
||||
"./src/keys/rsa.js": "./src/keys/rsa-browser.js"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "aegir-lint",
|
||||
"build": "aegir-build",
|
||||
"test": "aegir-test",
|
||||
"test:node": "aegir-test --env node",
|
||||
"test:browser": "aegir-test --env browser",
|
||||
"release": "aegir-release",
|
||||
"release-minor": "aegir-release --type minor",
|
||||
"release-major": "aegir-release --type major",
|
||||
"coverage": "aegir-coverage",
|
||||
"coverage-publish": "aegir-coverage publish"
|
||||
"lint": "aegir lint",
|
||||
"build": "aegir build",
|
||||
"build-proto": "pbjs --wrap commonjs --target static-module src/keys/keys.proto > src/keys/keys.proto.js",
|
||||
"test": "aegir test",
|
||||
"test:node": "aegir test -t node",
|
||||
"test:browser": "aegir test -t browser -t webworker",
|
||||
"release": "aegir release",
|
||||
"release-minor": "aegir release --type minor",
|
||||
"release-major": "aegir release --type major",
|
||||
"coverage": "aegir coverage --ignore src/keys/keys.proto.js"
|
||||
},
|
||||
"keywords": [
|
||||
"IPFS",
|
||||
@@ -32,22 +32,20 @@
|
||||
"dependencies": {
|
||||
"asn1.js": "^4.9.1",
|
||||
"async": "^2.5.0",
|
||||
"browserify-aes": "^1.0.6",
|
||||
"browserify-aes": "^1.0.8",
|
||||
"keypair": "^1.0.1",
|
||||
"libp2p-crypto-secp256k1": "^0.2.1",
|
||||
"nodeify": "^1.0.1",
|
||||
"libp2p-crypto-secp256k1": "~0.2.2",
|
||||
"multihashing-async": "~0.4.6",
|
||||
"pem-jwk": "^1.5.1",
|
||||
"protocol-buffers": "^3.2.1",
|
||||
"protons": "^1.0.0",
|
||||
"rsa-pem-to-jwk": "^1.1.3",
|
||||
"safe-buffer": "^5.1.1",
|
||||
"tweetnacl": "^1.0.0",
|
||||
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master",
|
||||
"multihashing-async": "~0.4.6"
|
||||
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
|
||||
},
|
||||
"devDependencies": {
|
||||
"aegir": "^11.0.2",
|
||||
"aegir": "^12.0.5",
|
||||
"benchmark": "^2.1.4",
|
||||
"chai": "^4.1.0",
|
||||
"chai": "^4.1.2",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"pre-commit": "^1.2.2"
|
||||
},
|
||||
@@ -56,7 +54,7 @@
|
||||
"test"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4.0.0",
|
||||
"node": ">=6.0.0",
|
||||
"npm": ">=3.0.0"
|
||||
},
|
||||
"repository": {
|
||||
@@ -79,4 +77,4 @@
|
||||
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
|
||||
"nikuda <nikuda@gmail.com>"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
55
src/aes/index-browser.js
Normal file
55
src/aes/index-browser.js
Normal file
@@ -0,0 +1,55 @@
|
||||
'use strict'
|
||||
|
||||
const asm = require('asmcrypto.js')
|
||||
const setImmediate = require('async/setImmediate')
|
||||
|
||||
exports.create = function (key, iv, callback) {
|
||||
const done = (err, res) => setImmediate(() => callback(err, res))
|
||||
|
||||
if (key.length !== 16 && key.length !== 32) {
|
||||
return done(new Error('Invalid key length'))
|
||||
}
|
||||
|
||||
const enc = new asm.AES_CTR.Encrypt({
|
||||
key: key,
|
||||
nonce: iv
|
||||
})
|
||||
const dec = new asm.AES_CTR.Decrypt({
|
||||
key: key,
|
||||
nonce: iv
|
||||
})
|
||||
|
||||
const res = {
|
||||
encrypt (data, cb) {
|
||||
const done = (err, res) => setImmediate(() => cb(err, res))
|
||||
|
||||
let res
|
||||
try {
|
||||
res = Buffer.from(
|
||||
enc.process(data).result
|
||||
)
|
||||
} catch (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
done(null, res)
|
||||
},
|
||||
|
||||
decrypt (data, cb) {
|
||||
const done = (err, res) => setImmediate(() => cb(err, res))
|
||||
|
||||
let res
|
||||
try {
|
||||
res = Buffer.from(
|
||||
dec.process(data).result
|
||||
)
|
||||
} catch (err) {
|
||||
return done(err)
|
||||
}
|
||||
|
||||
done(null, res)
|
||||
}
|
||||
}
|
||||
|
||||
done(null, res)
|
||||
}
|
@@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const nodeify = require('nodeify')
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
const nodeify = require('../nodeify')
|
||||
|
||||
const crypto = require('../webcrypto.js')()
|
||||
const lengths = require('./lengths')
|
||||
@@ -12,6 +11,11 @@ const hashTypes = {
|
||||
SHA512: 'SHA-512'
|
||||
}
|
||||
|
||||
const sign = (key, data, cb) => {
|
||||
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data)
|
||||
.then((raw) => Buffer.from(raw)), cb)
|
||||
}
|
||||
|
||||
exports.create = function (hashType, secret, callback) {
|
||||
const hash = hashTypes[hashType]
|
||||
|
||||
@@ -27,8 +31,7 @@ exports.create = function (hashType, secret, callback) {
|
||||
).then((key) => {
|
||||
return {
|
||||
digest (data, cb) {
|
||||
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data)
|
||||
.then((raw) => Buffer.from(raw)), cb)
|
||||
sign(key, data, cb)
|
||||
},
|
||||
length: lengths[hashType]
|
||||
}
|
||||
|
10
src/index.js
10
src/index.js
@@ -3,18 +3,10 @@
|
||||
const hmac = require('./hmac')
|
||||
const aes = require('./aes')
|
||||
const keys = require('./keys')
|
||||
const rsa = require('./keys/rsa')
|
||||
|
||||
exports = module.exports
|
||||
|
||||
exports.aes = aes
|
||||
exports.hmac = hmac
|
||||
exports.keys = keys
|
||||
|
||||
exports.randomBytes = (number) => {
|
||||
if (!number || typeof number !== 'number') {
|
||||
throw new Error('first argument must be a Number bigger than 0')
|
||||
}
|
||||
|
||||
return rsa.getRandomValues(new Uint8Array(number))
|
||||
}
|
||||
exports.randomBytes = require('./random-bytes')
|
||||
|
@@ -1,9 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
const webcrypto = require('../webcrypto.js')()
|
||||
const nodeify = require('nodeify')
|
||||
const nodeify = require('../nodeify')
|
||||
const BN = require('asn1.js').bignum
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
const util = require('../util')
|
||||
const toBase64 = util.toBase64
|
||||
|
@@ -1,8 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const multihashing = require('multihashing-async')
|
||||
const protobuf = require('protocol-buffers')
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
const protobuf = require('protons')
|
||||
|
||||
const crypto = require('./ed25519')
|
||||
const pbm = protobuf(require('./keys.proto'))
|
||||
|
@@ -2,7 +2,6 @@
|
||||
|
||||
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
|
||||
|
@@ -1,16 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
const protobuf = require('protocol-buffers')
|
||||
const pbm = protobuf(require('./keys.proto'))
|
||||
|
||||
const keys = exports.keys = require('./keys')
|
||||
const protobuf = require('protons')
|
||||
const keysPBM = protobuf(require('./keys.proto'))
|
||||
|
||||
exports = module.exports
|
||||
|
||||
exports.pbm = pbm
|
||||
const supportedKeys = {
|
||||
rsa: require('./rsa-class'),
|
||||
ed25519: require('./ed25519-class'),
|
||||
secp256k1: require('libp2p-crypto-secp256k1')(keysPBM, require('../random-bytes'))
|
||||
}
|
||||
|
||||
exports.supportedKeys = supportedKeys
|
||||
exports.keysPBM = keysPBM
|
||||
|
||||
function isValidKeyType (keyType) {
|
||||
const key = keys[keyType.toLowerCase()]
|
||||
const key = supportedKeys[keyType.toLowerCase()]
|
||||
return key !== undefined
|
||||
}
|
||||
|
||||
@@ -19,7 +24,7 @@ exports.generateEphemeralKeyPair = require('./ephemeral-keys')
|
||||
|
||||
// Generates a keypair of the given type and bitsize
|
||||
exports.generateKeyPair = (type, bits, cb) => {
|
||||
let key = keys[type.toLowerCase()]
|
||||
let key = supportedKeys[type.toLowerCase()]
|
||||
|
||||
if (!key) {
|
||||
return cb(new Error('invalid or unsupported key type'))
|
||||
@@ -31,7 +36,7 @@ exports.generateKeyPair = (type, bits, cb) => {
|
||||
// Generates a keypair of the given type and bitsize
|
||||
// seed is a 32 byte uint8array
|
||||
exports.generateKeyPairFromSeed = (type, seed, bits, cb) => {
|
||||
let key = keys[type.toLowerCase()]
|
||||
let key = supportedKeys[type.toLowerCase()]
|
||||
if (!key) {
|
||||
return cb(new Error('invalid or unsupported key type'))
|
||||
}
|
||||
@@ -44,16 +49,17 @@ exports.generateKeyPairFromSeed = (type, seed, bits, cb) => {
|
||||
// Converts a protobuf serialized public key into its
|
||||
// representative object
|
||||
exports.unmarshalPublicKey = (buf) => {
|
||||
const decoded = pbm.PublicKey.decode(buf)
|
||||
const decoded = keysPBM.PublicKey.decode(buf)
|
||||
const data = decoded.Data
|
||||
|
||||
switch (decoded.Type) {
|
||||
case pbm.KeyType.RSA:
|
||||
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)
|
||||
case keysPBM.KeyType.RSA:
|
||||
return supportedKeys.rsa.unmarshalRsaPublicKey(data)
|
||||
case keysPBM.KeyType.Ed25519:
|
||||
return supportedKeys.ed25519.unmarshalEd25519PublicKey(data)
|
||||
case keysPBM.KeyType.Secp256k1:
|
||||
if (supportedKeys.secp256k1) {
|
||||
return supportedKeys.secp256k1.unmarshalSecp256k1PublicKey(data)
|
||||
} else {
|
||||
throw new Error('secp256k1 support requires libp2p-crypto-secp256k1 package')
|
||||
}
|
||||
@@ -75,16 +81,17 @@ exports.marshalPublicKey = (key, type) => {
|
||||
// Converts a protobuf serialized private key into its
|
||||
// representative object
|
||||
exports.unmarshalPrivateKey = (buf, callback) => {
|
||||
const decoded = pbm.PrivateKey.decode(buf)
|
||||
const decoded = keysPBM.PrivateKey.decode(buf)
|
||||
const data = decoded.Data
|
||||
|
||||
switch (decoded.Type) {
|
||||
case pbm.KeyType.RSA:
|
||||
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)
|
||||
case keysPBM.KeyType.RSA:
|
||||
return supportedKeys.rsa.unmarshalRsaPrivateKey(data, callback)
|
||||
case keysPBM.KeyType.Ed25519:
|
||||
return supportedKeys.ed25519.unmarshalEd25519PrivateKey(data, callback)
|
||||
case keysPBM.KeyType.Secp256k1:
|
||||
if (supportedKeys.secp256k1) {
|
||||
return supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey(data, callback)
|
||||
} else {
|
||||
return callback(new Error('secp256k1 support requires libp2p-crypto-secp256k1 package'))
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const whilst = require('async/whilst')
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
const hmac = require('../hmac')
|
||||
|
||||
const cipherMap = {
|
||||
|
@@ -1,7 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
rsa: require('./rsa-class'),
|
||||
ed25519: require('./ed25519-class'),
|
||||
secp256k1: require('libp2p-crypto-secp256k1')
|
||||
}
|
@@ -5,13 +5,11 @@ module.exports = `enum KeyType {
|
||||
Ed25519 = 1;
|
||||
Secp256k1 = 2;
|
||||
}
|
||||
|
||||
message PublicKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}
|
||||
|
||||
message PrivateKey {
|
||||
required KeyType Type = 1;
|
||||
required bytes Data = 2;
|
||||
}`
|
||||
}`
|
@@ -1,7 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const nodeify = require('nodeify')
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
const nodeify = require('../nodeify')
|
||||
|
||||
const webcrypto = require('../webcrypto.js')()
|
||||
|
||||
@@ -18,11 +17,11 @@ exports.generateKey = function (bits, callback) {
|
||||
true,
|
||||
['sign', 'verify']
|
||||
)
|
||||
.then(exportKey)
|
||||
.then((keys) => ({
|
||||
privateKey: keys[0],
|
||||
publicKey: keys[1]
|
||||
})), callback)
|
||||
.then(exportKey)
|
||||
.then((keys) => ({
|
||||
privateKey: keys[0],
|
||||
publicKey: keys[1]
|
||||
})), callback)
|
||||
}
|
||||
|
||||
// Takes a jwk key
|
||||
|
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const multihashing = require('multihashing-async')
|
||||
const protobuf = require('protocol-buffers')
|
||||
const protobuf = require('protons')
|
||||
|
||||
const crypto = require('./rsa')
|
||||
const pbm = protobuf(require('./keys.proto'))
|
||||
|
11
src/nodeify.js
Normal file
11
src/nodeify.js
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
// Based on npmjs.com/nodeify but without additional `nextTick` calls
|
||||
// to keep the overhead low
|
||||
module.exports = function nodeify (promise, cb) {
|
||||
return promise.then((res) => {
|
||||
cb(null, res)
|
||||
}, (err) => {
|
||||
cb(err)
|
||||
})
|
||||
}
|
13
src/random-bytes.js
Normal file
13
src/random-bytes.js
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const rsa = require('./keys/rsa')
|
||||
|
||||
function randomBytes (number) {
|
||||
if (!number || typeof number !== 'number') {
|
||||
throw new Error('first argument must be a Number bigger than 0')
|
||||
}
|
||||
|
||||
return rsa.getRandomValues(new Uint8Array(number))
|
||||
}
|
||||
|
||||
module.exports = randomBytes
|
@@ -1,7 +1,6 @@
|
||||
'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
|
||||
@@ -11,8 +10,8 @@ exports.toBase64 = function toBase64 (bn, len) {
|
||||
|
||||
return s
|
||||
.replace(/(=*)$/, '') // Remove any trailing '='s
|
||||
.replace(/\+/g, '-') // 62nd char of encoding
|
||||
.replace(/\//g, '_') // 63rd char of encoding
|
||||
.replace(/\+/g, '-') // 62nd char of encoding
|
||||
.replace(/\//g, '_') // 63rd char of encoding
|
||||
}
|
||||
|
||||
// Convert a base64 encoded string to a BN.js instance
|
||||
|
@@ -7,7 +7,6 @@ const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const series = require('async/series')
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
const crypto = require('../../src')
|
||||
const fixtures = require('./../fixtures/aes')
|
||||
|
2
test/fixtures/go-elliptic-key.js
vendored
2
test/fixtures/go-elliptic-key.js
vendored
@@ -1,7 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
module.exports = {
|
||||
curve: 'P-256',
|
||||
bob: {
|
||||
|
2
test/fixtures/go-key-ed25519.js
vendored
2
test/fixtures/go-key-ed25519.js
vendored
@@ -1,7 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
module.exports = {
|
||||
// These were generated in a gore (https://github.com/motemen/gore) repl session:
|
||||
//
|
||||
|
2
test/fixtures/go-key-rsa.js
vendored
2
test/fixtures/go-key-rsa.js
vendored
@@ -1,7 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
module.exports = {
|
||||
private: {
|
||||
hash: Buffer.from([
|
||||
|
2
test/fixtures/go-stretch-key.js
vendored
2
test/fixtures/go-stretch-key.js
vendored
@@ -1,7 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
module.exports = [{
|
||||
cipher: 'AES-256',
|
||||
hash: 'SHA256',
|
||||
|
3
test/fixtures/secp256k1.js
vendored
3
test/fixtures/secp256k1.js
vendored
@@ -1,9 +1,6 @@
|
||||
'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'),
|
||||
|
@@ -2,7 +2,6 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
|
@@ -5,10 +5,9 @@ const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
const crypto = require('../../src')
|
||||
const ed25519 = crypto.keys.keys.ed25519
|
||||
const ed25519 = crypto.keys.supportedKeys.ed25519
|
||||
const fixtures = require('../fixtures/go-key-ed25519')
|
||||
|
||||
describe('ed25519', () => {
|
||||
|
@@ -5,10 +5,9 @@ const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const Buffer = require('safe-buffer').Buffer
|
||||
|
||||
const crypto = require('../../src')
|
||||
const rsa = crypto.keys.keys.rsa
|
||||
const rsa = crypto.keys.supportedKeys.rsa
|
||||
const fixtures = require('../fixtures/go-key-rsa')
|
||||
|
||||
describe('RSA', () => {
|
||||
|
@@ -32,7 +32,7 @@ const mockSecp256k1Module = {
|
||||
}
|
||||
|
||||
describe('without libp2p-crypto-secp256k1 module present', () => {
|
||||
crypto.keys.keys['secp256k1'] = undefined
|
||||
crypto.keys.supportedKeys['secp256k1'] = undefined
|
||||
|
||||
it('fails to generate a secp256k1 key', (done) => {
|
||||
crypto.keys.generateKeyPair('secp256k1', 256, (err, key) => {
|
||||
@@ -61,7 +61,7 @@ describe('with libp2p-crypto-secp256k1 module present', () => {
|
||||
let key
|
||||
|
||||
before((done) => {
|
||||
crypto.keys.keys['secp256k1'] = mockSecp256k1Module
|
||||
crypto.keys.supportedKeys['secp256k1'] = mockSecp256k1Module
|
||||
crypto.keys.generateKeyPair('secp256k1', 256, (err, _key) => {
|
||||
if (err) return done(err)
|
||||
key = _key
|
||||
|
Reference in New Issue
Block a user