Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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',
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;
}
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),
randomSerialNumber: function() {
return forge.util.bytesToHex(forge.random.getBytesSync(4));
},
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{
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);
}
export function randomBytes(size: number): string {
return forge.util.bytesToHex(forge.random.getBytesSync(Math.round(size * 0.5)))
}
export function randomBits(size: number): string {
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 });
}),
),
_entropy(input: any): void {
const inputString = String(input);
const bytes = forge.util.encodeUtf8(inputString);
forge.random.collect(bytes);
}
}
value: function _entropy(input) {
var inputString = String(input);
var bytes = forge.util.encodeUtf8(inputString);
forge.random.collect(bytes);
}
}]);