crypto#randomBytes JavaScript Examples
The following examples show how to use
crypto#randomBytes.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: genarateRandomkey.js From saasgear with MIT License | 6 votes |
generateRandomKey = () =>
new Promise((resolve, reject) => {
randomBytes(32, (error, buf) => {
if (error) {
return reject(error);
}
const token = buf.toString('hex');
return resolve(token);
});
})
Example #2
Source File: nexoCrypto.js From Lynx with MIT License | 6 votes |
NexoCrypto = /** @class */ (function () {
function NexoCrypto() {
}
NexoCrypto.encrypt = function (messageHeader, saleToPoiMessageJson, securityKey) {
var derivedKey = NexoDerivedKeyGenerator.deriveKeyMaterial(securityKey.passphrase);
var saleToPoiMessageByteArray = Buffer.from(saleToPoiMessageJson, "ascii");
var ivNonce = NexoCrypto.generateRandomIvNonce();
var encryptedSaleToPoiMessage = NexoCrypto.crypt(saleToPoiMessageByteArray, derivedKey, ivNonce, Modes.ENCRYPT);
var encryptedSaleToPoiMessageHmac = NexoCrypto.hmac(saleToPoiMessageByteArray, derivedKey);
var securityTrailer = {
adyenCryptoVersion: securityKey.adyenCryptoVersion,
hmac: encryptedSaleToPoiMessageHmac.toString("base64"),
keyIdentifier: securityKey.keyIdentifier,
keyVersion: securityKey.keyVersion,
nonce: ivNonce.toString("base64"),
};
return {
messageHeader: messageHeader,
nexoBlob: encryptedSaleToPoiMessage.toString("base64"),
securityTrailer: securityTrailer,
};
};
NexoCrypto.prototype.decrypt = function (saleToPoiSecureMessage, securityKey) {
NexoCrypto.validateSecurityKey(securityKey);
var encryptedSaleToPoiMessageByteArray = Buffer.from(saleToPoiSecureMessage.nexoBlob, "base64");
var derivedKey = NexoDerivedKeyGenerator.deriveKeyMaterial(securityKey.passphrase);
var ivNonce = Buffer.from(saleToPoiSecureMessage.securityTrailer.nonce, "base64");
var decryptedSaleToPoiMessageByteArray = NexoCrypto.crypt(encryptedSaleToPoiMessageByteArray, derivedKey, ivNonce, Modes.DECRYPT);
var receivedHmac = Buffer.from(saleToPoiSecureMessage.securityTrailer.hmac, "base64");
this.validateHmac(receivedHmac, decryptedSaleToPoiMessageByteArray, derivedKey);
return decryptedSaleToPoiMessageByteArray.toString("ascii");
};
NexoCrypto.validateSecurityKey = function (securityKey) {
var isValid = securityKey
&& securityKey.passphrase
&& securityKey.keyIdentifier
&& securityKey.keyVersion
&& securityKey.adyenCryptoVersion;
if (!isValid) {
throw new InvalidSecurityKeyException("Invalid Security Key");
}
};
NexoCrypto.crypt = function (bytes, dk, ivNonce, mode) {
var actualIV = Buffer.alloc(NEXO_IV_LENGTH);
for (var i = 0; i < NEXO_IV_LENGTH; i++) {
actualIV[i] = dk.iv[i] ^ ivNonce[i];
}
var cipher = mode === Modes.ENCRYPT
? createCipheriv("aes-256-cbc", dk.cipherKey, actualIV)
: createDecipheriv("aes-256-cbc", dk.cipherKey, actualIV);
var encrypted = cipher.update(bytes);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted;
};
NexoCrypto.hmac = function (bytes, derivedKey) {
var mac = createHmac("sha256", derivedKey.hmacKey);
return mac.update(bytes).digest();
};
NexoCrypto.generateRandomIvNonce = function () {
return randomBytes(NEXO_IV_LENGTH);
};
NexoCrypto.prototype.validateHmac = function (receivedHmac, decryptedMessage, derivedKey) {
var hmac = NexoCrypto.hmac(decryptedMessage, derivedKey);
var isValid = hmac.every(function (item, index) { return item === receivedHmac[index]; });
if (!isValid) {
throw new NexoCryptoException("Hmac validation failed");
}
};
return NexoCrypto;
}())
Example #3
Source File: broadcast.js From HinataMd with GNU General Public License v3.0 | 5 votes |
randomID = length => randomBytes(Math.ceil(length * .5)).toString('hex').slice(0, length)
Example #4
Source File: broadcastchats.js From HinataMd with GNU General Public License v3.0 | 5 votes |
randomID = length => randomBytes(Math.ceil(length * .5)).toString('hex').slice(0, length)
Example #5
Source File: broadcastgroups.js From HinataMd with GNU General Public License v3.0 | 5 votes |
randomID = length => randomBytes(Math.ceil(length * .5)).toString('hex').slice(0, length)
Example #6
Source File: testAttributeHandler.js From LightningWebChartJS with MIT License | 5 votes |
Object.defineProperty(global.self, 'crypto', {
value: {
getRandomValues: (arr) => randomBytes(arr.length)
}
});
Example #7
Source File: timing_attack_node.js From njsscan with GNU Lesser General Public License v3.0 | 5 votes |
create(password) {
const salt = randomBytes(128).toString('base64'); // <- salt
// salt was not base64 before being used by pbkdf2
const hash = pbkdf2Sync(password, salt, this.iters, this.keylen, this.digest).toString('base64');
return [salt, hash, this.iters].join('::');
}
Example #8
Source File: crypto.js From lyswhut-lx-music-desktop with Apache License 2.0 | 5 votes |
weapi = object => {
const text = JSON.stringify(object)
const secretKey = randomBytes(16).map(n => (base62.charAt(n % 62).charCodeAt()))
return {
params: aesEncrypt(Buffer.from(aesEncrypt(Buffer.from(text), 'cbc', presetKey, iv).toString('base64')), 'cbc', secretKey, iv).toString('base64'),
encSecKey: rsaEncrypt(secretKey.reverse(), publicKey).toString('hex'),
}
}
Example #9
Source File: crypto.js From lx-music-mobile with Apache License 2.0 | 5 votes |
weapi = object => {
const text = JSON.stringify(object)
const secretKey = randomBytes(16).map(n => (base62.charAt(n % 62).charCodeAt()))
return {
params: aesEncrypt(Buffer.from(aesEncrypt(Buffer.from(text), 'aes-128-cbc', presetKey, iv).toString('base64')), 'aes-128-cbc', secretKey, iv).toString('base64'),
encSecKey: rsaEncrypt(secretKey.reverse(), publicKey).toString('hex'),
}
}
Example #10
Source File: lnurl.js From stacker.news with MIT License | 5 votes |
function k1 () {
return randomBytes(32).toString('hex')
}
Example #11
Source File: index.js From rest-api-examples with MIT License | 5 votes |
state = randomBytes(8).toString('hex')