Skip to content

Instantly share code, notes, and snippets.

@kool79
Forked from rjz/crypto-aes-256-gcm-demo.js
Created February 19, 2021 23:51
Show Gist options
  • Save kool79/b1d06af85985d1e21f9c290c9166a07b to your computer and use it in GitHub Desktop.
Save kool79/b1d06af85985d1e21f9c290c9166a07b to your computer and use it in GitHub Desktop.

Revisions

  1. @rjz rjz revised this gist Sep 19, 2017. No changes.
  2. @rjz rjz renamed this gist Sep 19, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @rjz rjz created this gist Sep 19, 2017.
    44 changes: 44 additions & 0 deletions demo.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    const buffer = require('buffer');
    const crypto = require('crypto');

    // Demo implementation of using `aes-256-gcm` with node.js's `crypto` lib.
    const aes256gcm = (key) => {
    const ALGO = 'aes-256-gcm';

    // encrypt returns base64-encoded ciphertext
    const encrypt = (str) => {
    // Hint: the `iv` should be unique (but not necessarily random).
    // `randomBytes` here are (relatively) slow but convenient for
    // demonstration.
    const iv = new Buffer(crypto.randomBytes(16), 'utf8');
    const cipher = crypto.createCipheriv(ALGO, key, iv);

    // Hint: Larger inputs (it's GCM, after all!) should use the stream API
    let enc = cipher.update(str, 'utf8', 'base64');
    enc += cipher.final('base64');
    return [enc, iv, cipher.getAuthTag()];
    };

    // decrypt decodes base64-encoded ciphertext into a utf8-encoded string
    const decrypt = (enc, iv, authTag) => {
    const decipher = crypto.createDecipheriv(ALGO, key, iv);
    decipher.setAuthTag(authTag);
    let str = decipher.update(enc, 'base64', 'utf8');
    str += decipher.final('utf8');
    return str;
    };

    return {
    encrypt,
    decrypt,
    };
    };

    const KEY = new Buffer(crypto.randomBytes(32), 'utf8');

    const aesCipher = aes256gcm(KEY);

    const [encrypted, iv, authTag] = aesCipher.encrypt('hello, world');
    const decrypted = aesCipher.decrypt(encrypted, iv, authTag);

    console.log(decrypted); // 'hello, world'