{"size": 1339, "ext": "js", "max_stars_repo_path": "app/utils/crypto.utils.js", "max_stars_repo_name": "ypcloud/console-server", "max_stars_repo_stars_event_min_datetime": "2018-03-09T11:37:02.000Z", "max_stars_repo_stars_event_max_datetime": "2019-06-05T06:22:49.000Z", "max_issues_repo_path": "app/utils/crypto.utils.js", "max_issues_repo_name": "ypcloud/console-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/utils/crypto.utils.js", "max_forks_repo_name": "ypcloud/console-server", "max_forks_repo_forks_event_min_datetime": "2018-07-05T14:10:46.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-05T14:10:46.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 26.2549019608, "max_line_length": 85, "alphanum_fraction": 0.6594473488}
/**
 *
 * @main
 * @author: Mark Massoud
 * @description: AES responsible for encrypting/decrypting sensitive information data
 *               https://nodejs.org/api/crypto.html
 */

const crypto = require('crypto');
const MODE = 'aes-256-ctr';   // CTR is considered secure. DO NOT USE ECB.

module.exports = {

  /**
   * Encrypts a given text. Returns the result in the callback.
   *
   * @class AES
   * @method encrypt
   * @param plainText {String} Text to encrypt
   * @param secret {String} Symmetric password to encrypt with
   * @return encrypted {String} Encrypted plainText
   */
  encrypt: (plainText, secret) => {
    const cipher = crypto.createCipher(MODE, secret);

    let encrypted = cipher.update(plainText, 'utf8', 'hex');
    encrypted += cipher.final('hex');

    return encrypted;
  },

  /**
   * Decrypts a given cipher. Returns the result in the callback.
   *
   * @class AES
   * @method decrypt
   * @param cipherText {String} Encrypted text to decrypt
   * @param secret {String} Symmetric password to decrypt with
   * @return decrypted {String} Decrypted cipherText
   */
  decrypt: (cipherText, secret) => {
    const decipher = crypto.createDecipher(MODE, secret);

    let decrypted = decipher.update(cipherText, 'hex', 'utf8');
    decrypted += decipher.final('utf8');

    return decrypted;
  }

};
