How to use the random function from node-forge
Find comprehensive JavaScript node-forge.random code examples handpicked from public code repositorys.
GitHub: offen/offen
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)
38
706
6
+ 5 other calls in file
GitHub: juhoen/hybrid-crypto-js
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;
40
126
6
GitHub: juhoen/hybrid-crypto-js
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) {
40
126
6
+ 7 other calls in file
GitHub: invizi/invizi
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()
19
62
5
GitHub: mitro-co/keyczarjs
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); }
17
38
9
+ 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);
13
38
6
+ 7 other calls in file
GitHub: Andy0570/evernote
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
1
3
2
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;
1
3
1
+ 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);
1
1
3
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());
1
1
0
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);
0
1
0
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;
1
0
0
+ 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
1
0
0
+ 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));
0
0
1
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);
0
0
0
GitHub: y-kkky/dotfiles
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);
0
0
0
+ 3 other calls in file
GitHub: y-kkky/dotfiles
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));
0
0
0
GitHub: ongeri/iframe
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; };
0
0
0
node-forge.pki is the most popular function in node-forge (10287 examples)