mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-07-21 13:02:18 +00:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
857d2bd902 | ||
|
200110cb9d | ||
|
9e5778694c | ||
|
87e8f1c86f | ||
|
df75980a88 | ||
|
934390acd3 | ||
|
8b80b46667 | ||
|
e8efad546f | ||
|
e8cbf13d85 | ||
|
c7e0409c1c | ||
|
f4c00893ad | ||
|
b05e77f375 | ||
|
ad478454d8 | ||
|
8c69ffb20f | ||
|
e689a402a3 | ||
|
4bd032a6ae | ||
|
50c61ba46e |
31
CHANGELOG.md
31
CHANGELOG.md
@@ -1,3 +1,34 @@
|
|||||||
|
<a name="0.14.1"></a>
|
||||||
|
## [0.14.1](https://github.com/libp2p/js-libp2p-crypto/compare/v0.14.0...v0.14.1) (2018-11-05)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* dont setimmediate when its not needed ([9e57786](https://github.com/libp2p/js-libp2p-crypto/commit/9e57786))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.14.0"></a>
|
||||||
|
# [0.14.0](https://github.com/libp2p/js-libp2p-crypto/compare/v0.13.0...v0.14.0) (2018-09-17)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* windows build ([c7e0409](https://github.com/libp2p/js-libp2p-crypto/commit/c7e0409))
|
||||||
|
* **lint:** use ~ for ursa-optional version ([e8cbf13](https://github.com/libp2p/js-libp2p-crypto/commit/e8cbf13))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* use ursa-optional for lightning fast key generation ([b05e77f](https://github.com/libp2p/js-libp2p-crypto/commit/b05e77f))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.13.0"></a>
|
||||||
|
# [0.13.0](https://github.com/libp2p/js-libp2p-crypto/compare/v0.12.1...v0.13.0) (2018-04-05)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.12.1"></a>
|
<a name="0.12.1"></a>
|
||||||
## [0.12.1](https://github.com/libp2p/js-libp2p-crypto/compare/v0.12.0...v0.12.1) (2018-02-12)
|
## [0.12.1](https://github.com/libp2p/js-libp2p-crypto/compare/v0.12.0...v0.12.1) (2018-02-12)
|
||||||
|
|
||||||
|
67
README.md
67
README.md
@@ -16,6 +16,10 @@
|
|||||||
|
|
||||||
This repo contains the JavaScript implementation of the crypto primitives needed for libp2p. This is based on this [go implementation](https://github.com/libp2p/go-libp2p-crypto).
|
This repo contains the JavaScript implementation of the crypto primitives needed for libp2p. This is based on this [go implementation](https://github.com/libp2p/go-libp2p-crypto).
|
||||||
|
|
||||||
|
## Lead Maintainer
|
||||||
|
|
||||||
|
[Friedel Ziegelmayer](https://github.com/dignifiedquire/)
|
||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
- [Install](#install)
|
- [Install](#install)
|
||||||
@@ -70,8 +74,51 @@ This uses `CTR` mode.
|
|||||||
- `data: Buffer`
|
- `data: Buffer`
|
||||||
- `callback: Function`
|
- `callback: Function`
|
||||||
|
|
||||||
```
|
```js
|
||||||
TODO: Example of using aes
|
var crypto = require('libp2p-crypto')
|
||||||
|
|
||||||
|
// Setting up Key and IV
|
||||||
|
|
||||||
|
// A 16 bytes array, 128 Bits, AES-128 is chosen
|
||||||
|
var key128 = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
||||||
|
|
||||||
|
// A 16 bytes array, 128 Bits,
|
||||||
|
var IV = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
||||||
|
|
||||||
|
async function main () {
|
||||||
|
let decryptedMessage = 'Hello, world!'
|
||||||
|
let encryptedMessage
|
||||||
|
|
||||||
|
// Encrypting
|
||||||
|
await crypto.aes.create(key128, IV, (err, cipher) => {
|
||||||
|
if (!err) {
|
||||||
|
cipher.encrypt(Buffer.from(decryptedMessage), (err, encryptedBuffer) => {
|
||||||
|
if (!err) {
|
||||||
|
console.log(encryptedBuffer)
|
||||||
|
// prints: <Buffer 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
||||||
|
encryptedMessage = encryptedBuffer
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Decrypting
|
||||||
|
await crypto.aes.create(key128, IV, (err, cipher) => {
|
||||||
|
if (!err) {
|
||||||
|
cipher.decrypt(encryptedMessage, (err, decryptedBuffer) => {
|
||||||
|
if (!err) {
|
||||||
|
console.log(decryptedBuffer)
|
||||||
|
// prints: <Buffer 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
|
||||||
|
|
||||||
|
console.log(decryptedBuffer.toString('utf-8'))
|
||||||
|
// prints: Hello, world!
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
main()
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### `crypto.hmac`
|
### `crypto.hmac`
|
||||||
@@ -91,8 +138,20 @@ Exposes an interface to the Keyed-Hash Message Authentication Code (HMAC) as def
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```
|
```js
|
||||||
TODO: Example of using hmac
|
var crypto = require('libp2p-crypto')
|
||||||
|
|
||||||
|
let hash = 'SHA1' // 'SHA256' || 'SHA512'
|
||||||
|
|
||||||
|
crypto.hmac.create(hash, Buffer.from('secret'), (err, hmac) => {
|
||||||
|
if (!err) {
|
||||||
|
hmac.digest(Buffer.from('hello world'), (err, sig) => {
|
||||||
|
if (!err) {
|
||||||
|
console.log(sig)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### `crypto.keys`
|
### `crypto.keys`
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Benchmark = require('benchmark')
|
const Benchmark = require('benchmark')
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Benchmark = require('benchmark')
|
const Benchmark = require('benchmark')
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const Benchmark = require('benchmark')
|
const Benchmark = require('benchmark')
|
||||||
|
30
package.json
30
package.json
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-crypto",
|
"name": "libp2p-crypto",
|
||||||
"version": "0.12.1",
|
"version": "0.14.1",
|
||||||
"description": "Crypto primitives for libp2p",
|
"description": "Crypto primitives for libp2p",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
|
"leadMaintainer": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"browser": {
|
"browser": {
|
||||||
"./src/hmac/index.js": "./src/hmac/index-browser.js",
|
"./src/hmac/index.js": "./src/hmac/index-browser.js",
|
||||||
"./src/keys/ecdh.js": "./src/keys/ecdh-browser.js",
|
"./src/keys/ecdh.js": "./src/keys/ecdh-browser.js",
|
||||||
@@ -27,35 +28,30 @@
|
|||||||
"crypto",
|
"crypto",
|
||||||
"rsa"
|
"rsa"
|
||||||
],
|
],
|
||||||
"author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"asn1.js": "^5.0.0",
|
"asn1.js": "^5.0.1",
|
||||||
"async": "^2.6.0",
|
"async": "^2.6.1",
|
||||||
"browserify-aes": "^1.1.1",
|
"browserify-aes": "^1.2.0",
|
||||||
"bs58": "^4.0.1",
|
"bs58": "^4.0.1",
|
||||||
"keypair": "^1.0.1",
|
"keypair": "^1.0.1",
|
||||||
"libp2p-crypto-secp256k1": "~0.2.2",
|
"libp2p-crypto-secp256k1": "~0.2.2",
|
||||||
"multihashing-async": "~0.4.7",
|
"multihashing-async": "~0.5.1",
|
||||||
"node-forge": "^0.7.1",
|
"node-forge": "~0.7.6",
|
||||||
"pem-jwk": "^1.5.1",
|
"pem-jwk": "^1.5.1",
|
||||||
"protons": "^1.0.1",
|
"protons": "^1.0.1",
|
||||||
"rsa-pem-to-jwk": "^1.1.3",
|
"rsa-pem-to-jwk": "^1.1.3",
|
||||||
"tweetnacl": "^1.0.0",
|
"tweetnacl": "^1.0.0",
|
||||||
|
"ursa-optional": "~0.9.9",
|
||||||
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
|
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^12.4.0",
|
"aegir": "^17.0.1",
|
||||||
"benchmark": "^2.1.4",
|
"benchmark": "^2.1.4",
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.2.0",
|
||||||
"chai-string": "^1.4.0",
|
"chai-string": "^1.5.0",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1"
|
||||||
"pre-commit": "^1.2.2"
|
|
||||||
},
|
},
|
||||||
"pre-commit": [
|
|
||||||
"lint",
|
|
||||||
"test"
|
|
||||||
],
|
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=6.0.0",
|
"node": ">=6.0.0",
|
||||||
"npm": ">=3.0.0"
|
"npm": ">=3.0.0"
|
||||||
@@ -74,6 +70,8 @@
|
|||||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||||
"Greenkeeper <support@greenkeeper.io>",
|
"Greenkeeper <support@greenkeeper.io>",
|
||||||
"Jack Kleeman <jackkleeman@gmail.com>",
|
"Jack Kleeman <jackkleeman@gmail.com>",
|
||||||
|
"Jacob Heun <jacobheun@gmail.com>",
|
||||||
|
"Joao Santos <jrmsantos15@gmail.com>",
|
||||||
"Maciej Krüger <mkg20001@gmail.com>",
|
"Maciej Krüger <mkg20001@gmail.com>",
|
||||||
"Richard Littauer <richard.littauer@gmail.com>",
|
"Richard Littauer <richard.littauer@gmail.com>",
|
||||||
"Richard Schneider <makaretu@gmail.com>",
|
"Richard Schneider <makaretu@gmail.com>",
|
||||||
|
@@ -10,9 +10,7 @@ exports.create = function (hash, secret, callback) {
|
|||||||
|
|
||||||
hmac.update(data)
|
hmac.update(data)
|
||||||
|
|
||||||
setImmediate(() => {
|
cb(null, hmac.digest())
|
||||||
cb(null, hmac.digest())
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
length: lengths[hash]
|
length: lengths[hash]
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,27 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const crypto = require('crypto')
|
const crypto = require('crypto')
|
||||||
const keypair = require('keypair')
|
let keypair
|
||||||
|
try {
|
||||||
|
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') {
|
||||||
|
throw new Error('Force keypair usage')
|
||||||
|
}
|
||||||
|
|
||||||
|
const ursa = require('ursa-optional') // throws if not compiled
|
||||||
|
keypair = ({bits}) => {
|
||||||
|
const key = ursa.generatePrivateKey(bits)
|
||||||
|
return {
|
||||||
|
private: key.toPrivatePem(),
|
||||||
|
public: key.toPublicPem()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'ursa') {
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
|
keypair = require('keypair')
|
||||||
|
}
|
||||||
const setImmediate = require('async/setImmediate')
|
const setImmediate = require('async/setImmediate')
|
||||||
const pemToJwk = require('pem-jwk').pem2jwk
|
const pemToJwk = require('pem-jwk').pem2jwk
|
||||||
const jwkToPem = require('pem-jwk').jwk2pem
|
const jwkToPem = require('pem-jwk').jwk2pem
|
||||||
|
65
test/keys/rsa-crypto-libs.js
Normal file
65
test/keys/rsa-crypto-libs.js
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
/* eslint-env mocha */
|
||||||
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
chai.use(require('chai-string'))
|
||||||
|
|
||||||
|
const LIBS = ['ursa', 'keypair']
|
||||||
|
|
||||||
|
describe('RSA crypto libs', function () {
|
||||||
|
this.timeout(20 * 1000)
|
||||||
|
|
||||||
|
LIBS.forEach(lib => {
|
||||||
|
describe(lib, () => {
|
||||||
|
let crypto
|
||||||
|
let rsa
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
process.env.LP2P_FORCE_CRYPTO_LIB = lib
|
||||||
|
|
||||||
|
for (const path in require.cache) { // clear module cache
|
||||||
|
if (path.endsWith('.js')) {
|
||||||
|
delete require.cache[path]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crypto = require('../../src')
|
||||||
|
rsa = crypto.keys.supportedKeys.rsa
|
||||||
|
})
|
||||||
|
|
||||||
|
it('generates a valid key', (done) => {
|
||||||
|
crypto.keys.generateKeyPair('RSA', 512, (err, key) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(key).to.be.an.instanceof(rsa.RsaPrivateKey)
|
||||||
|
|
||||||
|
key.hash((err, digest) => {
|
||||||
|
if (err) {
|
||||||
|
return done(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(digest).to.have.length(34)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
after(() => {
|
||||||
|
for (const path in require.cache) { // clear module cache
|
||||||
|
if (path.endsWith('.js')) {
|
||||||
|
delete require.cache[path]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete process.env.LP2P_FORCE_CRYPTO_LIB
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
3
test/node.js
Normal file
3
test/node.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
require('./keys/rsa-crypto-libs')
|
Reference in New Issue
Block a user