@bicycle-codes/simple-aes
Version:
An easy way to use symmetric keys in browsers or node
150 lines (114 loc) • 3.98 kB
Markdown
//github.com/bicycle-codes/simple-aes/actions/workflows/nodejs.yml/badge.svg)
[](README.md)
[](README.md)
[](https://semver.org/)
[](https://packagephobia.com/result?p=@bicycle-codes/simple-aes)
[](LICENSE)
Cryptography used by [vanishing.page](https://vanishing.page/). Works in browsers and node.
This is generally useful as a dead simple way of working with symmetric keys in a browser or node.
Thanks to [Fission](https://github.com/fission-codes/), the original author for much of this code.
<!-- toc -->
- [install](
- [use](
* [bundler](
* [pre-bundled](
- [API](
* [`@bicycle-codes/simple-aes`](
* [`@bicycle-codes/simple-aes/compat`](
* [`encryptMessage`](
* [`decryptMessage`](
<!-- tocstop -->
```sh
npm i -S @bicycle-codes/simple-aes
```
Just import
```js
import {
decryptMessage,
encryptMessage,
type Message
} from '@bicycle-codes/simple-aes'
// } from '@bicycle-codes/simple-aes/compat' // for older browsers
```
This exposes pre-bundled & minified JS files.
```sh
cp ./node_modules/@bicycle-codes/simple-aes/dist/compat.min.js ./public
cp ./node_modules/@bicycle-codes/simple-aes/dist/index.min.js ./public/simple-aes.min.js
```
```html
<body>
<!-- ... -->
<script type="module" src="./compat.min.js"></script>
<!-- or webcrypto version -->
<script type="module" src="./simple-aes.min.js"></script>
</body>
```
Use the `webcrypto` API.
```js
import {
decryptMessage,
encryptMessage,
type Message
} from '@bicycle-codes/simple-aes'
```
Use a user-land module, [@noble/ciphers](https://github.com/paulmillr/noble-ciphers). This will work in browsers of all ages.
```js
import {
encryptMessage,
decryptMessage
} from '@bicycle-codes/simple-aes/compat'
```
Generate a new AES key and encrypt the given message object.
Return an array of `[ encryptedMessage, { key }]`,
where `key` is a new AES key, encoded as `base64url`.
Can pass in a size for the key. By default it uses 256 bits.
```ts
async function encryptMessage (
msg:{ content:string },
opts:{ length:SymmKeyLength } = { length: DEFAULT_SYMM_LEN }
):Promise<[{ content:string }, { key:string }]>
```
```ts
import { SymmKeyLength, encryptMessage } from '@bicycle-codes/simple-aes'
const [encryptedMsg, { key }] = await encryptMessage({
content: 'hello world'
})
// or pass in a key size
await encryptMessage({
content: 'hello'
}, {
length: SymmKeyLength.B128
})
console.log(encryptedMessage)
// => { content: '5eAcA6+jnBfbuCx8L...' }
```
Decrypt the given message with the given key. Suitable for decrypting a message that was encrypted by this library. Key is an AES key, `base64url` encoded.
```ts
async function decryptMessage (
msg:{ content:string },
keyString:string // <-- base64url
):Promise<{ content:string }>
```
```js
import { test } from '@bicycle-codes/tapzero'
import { decryptMessage } from '@bicycle-codes/simple-aes'
test('decrypt the message', async t => {
const decrypted = await decryptMessage(message, key)
t.equal(decrypted.content, 'hello world',
'should decrypt to the right text')
})
```
![tests](https: