noble-secretbox-aes-gcm
secretbox — authenticated data encryption with AES-GCM.
Allows to encrypt arbitrary data in a cryptographically secure & modern way.
This library belongs to noble crypto
noble-crypto — high-security, easily auditable set of contained cryptographic libraries and tools.
- No dependencies, one small file
- Easily auditable TypeScript/JS code
- Uses es2019 bigint. Supported in Chrome, Firefox, node 10+
- All releases are signed and trusted
- Check out all libraries: secp256k1, ed25519, ripemd160, secretbox-aes-gcm
Usage
npm install noble-secretbox-aes-gcm
;const key = Uint8Array;const plaintext = "Hello world";const ciphertext = await ;const plaintext = await ;console;// Also works in browsers
API
;
plaintext
in encrypt
can be either a Uint8Array, or a string. If it's a string,
new TextDecoder().encode(plaintext)
would be executed before passing it further.
;
Note that decrypt
always returns Uint8Array
. If you've encrypted UTF-8 string,
toUTF8(result)
should be enough to get it back.
Internals
Secretbox receives one key, and one plaintext.
The output format is: iv + ciphertext + mac
:
iv
is 12 bytes; it's an initialization vector for AES-GCM mode.ciphertext
length depends on plaintextmac
is 16 bytes; AES-GCM calculates this authentication tag for us.
To slice through IV and MAC, you can use Uint8Array.prototype.slice()
:
const ciphertext = await ;const iv = ciphertext;const mac = ciphertext;
Security
Noble is production-ready & secure. Our goal is to have it audited by a good security expert.
DJB's secretbox uses XSalsa20-Poly1305. We'll use AES-GCM, which is also a good choice. DJB mentioned the AES box in his TODOs.
AES has been selected over Salsa, because it's natively implemented in Node & browsers and doesn't require any 3rd-party libraries.
License
MIT (c) Paul Miller (https://paulmillr.com), see LICENSE file.