How to use the random function from node-forge

Find comprehensive JavaScript node-forge.random code examples handpicked from public code repositorys.

146
147
148
149
150
151
152
153
154
155
    })
}

function randomBytes (num) {
  return new Promise(function (resolve, reject) {
    forge.random.getBytes(16, function (err, bytes) {
      if (err) {
        return reject(err)
      }
      resolve(bytes)
fork icon38
star icon706
watch icon6

+ 5 other calls in file

72
73
74
75
76
77
78
79
80
81
     */
    _entropy(input: any): void {
        const inputString = String(input);
        const bytes = forge.util.encodeUtf8(inputString);

        forge.random.collect(bytes);
    }
}

module.exports = RSA;
fork icon40
star icon126
watch icon6

205
206
207
208
209
210
211
212
213
214

publicKeys = publicKeys.map(function (key) {
  return typeof key === 'string' ? pki.publicKeyFromPem(key) : key;
}); // Generate random keys

var iv = forge.random.getBytesSync(this.options.aesIvSize);
var key = forge.random.getBytesSync(this.options.aesKeySize / 8); // Encrypt random key with all of the public keys

var encryptedKeys = {};
publicKeys.forEach(function (publicKey) {
fork icon40
star icon126
watch icon6

+ 7 other calls in file

43
44
45
46
47
48
49
50
51
52
suite.add('node crypto encrypt decrypt', function () {
  nodeCrypto('aes-256-ecb')
})
  .add('forge encrypt decrypt', function () {
    var cipher = forge.cipher.createCipher('AES-CBC', key)
    let iv = forge.random.getBytesSync(AES_IV_LENGTH)
    cipher.start({iv: iv})
    cipher.update(forge.util.createBuffer(message))
    cipher.finish()
    let ciphertext = iv + cipher.output.toHex()
fork icon19
star icon62
watch icon5

42
43
44
45
46
47
48
49
50
*/
function _generateAes(size) {
    if (!size) size = AES_DEFAULT_BITS;

    // generate random bytes for both AES and HMAC
    var keyBytes = forge.random.getBytes(size/8);
    var hmacBytes = forge.random.getBytes(HMAC_DEFAULT_BITS/8);
    return keyczar_util._aesFromBytes(keyBytes, hmacBytes);
}
fork icon17
star icon38
watch icon9

+ 7 other calls in file

69
70
71
72
73
74
75
76
77
78
  var aesDec = decCipher.output;
  return forge.util.decodeUtf8(aesDec);
}

function generateRandomSalt8Byte() {
  return forge.random.getBytesSync(8);
}

function base64Encoding(input) {
  return forge.util.encode64(input);
fork icon13
star icon38
watch icon6

+ 7 other calls in file

342
343
344
345
346
347
348
349
350
351


// 生成基于口令的 16 进制密钥
// note an optional message digest can be passed as the final parameter
// 注意,可以将可选的消息摘要算法作为最终参数传递
var salt = forge.random.getBytesSync(128);
var derivedKey = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);

// 异步生成密钥
// note an optional message digest can be passed before the callback
fork icon1
star icon3
watch icon2

73
74
75
76
77
78
79
80
81
// Use RSA-KEM to encrypt the msg with a randomly
// generated one time password and AES-256
const kdf1 = new forge.kem.kdf1(forge.md.sha256.create());
const kem = forge.kem.rsa.create(kdf1);
const otp = kem.encrypt(publicKey, 32);
const iv = forge.random.getBytesSync(12);
const cipher = forge.cipher.createCipher('AES-GCM', otp.key);
let aesCipherText;
let encryptedOTP;
fork icon1
star icon3
watch icon1

+ 3 other calls in file

11
12
13
14
15
16
17
18
19
20
// get derived bytes
// Notes:
// 1. If using an alternative hash (eg: "-md sha1") pass
//   "forge.md.sha1.create()" as the final parameter.
// 2. If using "-nosalt", set salt to null.
var salt = forge.random.getBytesSync(8);
// var md = forge.md.sha1.create(); // "-md sha1"
var derivedBytes = forge.pbe.opensslDeriveBytes(
  password, salt, keySize + ivSize/*, md*/);
var buffer = forge.util.createBuffer(derivedBytes);
fork icon1
star icon1
watch icon3

18
19
20
21
22
23
24
25
26
27
var n = (p=new BigInteger(p)).multiply(q=new BigInteger(q)),
	phiN = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)), e;
// generate 1 < e < phiN, which is propable prime
while((e=new BigInteger(phiN.bitLength(), {
	nextBytes: function(array) {
		var random = forge.random.getBytesSync(array.length);
		for(var index=0; index<array.length; index++)
			array[index] = random.charCodeAt(index);
	}
})).compareTo(phiN)>=0||!e.isProbablePrime());
fork icon1
star icon1
watch icon0

19
20
21
22
23
24
25
26
27
28
29
30
31
var good3 = forge.rc2.createEncryptionCipher(password, 128); // OK


var key1 = forge.random.getBytesSync(16);
var good4 = forge.cipher.createCipher('AES-CBC', key1); // OK


var key2 = forge.random.getBytesSync(8);
var bad8 = forge.cipher.createCipher('AES-CBC', key2); // NOT OK


var myBuffer = forge.util.createBuffer(manyBytes);
var key3 = myBuffer.getBytes(8);
fork icon0
star icon1
watch icon0

205
206
207
208
209
210
211
212
213
214
215
216
        })
    });
}


function generateTeamKey() {
    const salt = forge.random.getBytesSync(16);
    const randomKey = forge.random.getBytesSync(32);
    const teamKey = forge.pkcs5.pbkdf2(randomKey, salt, 10000, 32);


    return teamKey;
fork icon1
star icon0
watch icon0

+ 9 other calls in file

39
40
41
42
43
44
45
46
47
48
  
export function encryptBinaryStringGCM(binaryString, key) {
    const thisBuffer = forge.util.createBuffer();
    thisBuffer.putBytes(binaryString);
  
    const uniqueIV = forge.random.getBytesSync(12);
    const cipher = forge.cipher.createCipher('AES-GCM', key);
    cipher.start({
      iv: uniqueIV, // should be a 12-byte binary-encoded string or byte buffer
      tagLength: 128 // optional, defaults to 128 bits
fork icon1
star icon0
watch icon0

+ 7 other calls in file

346
347
348
349
350
351
352
353
354
355
356
  return arr;
};


//Encrypt data AES 256
export const _encryp = (secureData) => {
  var key = forge.random.getBytesSync(64);
  var iv = forge.random.getBytesSync(64);
  var cipher = forge.cipher.createCipher("AES-CBC", SECRET_KEY);
  cipher.start({ iv: iv });
  cipher.update(forge.util.createBuffer(secureData));
fork icon0
star icon0
watch icon1

25
26
27
28
29
30
31
32
33
34
let keyId;
exports.getSessionKey = function () {
	//TODO finish this
	/* let now = moment();
	if(keyCreated === undefined || keyCreated.isBefore(now.subtract(10, 'minutes'))) {
		key = forge.random.getBytesSync(32);
		keyCreated = now;
		keyId = "1234"
	} */
	let key = forge.random.getBytesSync(32);
fork icon0
star icon0
watch icon0

253
254
255
256
257
258
259
260
261
262
263
264
}


function _encryptKey(keyString, password) {
    // derive the key
    var iterationCount = _DEFAULT_ITERATIONS;
    var salt = forge.random.getBytes(_SALT_BYTES);
    var derivedKey = _deriveKey(password, salt, iterationCount);


    var iv = forge.random.getBytes(_PBE_AES_KEY_BYTES);
    var cipher = forge.aes.startEncrypting(derivedKey, iv, null);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

396
397
398
399
400
401
402
403
404
405
var key = {
    keyhash: keyhash
};
key.encrypt = function(input) {
    // generate a random IV
    var iv = forge.random.getBytes(keyBytes.length);

    // TODO: cache the cipher object?
    var cipher = forge.aes.startEncrypting(keyBytes, iv, null);
    cipher.update(new forge.util.ByteBuffer(input));
fork icon0
star icon0
watch icon0

85
86
87
88
89
90
91
92
93
94
95
96
97
98


};




SecureManager.generateKey = function () {
    var bytes = forge.random.getBytesSync(16);
    return bytes;
};



fork icon0
star icon0
watch icon0