libp2p-crypto
Advanced tools
Comparing version 0.17.8 to 0.17.9
@@ -0,1 +1,11 @@ | ||
<a name="0.17.9"></a> | ||
## [0.17.9](https://github.com/libp2p/js-libp2p-crypto/compare/v0.17.8...v0.17.9) (2020-08-05) | ||
### Features | ||
* add exporting/importing of non rsa keys in libp2p-key format ([#179](https://github.com/libp2p/js-libp2p-crypto/issues/179)) ([7273739](https://github.com/libp2p/js-libp2p-crypto/commit/7273739)) | ||
<a name="0.17.8"></a> | ||
@@ -2,0 +12,0 @@ ## [0.17.8](https://github.com/libp2p/js-libp2p-crypto/compare/v0.17.7...v0.17.8) (2020-07-20) |
{ | ||
"name": "libp2p-crypto", | ||
"version": "0.17.8", | ||
"version": "0.17.9", | ||
"description": "Crypto primitives for libp2p", | ||
@@ -9,5 +9,6 @@ "main": "src/index.js", | ||
"browser": { | ||
"./src/aes/ciphers.js": "./src/aes/ciphers-browser.js", | ||
"./src/ciphers/aes-gcm.js": "./src/ciphers/aes-gcm.browser.js", | ||
"./src/hmac/index.js": "./src/hmac/index-browser.js", | ||
"./src/keys/ecdh.js": "./src/keys/ecdh-browser.js", | ||
"./src/aes/ciphers.js": "./src/aes/ciphers-browser.js", | ||
"./src/keys/rsa.js": "./src/keys/rsa-browser.js" | ||
@@ -47,17 +48,18 @@ }, | ||
"keypair": "^1.0.1", | ||
"multibase": "^0.7.0", | ||
"multibase": "^1.0.1", | ||
"multicodec": "^1.0.4", | ||
"multihashing-async": "^0.8.1", | ||
"node-forge": "^0.9.1", | ||
"pem-jwk": "^2.0.0", | ||
"protons": "^1.0.1", | ||
"protons": "^1.2.1", | ||
"secp256k1": "^4.0.0", | ||
"ursa-optional": "~0.10.1" | ||
"uint8arrays": "^1.0.0", | ||
"ursa-optional": "^0.10.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.11", | ||
"@types/chai": "^4.2.12", | ||
"@types/chai-string": "^1.4.2", | ||
"@types/dirty-chai": "^2.0.2", | ||
"@types/mocha": "^7.0.1", | ||
"@types/sinon": "^9.0.0", | ||
"aegir": "^22.0.0", | ||
"@types/mocha": "^8.0.1", | ||
"aegir": "^25.0.0", | ||
"benchmark": "^2.1.4", | ||
@@ -64,0 +66,0 @@ "chai": "^4.2.0", |
@@ -265,11 +265,20 @@ # js-libp2p-crypto | ||
### `crypto.keys.import(pem, password)` | ||
### `crypto.keys.import(encryptedKey, password)` | ||
- `pem: string` | ||
- `encryptedKey: string` | ||
- `password: string` | ||
Returns `Promise<RsaPrivateKey>` | ||
Returns `Promise<PrivateKey>` | ||
Converts a PEM password protected private key into its representative object. | ||
Converts an exported private key into its representative object. Supported formats are 'pem' (RSA only) and 'libp2p-key'. | ||
### `privateKey.export(password, format)` | ||
- `password: string` | ||
- `format: string` the format to export to: 'pem' (rsa only), 'libp2p-key' | ||
Returns `string` | ||
Exports the password protected `PrivateKey`. RSA keys will be exported as password protected PEM by default. Ed25519 and Secp256k1 keys will be exported as password protected AES-GCM base64 encoded strings ('libp2p-key' format). | ||
### `crypto.randomBytes(number)` | ||
@@ -276,0 +285,0 @@ |
@@ -97,2 +97,6 @@ /// <reference types="node" /> | ||
id(): Promise<string>; | ||
/** | ||
* Exports the password protected key in the format specified. | ||
*/ | ||
export(password: string, format?: "pkcs-8" | string): Promise<string>; | ||
} | ||
@@ -136,5 +140,2 @@ | ||
// Type alias for export method | ||
export type KeyInfo = any; | ||
class RsaPrivateKey implements PrivateKey { | ||
@@ -151,9 +152,3 @@ constructor(key: any, publicKey: Buffer); | ||
id(): Promise<string>; | ||
/** | ||
* Exports the key into a password protected PEM format | ||
* | ||
* @param password The password to read the encrypted PEM | ||
* @param format Defaults to 'pkcs-8'. | ||
*/ | ||
export(password: string, format?: "pkcs-8" | string): KeyInfo; | ||
export(password: string, format?: string): Promise<string>; | ||
} | ||
@@ -186,2 +181,3 @@ function unmarshalRsaPublicKey(buf: Buffer): RsaPublicKey; | ||
id(): Promise<string>; | ||
export(password: string, format?: string): Promise<string>; | ||
} | ||
@@ -219,2 +215,3 @@ | ||
id(): Promise<string>; | ||
export(password: string, format?: string): Promise<string>; | ||
} | ||
@@ -242,12 +239,10 @@ | ||
export function generateKeyPair( | ||
type: "Ed25519", | ||
bits: number | ||
type: "Ed25519" | ||
): Promise<keys.supportedKeys.ed25519.Ed25519PrivateKey>; | ||
export function generateKeyPair( | ||
export function generateKeyPair( | ||
type: "RSA", | ||
bits: number | ||
): Promise<keys.supportedKeys.rsa.RsaPrivateKey>; | ||
export function generateKeyPair( | ||
type: "secp256k1", | ||
bits: number | ||
export function generateKeyPair( | ||
type: "secp256k1" | ||
): Promise<keys.supportedKeys.secp256k1.Secp256k1PrivateKey>; | ||
@@ -327,3 +322,3 @@ | ||
*/ | ||
function _import(pem: string, password: string): Promise<supportedKeys.rsa.RsaPrivateKey>; | ||
function _import(pem: string, password: string, format?: string): Promise<supportedKeys.rsa.RsaPrivateKey>; | ||
export { _import as import }; | ||
@@ -330,0 +325,0 @@ } |
@@ -11,2 +11,3 @@ 'use strict' | ||
const pbm = protobuf(require('./keys.proto')) | ||
const exporter = require('./exporter') | ||
@@ -90,2 +91,17 @@ class Ed25519PublicKey { | ||
} | ||
/** | ||
* Exports the key into a password protected `format` | ||
* | ||
* @param {string} password - The password to encrypt the key | ||
* @param {string} [format=libp2p-key] - The format in which to export as | ||
* @returns {Promise<Buffer>} The encrypted private key | ||
*/ | ||
async export (password, format = 'libp2p-key') { // eslint-disable-line require-await | ||
if (format === 'libp2p-key') { | ||
return exporter.export(this.bytes, password) | ||
} else { | ||
throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT') | ||
} | ||
} | ||
} | ||
@@ -92,0 +108,0 @@ |
@@ -11,2 +11,4 @@ 'use strict' | ||
const importer = require('./importer') | ||
exports = module.exports | ||
@@ -113,4 +115,17 @@ | ||
exports.import = async (pem, password) => { // eslint-disable-line require-await | ||
const key = forge.pki.decryptRsaPrivateKey(pem, password) | ||
/** | ||
* | ||
* @param {string} encryptedKey | ||
* @param {string} password | ||
*/ | ||
exports.import = async (encryptedKey, password) => { // eslint-disable-line require-await | ||
try { | ||
const key = await importer.import(encryptedKey, password) | ||
return exports.unmarshalPrivateKey(key) | ||
} catch (_) { | ||
// Ignore and try the old pem decrypt | ||
} | ||
// Only rsa supports pem right now | ||
const key = forge.pki.decryptRsaPrivateKey(encryptedKey, password) | ||
if (key === null) { | ||
@@ -117,0 +132,0 @@ throw errcode(new Error('Cannot read the key, most likely the password is wrong or not a RSA key'), 'ERR_CANNOT_DECRYPT_PEM') |
@@ -8,4 +8,2 @@ 'use strict' | ||
const crypto = require('./rsa') | ||
const pbm = protobuf(require('./keys.proto')) | ||
require('node-forge/lib/sha512') | ||
@@ -15,2 +13,6 @@ require('node-forge/lib/ed25519') | ||
const crypto = require('./rsa') | ||
const pbm = protobuf(require('./keys.proto')) | ||
const exporter = require('./exporter') | ||
class RsaPublicKey { | ||
@@ -114,12 +116,10 @@ constructor (key) { | ||
* @param {string} password - The password to read the encrypted PEM | ||
* @param {string} [format] - Defaults to 'pkcs-8'. | ||
* @param {string} [format=pkcs-8] - The format in which to export as | ||
*/ | ||
async export (password, format = 'pkcs-8') { // eslint-disable-line require-await | ||
let pem = null | ||
if (format === 'pkcs-8') { | ||
const buffer = new forge.util.ByteBuffer(this.marshal()) | ||
const asn1 = forge.asn1.fromDer(buffer) | ||
const privateKey = forge.pki.privateKeyFromAsn1(asn1) | ||
const buffer = new forge.util.ByteBuffer(this.marshal()) | ||
const asn1 = forge.asn1.fromDer(buffer) | ||
const privateKey = forge.pki.privateKeyFromAsn1(asn1) | ||
if (format === 'pkcs-8') { | ||
const options = { | ||
@@ -131,8 +131,8 @@ algorithm: 'aes256', | ||
} | ||
pem = forge.pki.encryptRsaPrivateKey(privateKey, password, options) | ||
return forge.pki.encryptRsaPrivateKey(privateKey, password, options) | ||
} else if (format === 'libp2p-key') { | ||
return exporter.export(this.bytes, password) | ||
} else { | ||
throw errcode(new Error(`Unknown export format '${format}'. Must be pkcs-8`), 'ERR_INVALID_EXPORT_FORMAT') | ||
throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT') | ||
} | ||
return pem | ||
} | ||
@@ -139,0 +139,0 @@ } |
@@ -11,2 +11,3 @@ 'use strict' | ||
exports.pkcs1ToJwk = function (bytes) { | ||
bytes = Buffer.from(bytes) // convert Uint8Arrays | ||
const asn1 = forge.asn1.fromDer(bytes.toString('binary')) | ||
@@ -13,0 +14,0 @@ const privateKey = forge.pki.privateKeyFromAsn1(asn1) |
@@ -5,3 +5,6 @@ 'use strict' | ||
const sha = require('multihashing-async/src/sha') | ||
const errcode = require('err-code') | ||
const exporter = require('./exporter') | ||
module.exports = (keysProtobuf, randomBytes, crypto) => { | ||
@@ -88,2 +91,17 @@ crypto = crypto || require('./secp256k1')(randomBytes) | ||
} | ||
/** | ||
* Exports the key into a password protected `format` | ||
* | ||
* @param {string} password - The password to encrypt the key | ||
* @param {string} [format=libp2p-key] - The format in which to export as | ||
* @returns {Promise<string>} The encrypted private key | ||
*/ | ||
async export (password, format = 'libp2p-key') { // eslint-disable-line require-await | ||
if (format === 'libp2p-key') { | ||
return exporter.export(this.bytes, password) | ||
} else { | ||
throw errcode(new Error(`export format '${format}' is not supported`), 'ERR_INVALID_EXPORT_FORMAT') | ||
} | ||
} | ||
} | ||
@@ -90,0 +108,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
483188
10
39
1837
312
14
+ Addedmulticodec@^1.0.4
+ Addeduint8arrays@^1.0.0
+ Added@multiformats/base-x@4.0.1(transitive)
+ Added@zxing/text-encoding@0.9.0(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedmultibase@3.1.2(transitive)
+ Addedmulticodec@1.0.4(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addeduint8arrays@1.1.0(transitive)
+ Addedutil@0.12.5(transitive)
+ Addedweb-encoding@1.1.5(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
- Removedmultibase@0.7.0(transitive)
Updatedmultibase@^1.0.1
Updatedprotons@^1.2.1
Updatedursa-optional@^0.10.1