A typed package for nodejs to simplify aes-256-gcm encryption and decryption.
npm install ryushar/aes-256-gcm
or
yarn add ryushar/aes-256-gcm
// commonjs
// const AES256GCM = require("@ryushar/aes-256-gcm").default;
import AES256GCM from "@ryushar/aes-256-gcm";
const SECRET_KEY = AES256GCM.createKey(); // or supply your own 32 byte key buffer
/**
* In a real scenario you may create a key once and save it.
* const SECRET_KEY_BASE64 = AES256GCM.createKey().toString("base64");
*
* And then in future use that saved key for your encryption / decryption tasks.
* const SECRET_KEY = Buffer.from(SECRET_KEY_BASE64, "base64");
*/
const text = "My secret text";
const buffer = Buffer.from(text, "utf8");
let encrypted_buffer = null;
try {
encrypted_buffer = AES256GCM.encryptSync(buffer, SECRET_KEY);
} catch (error) {
console.error("Encryption failed.\n", error);
}
if (encrypted_buffer !== null) {
let decrypted_buffer = null;
try {
decrypted_buffer = AES256GCM.decrypt(encrypted_buffer, SECRET_KEY);
console.log(decrypted_buffer.toString("utf8") === text); // true
} catch (error) {
console.error("Decryption failed.\n", error);
}
}
Returns a Buffer
object with random 32 bytes.
Returns a Promise
which resolves to a Buffer
object with the encrypted content.
Returns a Buffer
object with the encrypted content.
Returns a Buffer
object with the decrypted content.