https://github.com/dchest/xaes
JavaScript XAES-256-GCM implementation using Web Cryptography API
https://github.com/dchest/xaes
Last synced: 3 months ago
JSON representation
JavaScript XAES-256-GCM implementation using Web Cryptography API
- Host: GitHub
- URL: https://github.com/dchest/xaes
- Owner: dchest
- License: mit
- Created: 2024-06-27T19:37:46.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-09T18:13:05.000Z (almost 2 years ago)
- Last Synced: 2025-03-18T08:11:15.453Z (over 1 year ago)
- Language: JavaScript
- Size: 43 KB
- Stars: 14
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xaes
Implementation of XAES-256-GCM as defined in
based on the Web Cryptography API .
It uses a 256-bit AES-CBC CryptoKey and a 192-bit nonce to derive
a 256-bit key and a 96-bit nonce for AES-GCM.
Due to the use of the standard CryptoKey and Web Cryptography API operations,
this implementation is fully compatible with other parts of the Web Cryptography API.
For example, keys can be stored in IndexedDB and be non-extractable. The only
additional requirement is that the key must have 'encrypt' usage even for decryption,
however, there's no real distinction between encryption and decryption operations
for AES-GCM anyway (you can simulate decryption by encrypting the ciphertext).
## Installation
```
npm install xaes
```
## Example
```javascript
import { encrypt, decrypt, importKey } from 'xaes';
(async () => {
const keyBytes = new Uint8Array(32).fill(0x1);
const key = await importKey('raw', keyBytes, true);
const iv = new Uint8Array(24).fill(0x2);
const plaintext = new TextEncoder().encode('Hello, World!');
const additionalData = new TextEncoder().encode('Additional Data');
const ciphertext = await encrypt({ iv, additionalData }, key, plaintext);
const decrypted = await decrypt({ iv, additionalData }, key, ciphertext);
console.log(new TextDecoder().decode(decrypted)); // Hello, World!
})();
```