chore: update deps

This commit is contained in:
Vasco Santos 2021-04-07 11:15:26 +02:00
parent a05e870c45
commit 3041ef7ba7
12 changed files with 58 additions and 52 deletions

View File

@ -8,8 +8,8 @@ stages:
- cov - cov
node_js: node_js:
- '10'
- '12' - '12'
- '14'
os: os:
- linux - linux

View File

@ -39,19 +39,19 @@
], ],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"err-code": "^2.0.0", "err-code": "^3.0.1",
"is-typedarray": "^1.0.0", "is-typedarray": "^1.0.0",
"iso-random-stream": "^1.1.0", "iso-random-stream": "^2.0.0",
"keypair": "^1.0.1", "keypair": "^1.0.1",
"multibase": "^3.0.0", "multibase": "^4.0.3",
"multicodec": "^2.0.0", "multicodec": "^3.0.1",
"multihashes": "^4.0.2", "multihashes": "^4.0.2",
"multihashing-async": "^2.0.1", "multihashing-async": "^2.1.2",
"node-forge": "^0.10.0", "node-forge": "^0.10.0",
"pem-jwk": "^2.0.0", "pem-jwk": "^2.0.0",
"protons": "^2.0.0", "protons": "^2.0.0",
"secp256k1": "^4.0.0", "secp256k1": "^4.0.0",
"uint8arrays": "^1.1.0", "uint8arrays": "^2.1.4",
"ursa-optional": "^0.10.1" "ursa-optional": "^0.10.1"
}, },
"devDependencies": { "devDependencies": {
@ -59,15 +59,15 @@
"@types/chai-string": "^1.4.2", "@types/chai-string": "^1.4.2",
"@types/dirty-chai": "^2.0.2", "@types/dirty-chai": "^2.0.2",
"@types/mocha": "^8.0.1", "@types/mocha": "^8.0.1",
"aegir": "^25.0.0", "aegir": "^33.0.0",
"benchmark": "^2.1.4", "benchmark": "^2.1.4",
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-string": "^1.5.0", "chai-string": "^1.5.0",
"dirty-chai": "^2.0.1", "dirty-chai": "^2.0.1",
"sinon": "^9.0.0" "sinon": "^10.0.0"
}, },
"engines": { "engines": {
"node": ">=10.0.0", "node": ">=12.0.0",
"npm": ">=6.0.0" "npm": ">=6.0.0"
}, },
"repository": { "repository": {

View File

@ -11,11 +11,11 @@ const webcrypto = require('../webcrypto')
* *
* @param {object} [options] * @param {object} [options]
* @param {string} [options.algorithm=AES-GCM] * @param {string} [options.algorithm=AES-GCM]
* @param {Number} [options.nonceLength=12] * @param {number} [options.nonceLength=12]
* @param {Number} [options.keyLength=16] * @param {number} [options.keyLength=16]
* @param {string} [options.digest=sha256] * @param {string} [options.digest=sha256]
* @param {Number} [options.saltLength=16] * @param {number} [options.saltLength=16]
* @param {Number} [options.iterations=32767] * @param {number} [options.iterations=32767]
* @returns {*} * @returns {*}
*/ */
function create ({ function create ({
@ -33,8 +33,8 @@ function create ({
* Uses the provided password to derive a pbkdf2 key. The key * Uses the provided password to derive a pbkdf2 key. The key
* will then be used to encrypt the data. * will then be used to encrypt the data.
* *
* @param {Uint8Array} data The data to decrypt * @param {Uint8Array} data - The data to decrypt
* @param {string} password A plain password * @param {string} password - A plain password
* @returns {Promise<Uint8Array>} * @returns {Promise<Uint8Array>}
*/ */
async function encrypt (data, password) { // eslint-disable-line require-await async function encrypt (data, password) { // eslint-disable-line require-await
@ -58,8 +58,8 @@ function create ({
* this decryption cipher must be the same as those used to create * this decryption cipher must be the same as those used to create
* the encryption cipher. * the encryption cipher.
* *
* @param {Uint8Array} data The data to decrypt * @param {Uint8Array} data - The data to decrypt
* @param {string} password A plain password * @param {string} password - A plain password
* @returns {Promise<Uint8Array>} * @returns {Promise<Uint8Array>}
*/ */
async function decrypt (data, password) { async function decrypt (data, password) {

View File

@ -9,12 +9,12 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
/** /**
* *
* @param {object} [options] * @param {object} [options]
* @param {Number} [options.algorithmTagLength=16] * @param {number} [options.algorithmTagLength=16]
* @param {Number} [options.nonceLength=12] * @param {number} [options.nonceLength=12]
* @param {Number} [options.keyLength=16] * @param {number} [options.keyLength=16]
* @param {string} [options.digest=sha256] * @param {string} [options.digest=sha256]
* @param {Number} [options.saltLength=16] * @param {number} [options.saltLength=16]
* @param {Number} [options.iterations=32767] * @param {number} [options.iterations=32767]
* @returns {*} * @returns {*}
*/ */
function create ({ function create ({
@ -49,8 +49,8 @@ function create ({
* Uses the provided password to derive a pbkdf2 key. The key * Uses the provided password to derive a pbkdf2 key. The key
* will then be used to encrypt the data. * will then be used to encrypt the data.
* *
* @param {Uint8Array} data The data to decrypt * @param {Uint8Array} data - The data to decrypt
* @param {string|Uint8Array} password A plain password * @param {string|Uint8Array} password - A plain password
* @returns {Promise<Uint8Array>} * @returns {Promise<Uint8Array>}
*/ */
async function encrypt (data, password) { // eslint-disable-line require-await async function encrypt (data, password) { // eslint-disable-line require-await
@ -76,7 +76,7 @@ function create ({
* the encryption cipher. * the encryption cipher.
* *
* @private * @private
* @param {Uint8Array} ciphertextAndNonce The data to decrypt * @param {Uint8Array} ciphertextAndNonce - The data to decrypt
* @param {Uint8Array} key * @param {Uint8Array} key
* @returns {Promise<Uint8Array>} * @returns {Promise<Uint8Array>}
*/ */
@ -100,8 +100,8 @@ function create ({
* this decryption cipher must be the same as those used to create * this decryption cipher must be the same as those used to create
* the encryption cipher. * the encryption cipher.
* *
* @param {Uint8Array} data The data to decrypt * @param {Uint8Array} data - The data to decrypt
* @param {string|Uint8Array} password A plain password * @param {string|Uint8Array} password - A plain password
*/ */
async function decrypt (data, password) { // eslint-disable-line require-await async function decrypt (data, password) { // eslint-disable-line require-await
// Create Uint8Arrays of salt and ciphertextAndNonce. // Create Uint8Arrays of salt and ciphertextAndNonce.

View File

@ -80,7 +80,7 @@ class Ed25519PrivateKey {
* The public key is a protobuf encoding containing a type and the DER encoding * The public key is a protobuf encoding containing a type and the DER encoding
* of the PKCS SubjectPublicKeyInfo. * of the PKCS SubjectPublicKeyInfo.
* *
* @returns {Promise<String>} * @returns {Promise<string>}
*/ */
async id () { async id () {
const encoding = mh.encode(this.public.bytes, 'identity') const encoding = mh.encode(this.public.bytes, 'identity')

View File

@ -9,7 +9,7 @@ module.exports = {
* The PrivateKey is encrypted via a password derived PBKDF2 key * The PrivateKey is encrypted via a password derived PBKDF2 key
* leveraging the aes-gcm cipher algorithm. * leveraging the aes-gcm cipher algorithm.
* *
* @param {Uint8Array} privateKey The PrivateKey protobuf * @param {Uint8Array} privateKey - The PrivateKey protobuf
* @param {string} password * @param {string} password
* @returns {Promise<string>} A base64 encoded string * @returns {Promise<string>} A base64 encoded string
*/ */

View File

@ -9,7 +9,7 @@ module.exports = {
* with the given password. The privateKey must have been exported * with the given password. The privateKey must have been exported
* using the same password and underlying cipher (aes-gcm) * using the same password and underlying cipher (aes-gcm)
* *
* @param {string} privateKey A base64 encoded encrypted key * @param {string} privateKey - A base64 encoded encrypted key
* @param {string} password * @param {string} password
* @returns {Promise<Uint8Array>} The private key protobuf * @returns {Promise<Uint8Array>} The private key protobuf
*/ */

View File

@ -10,23 +10,18 @@ const uint8ArrayFromString = require('uint8arrays/from-string')
const importer = require('./importer') const importer = require('./importer')
exports = module.exports
const supportedKeys = { const supportedKeys = {
rsa: require('./rsa-class'), rsa: require('./rsa-class'),
ed25519: require('./ed25519-class'), ed25519: require('./ed25519-class'),
secp256k1: require('./secp256k1-class')(keysPBM, require('../random-bytes')) secp256k1: require('./secp256k1-class')(keysPBM, require('../random-bytes'))
} }
exports.supportedKeys = supportedKeys
exports.keysPBM = keysPBM
const ErrMissingSecp256K1 = { const ErrMissingSecp256K1 = {
message: 'secp256k1 support requires libp2p-crypto-secp256k1 package', message: 'secp256k1 support requires libp2p-crypto-secp256k1 package',
code: 'ERR_MISSING_PACKAGE' code: 'ERR_MISSING_PACKAGE'
} }
function typeToKey (type) { function typeToKey(type) {
const key = supportedKeys[type.toLowerCase()] const key = supportedKeys[type.toLowerCase()]
if (!key) { if (!key) {
const supported = Object.keys(supportedKeys).join(' / ') const supported = Object.keys(supportedKeys).join(' / ')
@ -35,17 +30,14 @@ function typeToKey (type) {
return key return key
} }
exports.keyStretcher = require('./key-stretcher')
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 = async (type, bits) => { // eslint-disable-line require-await const generateKeyPair = async (type, bits) => { // eslint-disable-line require-await
return typeToKey(type).generateKeyPair(bits) return typeToKey(type).generateKeyPair(bits)
} }
// Generates a keypair of the given type and bitsize // Generates a keypair of the given type and bitsize
// seed is a 32 byte uint8array // seed is a 32 byte uint8array
exports.generateKeyPairFromSeed = async (type, seed, bits) => { // eslint-disable-line require-await const generateKeyPairFromSeed = async (type, seed, bits) => { // eslint-disable-line require-await
const key = typeToKey(type) const key = typeToKey(type)
if (type.toLowerCase() !== 'ed25519') { if (type.toLowerCase() !== 'ed25519') {
throw errcode(new Error('Seed key derivation is unimplemented for RSA or secp256k1'), 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE') throw errcode(new Error('Seed key derivation is unimplemented for RSA or secp256k1'), 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
@ -55,7 +47,7 @@ exports.generateKeyPairFromSeed = async (type, seed, bits) => { // eslint-disabl
// Converts a protobuf serialized public key into its // Converts a protobuf serialized public key into its
// representative object // representative object
exports.unmarshalPublicKey = (buf) => { const unmarshalPublicKey = (buf) => {
const decoded = keysPBM.PublicKey.decode(buf) const decoded = keysPBM.PublicKey.decode(buf)
const data = decoded.Data const data = decoded.Data
@ -76,7 +68,7 @@ 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) => { const marshalPublicKey = (key, type) => {
type = (type || 'rsa').toLowerCase() type = (type || 'rsa').toLowerCase()
typeToKey(type) // check type typeToKey(type) // check type
return key.bytes return key.bytes
@ -84,7 +76,7 @@ exports.marshalPublicKey = (key, type) => {
// Converts a protobuf serialized private key into its // Converts a protobuf serialized private key into its
// representative object // representative object
exports.unmarshalPrivateKey = async (buf) => { // eslint-disable-line require-await const unmarshalPrivateKey = async (buf) => { // eslint-disable-line require-await
const decoded = keysPBM.PrivateKey.decode(buf) const decoded = keysPBM.PrivateKey.decode(buf)
const data = decoded.Data const data = decoded.Data
@ -105,7 +97,7 @@ exports.unmarshalPrivateKey = async (buf) => { // eslint-disable-line require-aw
} }
// 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) => { const marshalPrivateKey = (key, type) => {
type = (type || 'rsa').toLowerCase() type = (type || 'rsa').toLowerCase()
typeToKey(type) // check type typeToKey(type) // check type
return key.bytes return key.bytes
@ -116,10 +108,10 @@ exports.marshalPrivateKey = (key, type) => {
* @param {string} encryptedKey * @param {string} encryptedKey
* @param {string} password * @param {string} password
*/ */
exports.import = async (encryptedKey, password) => { // eslint-disable-line require-await const importKey = async (encryptedKey, password) => { // eslint-disable-line require-await
try { try {
const key = await importer.import(encryptedKey, password) const key = await importer.import(encryptedKey, password)
return exports.unmarshalPrivateKey(key) return unmarshalPrivateKey(key)
} catch (_) { } catch (_) {
// Ignore and try the old pem decrypt // Ignore and try the old pem decrypt
} }
@ -133,3 +125,17 @@ exports.import = async (encryptedKey, password) => { // eslint-disable-line requ
der = uint8ArrayFromString(der.getBytes(), 'ascii') der = uint8ArrayFromString(der.getBytes(), 'ascii')
return supportedKeys.rsa.unmarshalRsaPrivateKey(der) return supportedKeys.rsa.unmarshalRsaPrivateKey(der)
} }
module.exports = {
supportedKeys,
keysPBM,
keyStretcher: require('./key-stretcher'),
generateEphemeralKeyPair: require('./ephemeral-keys'),
generateKeyPair,
generateKeyPairFromSeed,
unmarshalPublicKey,
marshalPublicKey,
unmarshalPrivateKey,
marshalPrivateKey,
import: importKey
}

View File

@ -101,7 +101,7 @@ class RsaPrivateKey {
* The public key is a protobuf encoding containing a type and the DER encoding * The public key is a protobuf encoding containing a type and the DER encoding
* of the PKCS SubjectPublicKeyInfo. * of the PKCS SubjectPublicKeyInfo.
* *
* @returns {Promise<String>} * @returns {Promise<string>}
*/ */
async id () { async id () {
const hash = await this.public.hash() const hash = await this.public.hash()

View File

@ -23,7 +23,7 @@ const hashName = {
* @param {string} password * @param {string} password
* @param {string} salt * @param {string} salt
* @param {number} iterations * @param {number} iterations
* @param {number} keySize (in bytes) * @param {number} keySize - (in bytes)
* @param {string} hash - The hash name ('sha1', 'sha2-512, ...) * @param {string} hash - The hash name ('sha1', 'sha2-512, ...)
* @returns {string} - A new password * @returns {string} - A new password
*/ */

View File

@ -51,7 +51,7 @@ describe('libp2p-crypto', function () {
}) })
it('generateKeyPairFromSeed', () => { it('generateKeyPairFromSeed', () => {
var seed = crypto.randomBytes(32) const seed = crypto.randomBytes(32)
return expectErrCode(crypto.keys.generateKeyPairFromSeed('invalid-key-type', seed, 512), 'ERR_UNSUPPORTED_KEY_TYPE') return expectErrCode(crypto.keys.generateKeyPairFromSeed('invalid-key-type', seed, 512), 'ERR_UNSUPPORTED_KEY_TYPE')
}) })

View File

@ -33,7 +33,7 @@ describe('ed25519', function () {
}) })
it('generates a valid key from seed', async () => { it('generates a valid key from seed', async () => {
var seed = crypto.randomBytes(32) const seed = crypto.randomBytes(32)
const seededkey = await crypto.keys.generateKeyPairFromSeed('Ed25519', seed, 512) const seededkey = await crypto.keys.generateKeyPairFromSeed('Ed25519', seed, 512)
expect(seededkey).to.be.an.instanceof(ed25519.Ed25519PrivateKey) expect(seededkey).to.be.an.instanceof(ed25519.Ed25519PrivateKey)
const digest = await seededkey.hash() const digest = await seededkey.hash()