@libp2p/aes-ctr
Version:
Streaming AES-CTR for node and browsers
95 lines (59 loc) • 3.25 kB
Markdown
[](http://libp2p.io/)
[](https://discuss.libp2p.io)
[](https://codecov.io/gh/libp2p/js-libp2p-aes-ctr)
[](https://github.com/libp2p/js-libp2p-aes-ctr/actions/workflows/js-test-and-release.yml?query=branch%3Amain)
> Streaming AES-CTR for node and browsers
<!--
!IMPORTANT!
Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.
To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts
To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.
-->
WebCrypto does not support streaming encryption - <https://github.com/w3c/webcrypto/issues/73>
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.
In node.js it uses the regular streaming API exported by the `crypto` module.
This uses `CTR` mode.
## Example
```js
import { create } from '@libp2p/aes-ctr'
// Setting up Key and IV
// A 16 bytes array, 128 Bits, AES-128 is chosen
const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
// A 16 bytes array, 128 Bits,
const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
const decryptedMessage = 'Hello, world!'
// Encrypting
const cipher = create(key128, IV)
const encryptedBuffer = cipher.encrypt(Uint8Array.from(decryptedMessage))
console.log(encryptedBuffer)
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
// Decrypting
const decipher = create(key128, IV)
const decryptedBuffer = decipher.decrypt(encryptedBuffer)
console.log(decryptedBuffer)
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
console.log(decryptedBuffer.toString('utf-8'))
// prints: Hello, world!
```
```console
$ npm i @libp2p/aes-ctr
```
Loading this module through a script tag will make its exports available as `Libp2pAesCtr` in the global namespace.
```html
<script src="https://unpkg.com/@libp2p/aes-ctr/dist/index.min.js"></script>
```
- <https://libp2p.github.io/js-libp2p-aes-ctr>
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](https://github.com/libp2p/js-libp2p-aes-ctr/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/libp2p/js-libp2p-aes-ctr/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
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.