alexx876

Untitled

Feb 16th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var crypto = require('crypto'); var CIPHER_ALGORITHM = 'aes-256-ctr';  function encrypt(key, input) { if (typeof key !== 'string' || !key) { throw new TypeError('Provided "key" must be a non-empty string'); } var isString = typeof input === 'string'; var isBuffer = Buffer.isBuffer(input); if (!(isString || isBuffer) || (isString && !input) || (isBuffer && !Buffer.byteLength(input))) { throw new TypeError('Provided "input" must be a non-empty string or buffer'); } var sha256 = crypto.createHash('sha256'); sha256.update(key); var iv = crypto.randomBytes(16); var cipher = crypto.createCipheriv(CIPHER_ALGORITHM, sha256.digest(), iv); var buffer = input; if (isString) { buffer = Buffer.from(input); } var ciphertext = cipher.update(buffer); var encrypted = Buffer.concat([iv, ciphertext, cipher.final()]); if (isString) { encrypted = encrypted.toString('base64'); } return encrypted; }
  2.  
  3.  
  4. const KEY = 'ПРИВАТНЫЙ_КЛЮЧ';
  5. const SALT = 'КЛЮЧ ДЛЯ ШИФРОВАНИЯ';
  6.  
  7. console.log('Приватный ключ - ');
  8. console.log(encrypt(SALT,KEY))
Add Comment
Please, Sign In to add comment