'use strict';

const crypto = require('crypto')

const ALGORITHM = 'aes-256-gcm'
const BLOCK_SIZE_BYTES = 16; // 128 bit

/**
 * Provides easy encryption/decryption methods using AES 256 GCM.
 */
class Aes256Gcm {
  /**
   * No need to run the constructor. The class only has static methods.
   */
  constructor() { }

  /**
   * Encrypts text with AES 256 GCM.
   * @param {string} text - Cleartext to encode.
   * @param {string} secret - Shared secret key, must be 32 bytes.
   * @returns {object}
   */
  static encrypt(text, secret) {
    const iv = crypto.randomBytes(BLOCK_SIZE_BYTES);
    const cipher = crypto.createCipheriv(ALGORITHM, secret, iv);

    let ciphertext = cipher.update(text, 'utf8', 'base64');
    ciphertext  += cipher.final('base64');
    let finaltext  = ciphertext.substr(0, 32);
    console.log()
    return {
      finaltext,
      iv: iv.toString('base64'),
      tag: cipher.getAuthTag().toString('base64'),
    };
  }

  /**
   * Decrypts AES 256 CGM encrypted text.
   * @param {string} ciphertext - Base64-encoded ciphertext.
   * @param {string} iv - The base64-encoded initialization vector.
   * @param {string} tag - The base64-encoded authentication tag generated by getAuthTag().
   * @param {string} secret - Shared secret key, must be 32 bytes.
   * @returns {string}
   */
  static decrypt(ciphertext, iv, tag, secret) {
    const decipher = crypto.createDecipheriv(ALGORITHM, secret, Buffer.from(iv, 'base64'));
    decipher.setAuthTag(Buffer.from(tag, 'base64'));

    let cleartext = decipher.update(ciphertext, 'base64', 'utf8');
    cleartext += decipher.final('utf8');

    return cleartext;
  }
}

// Must be 32 bytes.
const SHARED_SECRET = '4d885c79-7c5e-41de-80eb-7c1e636b2ff4';
 
// Encrypt:
let test = Aes256Gcm.encrypt('hi', SHARED_SECRET.replace(/-/g, ""));
let test2 = JSON.stringify(test)
console.log(test)
console.log(test2)

let test3 = JSON.parse(test2)
console.log(test3)

let cleartext = Aes256Gcm.decrypt(test.ciphertext, test.iv, test.tag, SHARED_SECRET);
console.log(cleartext)


// module.exports = Aes256Gcm;
 

NodeJS Online Compiler

Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json.

About NodeJS

Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.

Key features

  • Built on Google chrome's javascript engine V8 and is pretty fast.
  • Node.js was developed by Ryan Dahl in 2009.
  • Server-side platform for building fast and scalable applications.
  • Node.js is Asynchronous, event-driven and works on single-thread model thus eliminating the dis-advantages of multi-thread model.
  • Supports various platforms like Windows, Linux, MacOS etc.
  • Provides rich library of java script modules which simplifies the development efforts.
  • Released under MIT license.

Express Framework

Express is one of the most popular web application framework in the NodeJS echosystem.

  • Pretty fast
  • Minimalist
  • Unopinionated
  • Very flexible

Syntax help

Examples

Using Moment

let moment = require('moment');

console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));

Using Lodash

const _ = require("lodash");

let colors = ['blue', 'green', 'yellow', 'red'];

let firstElement = _.first(colors);
let lastElement = _.last(colors);

console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);

Libraries supported

Following are the libraries supported by OneCompiler's NodeJS compiler.

  • lodash
  • moment
  • underscore
  • uuid
  • ejs
  • md5
  • url