1.1.0 • Published 7 months ago

@juanelas/aes-gcm v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

License: MIT Contributor Covenant JavaScript Style Guide Node.js CI

@juanelas/aes-gcm

Easy to use aes-gcm cipher for node.js and browser.

In order to facilitate ciphertext exchange (encrypted data and IV), the generated ciphertext object (obtained after encrypting) can be JSON.stringified. The decrypt method accept as well the JSON version of the ciphertext.

Install

npm install @juanelas/aes-gcm

Usage

Either require (Node.js CJS):

const aesGcm = require('@juanelas/aes-gcm')

or import (TypeScript or JavaScript ES module):

import * as aesGcm from '@juanelas/aes-gcm'

An example code for encryption and decryption could be:

const key = await aesGcm.generateKey(256, false) // create a non-extractable random key of 256 bits

const mStr = 'my-plaintext-msg'
const m = (new TextEncoder()).encode(mStr) // convert to buffer

// encrypt m with key
const c = await aesGcm.encrypt(m, key)

// decrypt c with key
const d = await aesGcm.decrypt(c, key)
const dStr = (new TextDecoder()).decode(d) // convert back to utf-8 string 'my-plaintext-msg'

// to ease ciphertext exchange, you could JSON.stringify it
const cStr = JSON.stringify(c)

 // The JSON.stringified ciphertext can as well be directly decrypted
const d = await aesGcm.decrypt(cStr, key)
const dStr = (new TextDecoder()).decode(d) // convert back to utf-8 string 'my-plaintext-msg'

// If needed, you could recover the Ciphertext object (with iv and encrypted) from cStr
const c2 = aesGcm.Ciphertext.fromJSON(cStr)

API reference documentation

Check the API

1.1.0

7 months ago

1.0.1

7 months ago