Skip to content

Instantly share code, notes, and snippets.

View shahinghasemi's full-sized avatar

Shahin Ghasemi shahinghasemi

View GitHub Profile
@shahinghasemi
shahinghasemi / encrypt.js
Last active October 18, 2023 08:48
FullStack AES-GCM Encryption Decryption using NodeJS and Browser standard libraries (native crypto API)
//----------------------------------------------------------------
// ------------------- ServerSide(NODE.JS) -------------------
//----------------------------------------------------------------
function encrypt(message){
const KEY = crypto.randomBytes(32)
const IV = crypto.randomBytes(16)
const ALGORITHM = 'aes-256-gcm';
const cipher = crypto.createCipheriv(ALGORITHM, KEY, IV);
let encrypted = cipher.update(message, 'utf8', 'hex');