How to use the node-forge.random function in node-forge

To help you get started, we’ve selected a few node-forge examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sx1989827 / DOClever / Desktop / node_modules / selfsigned / index.js View on Github external
var generatePem = function (keyPair) {
    var cert = forge.pki.createCertificate();

    cert.serialNumber = toPositiveHex(forge.util.bytesToHex(forge.random.getBytesSync(9))); // the serial number can be decimal or hex (if preceded by 0x)

    cert.validity.notBefore = new Date();
    cert.validity.notAfter = new Date();
    cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + (options.days || 365));

    attrs = attrs || [{
      name: 'commonName',
      value: 'example.org'
    }, {
      name: 'countryName',
      value: 'US'
    }, {
      shortName: 'ST',
      value: 'Virginia'
    }, {
      name: 'localityName',
github digitalbazaar / did-io / lib / ld-key-pair.js View on Github external
const iterations = 4096;
    const key = await LDKeyPair.pbkdf2(password, salt, iterations, keySize);

    const jweHeader = {
      alg: 'PBES2-A128GCMKW',
      enc: 'A128GCMKW',
      jwk: {
        kty: 'PBKDF2',
        s: didUtil.encodeBase64Url(salt, {forge}),
        c: iterations
      }
    };

    // FIXME: this probably needs to be cleaned up/made more standard

    const iv = forge.random.getBytesSync(12);
    const cipher = forge.cipher.createCipher('AES-GCM', key);
    cipher.start({iv});
    cipher.update(forge.util.createBuffer(JSON.stringify(privateKey)));
    cipher.finish();
    const encrypted = cipher.output.getBytes();
    const tag = cipher.mode.tag.getBytes();

    const jwe = {
      unprotected: jweHeader,
      iv: didUtil.encodeBase64Url(iv, {forge}),
      ciphertext: didUtil.encodeBase64Url(encrypted, {forge}),
      tag: didUtil.encodeBase64Url(tag, {forge})
    };

    return jwe;
  }
github Mastercard / client-encryption-nodejs / lib / mcapi / crypto / crypto.js View on Github external
this.newEncryptionParams = function (options) {
    options = options || {};
    // Generate a random initialization vector (IV)
    const iv = options["iv"] || forge.random.getBytesSync(16);

    // Generate a secret key (should be 128 (or 256) bits)
    const secretKey = options["secretKey"] || generateSecretKey('AES', 128, 'SHA-256');

    // Encrypt secret key with issuer key
    const encryptedKey = this.encryptionCertificate.publicKey.encrypt(secretKey,
      'RSA-OAEP', createOAEPOptions('RSA-OAEP', this.oaepHashingAlgorithm));

    return {
      iv: iv,
      secretKey: secretKey,
      encryptedKey: encryptedKey,
      oaepHashingAlgorithm: this.oaepHashingAlgorithm,
      publicKeyFingerprint: this.publicKeyFingerprint,
      encoded: {
        iv: utils.bytesToString(iv, this.encoding),
github letsencrypt / boulder / test / js / crypto-util.js View on Github external
randomSerialNumber: function() {
    return forge.util.bytesToHex(forge.random.getBytesSync(4));
  },
github secretin / secretin-lib / server / index.js View on Github external
checkBruteforce(req.ip, req.params.hash, function(isBruteforce){
        var md = forge.md.sha256.create();
        md.update(req.params.hash + OPTIONAL_SALT);
        if(isBruteforce || md.digest().toHex() !== user.pass.hash){
          user.privateKey = {
            privateKey: forge.util.bytesToHex((forge.random.getBytesSync(3232))),
            iv: forge.util.bytesToHex((forge.random.getBytesSync(16)))
          };
          user.keys = {};
          user.pass.hash = forge.util.bytesToHex((forge.random.getBytesSync(32)));
        }

        db.users[req.params.name] = user;
        var hashedTitles = Object.keys(user.keys);
        if(hashedTitles.length !== 0){
          hashedTitles.forEach(function(hashedTitle){
            secretExists(hashedTitle, function(exists, secret){
              db.secrets[hashedTitle] = secret;
              db.secrets[hashedTitle].users = [req.params.name];
              if(Object.keys(db.secrets).length === hashedTitles.length){
                res.json(db);
              }
            });
          });
        }
        else{
github secretin / secretin-lib / server / index.js View on Github external
checkBruteforce(req.ip, req.params.hash, function(isBruteforce){
        var md = forge.md.sha256.create();
        md.update(req.params.hash + OPTIONAL_SALT);
        if(isBruteforce || md.digest().toHex() !== user.pass.hash){
          user.privateKey = {
            privateKey: forge.util.bytesToHex((forge.random.getBytesSync(3232))),
            iv: forge.util.bytesToHex((forge.random.getBytesSync(16)))
          };
          user.keys = {};
          user.pass.hash = forge.util.bytesToHex((forge.random.getBytesSync(32)));
        }

        db.users[req.params.name] = user;
        var hashedTitles = Object.keys(user.keys);
        if(hashedTitles.length !== 0){
          hashedTitles.forEach(function(hashedTitle){
            secretExists(hashedTitle, function(exists, secret){
              db.secrets[hashedTitle] = secret;
              db.secrets[hashedTitle].users = [req.params.name];
              if(Object.keys(db.secrets).length === hashedTitles.length){
                res.json(db);
              }
github roblav96 / robinhood.tools / src / common / security.ts View on Github external
export function randomBytes(size: number): string {
	return forge.util.bytesToHex(forge.random.getBytesSync(Math.round(size * 0.5)))
}
export function randomBits(size: number): string {
github topcoder-platform / community-app / src / server / renderer.jsx View on Github external
fs.readFile(buildInfoUrl, (err1, buildInfo) =>
      forge.random.getBytes(32, (err2, iv) => {
        const key = JSON.parse(buildInfo).rndkey;
        const cipher = forge.cipher.createCipher('AES-CBC', key.toString());
        cipher.start({ iv });
        resolve({ cipher, iv });
      }),
    ),
github juhoen / hybrid-crypto-js / src / rsa.js View on Github external
_entropy(input: any): void {
		const inputString = String(input);
		const bytes = forge.util.encodeUtf8(inputString);

		forge.random.collect(bytes);
	}
}
github juhoen / hybrid-crypto-js / lib / rsa.js View on Github external
value: function _entropy(input) {
      var inputString = String(input);
      var bytes = forge.util.encodeUtf8(inputString);
      forge.random.collect(bytes);
    }
  }]);