<commit_before>import forge from 'node-forge';

const forgePki = forge.pki;
const forgeRsa = forge.pki.rsa;


function generateRsa(cb) {
  return forgeRsa.generateKeyPair({ bits: 2048, workers: -1 }, (err, keypair) => {
    if (err) {
      return cb(err);
    }

    return cb(null, {
      private: forgePki.privateKeyToPem(keypair.privateKey),
      public: forgePki.publicKeyToPem(keypair.publicKey)
    });
  });
}

function generateSignature(content, keypair, cb) {
  const md = forge.md.sha1.create();
  const privateKey = forgePki.privateKeyfromPem(keypair.private);
  md.update(content, 'utf8');
  const signature = privateKey.sign(md);
  return cb(null, signature, content);
}

export { generateRsa as default, generateSignature };<commit_msg>Add some more encryption helpers<commit_after>import forge from 'node-forge';

const forgePki = forge.pki;
const forgeRsa = forge.pki.rsa;


function generateRsa(cb) {
  return forgeRsa.generateKeyPair({ bits: 2048, workers: -1 }, (err, keypair) => {
    if (err) {
      return cb(err);
    }

    return cb(null, {
      private: forgePki.privateKeyToPem(keypair.privateKey),
      public: forgePki.publicKeyToPem(keypair.publicKey)
    });
  });
}

function generateSignature(content, keypair, cb) {
  const md = forge.md.sha1.create();
  const privateKey = forgePki.privateKeyFromPem(keypair.private);
  md.update(content, 'utf8');
  const signature = privateKey.sign(md);
  return cb(null, signature, content);
}

function validateSignature(signature, data, keypair, cb) {
  const publicKey = forgePki.publicKeyFromPem(keypair.public);
  const verified = publicKey.verify(data, signature);
  return cb(null, verified);
}

function encryptData(keypair, data, method = 'RSAES-PKCS1-V1_5', cb) {
  if (typeof method === 'function') {
    cb = method;  // eslint-disable-line no-param-reassign
  }
  const publicKey = forgePki.publicKeyFromPem(keypair.public);
  const encrypted = publicKey.encrypt(data, method);
  return cb(null, encrypted);
}

function decryptData(keypair, data, method = 'RSAES-PKCS1-V1_5', cb) {
  if (typeof method === 'function') {
    cb = method;  // eslint-disable-line no-param-reassign
  }
  const privateKey = forgePki.privateKeyFromPem(keypair.private);
  const decrypted = privateKey.decrypt(data, method);
  return cb(null, decrypted);
}

export { generateRsa as default, generateSignature, validateSignature, encryptData, decryptData };
{'fraction_non_alphanumeric': 0.13036303630363036, 'fraction_numerical': 0.00825082508250825, 'mean_word_length': 4.449438202247191, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1520538', 'n_tokens_mistral': 864, 'n_tokens_neox': 829, 'n_words': 178}