{"name":"@libp2p/crypto","version":"2.0.3-e664d14f","keywords":["IPFS","crypto","libp2p","rsa","secp256k1"],"license":"Apache-2.0 OR MIT","_id":"@libp2p/crypto@2.0.3-e664d14f","homepage":"https://github.com/libp2p/js-libp2p/tree/master/packages/crypto#readme","bugs":{"url":"https://github.com/libp2p/js-libp2p/issues"},"dist":{"shasum":"f0a4536d4cf0eea5b7b7ec1d7bde3ce7d73cc3e4","tarball":"http://registry-npm.ywwl.com/@libp2p/crypto/download/@libp2p/crypto-2.0.3-e664d14f.tgz","fileCount":175,"integrity":"sha512-3LxB3Ciecltky7HPD4GIUdRWEdD9wf4iUf9Lrv151j6+Jxbu3l9b7MrcQkaqBtFROQC8d+h0DKp3jXwbZfgDDg==","signatures":[{"sig":"MEUCIC55pbN63WicwTHVjBDUVce6Fju8Jh56qQIZBVzr0VccAiEAtKa1ey7NfKYO+3iJrBoFNS6m7TuTG+tqAZv+g4vKOUs=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":495173,"size":129634,"noattachment":false,"key":"/@libp2p/crypto/-/@libp2p/crypto-2.0.3-e664d14f.tgz"},"type":"module","types":"./dist/src/index.d.ts","browser":{"./dist/src/keys/rsa.js":"./dist/src/keys/rsa-browser.js","./dist/src/keys/ecdh.js":"./dist/src/keys/ecdh-browser.js","./dist/src/hmac/index.js":"./dist/src/hmac/index-browser.js","./dist/src/aes/ciphers.js":"./dist/src/aes/ciphers-browser.js","./dist/src/keys/ed25519.js":"./dist/src/keys/ed25519-browser.js","./dist/src/ciphers/aes-gcm.js":"./dist/src/ciphers/aes-gcm.browser.js"},"exports":{".":{"types":"./src/index.d.ts","import":"./dist/src/index.js"},"./aes":{"types":"./dist/src/aes/index.d.ts","import":"./dist/src/aes/index.js"},"./hmac":{"types":"./dist/src/hmac/index.d.ts","import":"./dist/src/hmac/index.js"},"./keys":{"types":"./dist/src/keys/index.d.ts","import":"./dist/src/keys/index.js"}},"gitHead":"e664d14f9f28d148cb5bf7212f6c60ce452b98dd","scripts":{"lint":"aegir lint","test":"aegir test","build":"aegir build","clean":"aegir clean","generate":"protons ./src/keys/keys.proto","dep-check":"aegir dep-check","test:node":"aegir test -t node --cov","test:chrome":"aegir test -t browser","test:webkit":"aegir test -t browser -- --browser webkit","test:firefox":"aegir test -t browser -- --browser firefox","test:electron-main":"aegir test -t electron-main","test:chrome-webworker":"aegir test -t webworker","test:firefox-webworker":"aegir test -t webworker -- --browser firefox"},"_npmUser":{"name":"npm-service-account-libp2p","email":"npm-service-account+libp2p@protocol.ai"},"repository":{"url":"git+https://github.com/libp2p/js-libp2p.git","type":"git"},"_npmVersion":"9.8.1","description":"Crypto primitives for libp2p","directories":{},"_nodeVersion":"18.17.0","dependencies":{"node-forge":"^1.1.0","uint8arrays":"^4.0.6","multiformats":"^12.0.1","@noble/curves":"^1.1.0","@noble/hashes":"^1.3.1","uint8arraylist":"^2.4.3","protons-runtime":"^5.0.0","@libp2p/interface":"0.1.2-e664d14f"},"eslintConfig":{"extends":"ipfs","parserOptions":{"sourceType":"module"},"ignorePatterns":["src/*.d.ts"]},"typesVersions":{"*":{"*":["*","dist/*","dist/src/*","dist/src/*/index"],"src/*":["*","dist/*","dist/src/*","dist/src/*/index"]}},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"aegir":"^40.0.8","protons":"^7.0.2","benchmark":"^2.1.4","@types/mocha":"^10.0.0"},"_npmOperationalInternal":{"tmp":"tmp/crypto_2.0.3-e664d14f_1692089460964_0.7199772372443394","host":"s3://npm-registry-packages"},"readme":"# @libp2p/crypto <!-- omit in toc -->\n\n[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)\n[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)\n[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)\n[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)\n\n> Crypto primitives for libp2p\n\n## Table of contents <!-- omit in toc -->\n\n- [Install](#install)\n  - [Browser `<script>` tag](#browser-script-tag)\n- [Lead Maintainer](#lead-maintainer)\n- [Usage](#usage)\n  - [Web Crypto API](#web-crypto-api)\n- [API](#api)\n  - [`crypto.aes`](#cryptoaes)\n    - [`crypto.aes.create(key, iv)`](#cryptoaescreatekey-iv)\n      - [`decrypt(data)`](#decryptdata)\n      - [`encrypt(data)`](#encryptdata)\n  - [`crypto.hmac`](#cryptohmac)\n    - [`crypto.hmac.create(hash, secret)`](#cryptohmaccreatehash-secret)\n      - [`digest(data)`](#digestdata)\n  - [`crypto.keys`](#cryptokeys)\n  - [`crypto.keys.generateKeyPair(type, bits)`](#cryptokeysgeneratekeypairtype-bits)\n  - [`crypto.keys.generateEphemeralKeyPair(curve)`](#cryptokeysgenerateephemeralkeypaircurve)\n  - [`crypto.keys.keyStretcher(cipherType, hashType, secret)`](#cryptokeyskeystretcherciphertype-hashtype-secret)\n  - [`crypto.keys.marshalPublicKey(key, [type])`](#cryptokeysmarshalpublickeykey-type)\n  - [`crypto.keys.unmarshalPublicKey(buf)`](#cryptokeysunmarshalpublickeybuf)\n  - [`crypto.keys.marshalPrivateKey(key, [type])`](#cryptokeysmarshalprivatekeykey-type)\n  - [`crypto.keys.unmarshalPrivateKey(buf)`](#cryptokeysunmarshalprivatekeybuf)\n  - [`crypto.keys.import(encryptedKey, password)`](#cryptokeysimportencryptedkey-password)\n  - [`privateKey.export(password, format)`](#privatekeyexportpassword-format)\n  - [`crypto.randomBytes(number)`](#cryptorandombytesnumber)\n  - [`crypto.pbkdf2(password, salt, iterations, keySize, hash)`](#cryptopbkdf2password-salt-iterations-keysize-hash)\n- [Contribute](#contribute)\n- [API Docs](#api-docs)\n- [License](#license)\n- [Contribution](#contribution)\n\n## Install\n\n```console\n$ npm i @libp2p/crypto\n```\n\n### Browser `<script>` tag\n\nLoading this module through a script tag will make it's exports available as `Libp2pCrypto` in the global namespace.\n\n```html\n<script src=\"https://unpkg.com/@libp2p/crypto/dist/index.min.js\"></script>\n```\n\nThis 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).\n\n## Lead Maintainer\n\n[Jacob Heun](https://github.com/jacobheun/)\n\n## Usage\n\n```js\nconst crypto = require('libp2p-crypto')\n\n// Now available to you:\n//\n// crypto.aes\n// crypto.hmac\n// crypto.keys\n// etc.\n//\n// See full API details below...\n```\n\n### Web Crypto API\n\nThe `libp2p-crypto` library depends on the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in the browser. Web Crypto is available in all modern browsers, however browsers restrict its usage to [Secure Contexts](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts).\n\n**This means you will not be able to use some `libp2p-crypto` functions in the browser when the page is served over HTTP.** To enable the Web Crypto API and allow `libp2p-crypto` to work fully, please serve your page over HTTPS.\n\n## API\n\n### `crypto.aes`\n\nExposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.\n\nThis uses `CTR` mode.\n\n#### `crypto.aes.create(key, iv)`\n\n- `key: Uint8Array` The key, if length `16` then `AES 128` is used. For length `32`, `AES 256` is used.\n- `iv: Uint8Array` Must have length `16`.\n\nReturns `Promise<{decrypt<Function>, encrypt<Function>}>`\n\n##### `decrypt(data)`\n\n- `data: Uint8Array`\n\nReturns `Promise<Uint8Array>`\n\n##### `encrypt(data)`\n\n- `data: Uint8Array`\n\nReturns `Promise<Uint8Array>`\n\n```js\nconst crypto = require('libp2p-crypto')\n\n// Setting up Key and IV\n\n// A 16 bytes array, 128 Bits, AES-128 is chosen\nconst key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])\n\n// A 16 bytes array, 128 Bits,\nconst IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])\n\nasync function main () {\n  const decryptedMessage = 'Hello, world!'\n\n  // Encrypting\n  const cipher = await crypto.aes.create(key128, IV)\n  const encryptedBuffer = await cipher.encrypt(Uint8Array.from(decryptedMessage))\n  console.log(encryptedBuffer)\n  // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>\n\n  // Decrypting\n  const decipher = await crypto.aes.create(key128, IV)\n  const decryptedBuffer = await cipher.decrypt(encryptedBuffer)\n\n  console.log(decryptedBuffer)\n  // prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>\n\n  console.log(decryptedBuffer.toString('utf-8'))\n  // prints: Hello, world!\n}\n\nmain()\n```\n\n### `crypto.hmac`\n\nExposes 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.\n\n#### `crypto.hmac.create(hash, secret)`\n\n- `hash: String`\n- `secret: Uint8Array`\n\nReturns `Promise<{digest<Function>}>`\n\n##### `digest(data)`\n\n- `data: Uint8Array`\n\nReturns `Promise<Uint8Array>`\n\nExample:\n\n```js\nconst crypto = require('libp2p-crypto')\n\nasync function main () {\n  const hash = 'SHA1' // 'SHA256' || 'SHA512'\n  const hmac = await crypto.hmac.create(hash, uint8ArrayFromString('secret'))\n  const sig = await hmac.digest(uint8ArrayFromString('hello world'))\n  console.log(sig)\n}\n\nmain()\n```\n\n### `crypto.keys`\n\n**Supported Key Types**\n\nThe [`generateKeyPair`](#generatekeypairtype-bits), [`marshalPublicKey`](#marshalpublickeykey-type), and [`marshalPrivateKey`](#marshalprivatekeykey-type) functions accept a string `type` argument.\n\nCurrently the `'RSA'`, `'ed25519'`, and `secp256k1` types are supported, although ed25519 and secp256k1 keys support only signing and verification of messages.  For encryption / decryption support, RSA keys should be used.\n\n### `crypto.keys.generateKeyPair(type, bits)`\n\n- `type: String`, see [Supported Key Types](#supported-key-types) above.\n- `bits: Number` Minimum of 1024\n\nReturns `Promise<{privateKey<Uint8Array>, publicKey<Uint8Array>}>`\n\nGenerates a keypair of the given type and bitsize.\n\n### `crypto.keys.generateEphemeralKeyPair(curve)`\n\n- `curve: String`, one of `'P-256'`, `'P-384'`, `'P-521'` is currently supported\n\nReturns `Promise`\n\nGenerates an ephemeral public key and returns a function that will compute the shared secret key.\n\nFocuses only on ECDH now, but can be made more general in the future.\n\nResolves to an object of the form:\n\n```js\n{\n  key: Uint8Array,\n  genSharedKey: Function\n}\n```\n\n### `crypto.keys.keyStretcher(cipherType, hashType, secret)`\n\n- `cipherType: String`, one of `'AES-128'`, `'AES-256'`, `'Blowfish'`\n- `hashType: String`, one of `'SHA1'`, `SHA256`, `SHA512`\n- `secret: Uint8Array`\n\nReturns `Promise`\n\nGenerates a set of keys for each party by stretching the shared key.\n\nResolves to an object of the form:\n\n```js\n{\n  k1: {\n    iv: Uint8Array,\n    cipherKey: Uint8Array,\n    macKey: Uint8Array\n  },\n  k2: {\n    iv: Uint8Array,\n    cipherKey: Uint8Array,\n    macKey: Uint8Array\n  }\n}\n```\n\n### `crypto.keys.marshalPublicKey(key, [type])`\n\n- `key: keys.rsa.RsaPublicKey | keys.ed25519.Ed25519PublicKey | keys.secp256k1.Secp256k1PublicKey`\n- `type: String`, see [Supported Key Types](#supported-key-types) above.  Defaults to 'rsa'.\n\nReturns `Uint8Array`\n\nConverts a public key object into a protobuf serialized public key.\n\n### `crypto.keys.unmarshalPublicKey(buf)`\n\n- `buf: Uint8Array`\n\nReturns `RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey`\n\nConverts a protobuf serialized public key into its representative object.\n\n### `crypto.keys.marshalPrivateKey(key, [type])`\n\n- `key: keys.rsa.RsaPrivateKey | keys.ed25519.Ed25519PrivateKey | keys.secp256k1.Secp256k1PrivateKey`\n- `type: String`, see [Supported Key Types](#supported-key-types) above.\n\nReturns `Uint8Array`\n\nConverts a private key object into a protobuf serialized private key.\n\n### `crypto.keys.unmarshalPrivateKey(buf)`\n\n- `buf: Uint8Array`\n\nReturns `Promise<RsaPrivateKey|Ed25519PrivateKey|Secp256k1PrivateKey>`\n\nConverts a protobuf serialized private key into its representative object.\n\n### `crypto.keys.import(encryptedKey, password)`\n\n- `encryptedKey: string`\n- `password: string`\n\nReturns `Promise<PrivateKey>`\n\nConverts an exported private key into its representative object. Supported formats are 'pem' (RSA only) and 'libp2p-key'.\n\n### `privateKey.export(password, format)`\n\n- `password: string`\n- `format: string` the format to export to: 'pem' (rsa only), 'libp2p-key'\n\nReturns `string`\n\nExports 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).\n\n### `crypto.randomBytes(number)`\n\n- `number: Number`\n\nReturns `Uint8Array`\n\nGenerates a Uint8Array with length `number` populated by random bytes.\n\n### `crypto.pbkdf2(password, salt, iterations, keySize, hash)`\n\n- `password: String`\n- `salt: String`\n- `iterations: Number`\n- `keySize: Number` in bytes\n- `hash: String` the hashing algorithm ('sha1', 'sha2-512', ...)\n\nComputes the Password Based Key Derivation Function 2; returning a new password.\n\n## Contribute\n\nFeel free to join in. All welcome. Open an [issue](https://github.com/libp2p/js-libp2p-crypto/issues)!\n\nThis repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).\n\n[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/contributing.md)\n\n## API Docs\n\n- <https://libp2p.github.io/js-libp2p/modules/_libp2p_crypto.html>\n\n## License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)\n- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","publish_time":1692089461226,"maintainers":[{"name":"jacobheun","email":"jacobheun@gmail.com"},{"name":"npm-service-account-libp2p","email":"npm-service-account+libp2p@ipfs.tech"},{"name":"alanshaw","email":"alan138@gmail.com"},{"name":"vascosantos","email":"santos.vasco10@gmail.com"},{"name":"achingbrain","email":"alex@achingbrain.net"},{"name":"daviddias","email":"mail@daviddias.me"}],"_cnpm_publish_time":1692089461226,"dist-tags":{"latest":"5.2.0","next":"5.2.0-228942556"}}