|
| 1 | +[](http://libp2p.io/) |
| 2 | +[](https://discuss.libp2p.io) |
| 3 | +[](https://codecov.io/gh/libp2p/js-libp2p) |
| 4 | +[](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain) |
| 5 | + |
| 6 | +> Streaming AES-CTR for node and browsers |
| 7 | +
|
| 8 | +# About |
| 9 | + |
| 10 | +WebCrypto does not support streaming encryption - <https://github.com/w3c/webcrypto/issues/73> |
| 11 | + |
| 12 | +In browsers this module uses `node-forge` to expose a streaming interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197. |
| 13 | + |
| 14 | +In node.js it uses the regular streaming API exported by the `crypto` module. |
| 15 | + |
| 16 | +This uses `CTR` mode. |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +```js |
| 21 | +import { create } from '@libp2p/crypto-aes-ctr' |
| 22 | + |
| 23 | +// Setting up Key and IV |
| 24 | + |
| 25 | +// A 16 bytes array, 128 Bits, AES-128 is chosen |
| 26 | +const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) |
| 27 | + |
| 28 | +// A 16 bytes array, 128 Bits, |
| 29 | +const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) |
| 30 | + |
| 31 | +const decryptedMessage = 'Hello, world!' |
| 32 | + |
| 33 | +// Encrypting |
| 34 | +const cipher = await crypto.aes.create(key128, IV) |
| 35 | +const encryptedBuffer = await encrypt(Uint8Array.from(decryptedMessage)) |
| 36 | +console.log(encryptedBuffer) |
| 37 | +// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c> |
| 38 | + |
| 39 | +// Decrypting |
| 40 | +const decipher = await crypto.aes.create(key128, IV) |
| 41 | +const decryptedBuffer = await decrypt(encryptedBuffer) |
| 42 | + |
| 43 | +console.log(decryptedBuffer) |
| 44 | +// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c> |
| 45 | + |
| 46 | +console.log(decryptedBuffer.toString('utf-8')) |
| 47 | +// prints: Hello, world! |
| 48 | +``` |
| 49 | + |
| 50 | +# Install |
| 51 | + |
| 52 | +```console |
| 53 | +$ npm i @libp2p/crypto-aes-ctr |
| 54 | +``` |
| 55 | + |
| 56 | +## Browser `<script>` tag |
| 57 | + |
| 58 | +Loading this module through a script tag will make it's exports available as `Libp2pCryptoAesCtr` in the global namespace. |
| 59 | + |
| 60 | +```html |
| 61 | +<script src="https://unpkg.com/@libp2p/crypto-aes-ctr/dist/index.min.js"></script> |
| 62 | +``` |
| 63 | + |
| 64 | +# API Docs |
| 65 | + |
| 66 | +- <https://libp2p.github.io/js-libp2p/modules/_libp2p_crypto_aes_ctr.html> |
| 67 | + |
| 68 | +# License |
| 69 | + |
| 70 | +Licensed under either of |
| 71 | + |
| 72 | +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) |
| 73 | +- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>) |
| 74 | + |
| 75 | +# Contribution |
| 76 | + |
| 77 | +Unless 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. |
0 commit comments