Skip to content

Instantly share code, notes, and snippets.

@afifmakarim
Last active July 21, 2021 12:59
Show Gist options
  • Select an option

  • Save afifmakarim/be8e5cfa6b3134ae874a1dcc177a91ab to your computer and use it in GitHub Desktop.

Select an option

Save afifmakarim/be8e5cfa6b3134ae874a1dcc177a91ab to your computer and use it in GitHub Desktop.
AES-256-GCM - Custom AES256GCM with prefix & md5 secretKey
const crypto = require('crypto');
const encrypt = (text, secret, prefix) => {
// make buffer length 12
const iv = Buffer.alloc(12);
const key = crypto.createHash('md5').update(secret).digest("hex");
// AES 256 GCM Mode
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
// custom with prefix
const prefix = Buffer.from(prefix, "utf-8");
// encrypt the given text
const encrypted = Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]);
const enfix = Buffer.concat([prefix, encrypted]);
// extract the auth tag
const tag = cipher.getAuthTag();
// generate output
return Buffer.concat([enfix, tag]).toString('base64');
}
const payload = {
customerKey:"testing_testing",
customerNumber:"14045",
};
const prefix = "http_";
const secretKey = "secret";
const encrypted = encrypt(JSON.stringify(payload), secretKey, prefix);
console.log(encrypted);
// decrypt mode soon ...
// referensi https://gist.github.com/AndiDittrich/4629e7db04819244e843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment