Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
toV3Keystore(password, options) {
options = options || {};
const salt = options.salt || randomBytes(32);
const iv = options.iv || randomBytes(16);
let derivedKey;
const kdf = options.kdf || 'scrypt';
const kdfparams = {
dklen: options.dklen || 32,
salt: salt.toString('hex')
};
if (kdf === 'pbkdf2') {
kdfparams.c = options.c || 262144;
kdfparams.prf = 'hmac-sha256';
derivedKey = pbkdf2Sync(
Buffer.from(password),
Buffer.from(kdfparams.salt, 'hex'),
kdfparams.c,
throw new Error('Unsupported cipher');
}
const ciphertext = Buffer.concat([
cipher.update(Buffer.from(this.privateKey.replace('0x', ''), 'hex')),
cipher.final()
]);
const mac = keccak256(Buffer.concat([derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex')])).replace(
'0x',
''
);
return {
version: 3,
id: uuid.v4({random: options.uuid || randomBytes(16)}),
address: this.address.toLowerCase().replace('0x', ''),
crypto: {
ciphertext: ciphertext.toString('hex'),
cipherparams: {
iv: iv.toString('hex')
},
cipher: options.cipher || 'aes-128-ctr',
kdf,
kdfparams,
mac: mac.toString('hex')
}
};
}
function uniqueId (f) {
return randomBytes(8).toString('hex')
}
export function encrypt(
privateKey: string,
password: string,
opts: EncryptOptions = {}
): PrivateKey {
const account = privateKeyToAccount(privateKey);
const salt = opts.salt || randomBytes(32);
const iv = opts.iv || randomBytes(16);
let derivedKey;
const kdf = opts.kdf || "scrypt";
const kdfparams: Kdfparams = {
dklen: opts.dklen || 32,
salt: salt.toString("hex")
};
if (kdf === "pbkdf2") {
kdfparams.c = opts.c || 262144;
kdfparams.prf = "hmac-sha256";
derivedKey = crypto.pbkdf2Sync(
Buffer.from(password),
salt,
kdfparams.c,
const cryptoRandomString = len => {
if (!Number.isFinite(len)) {
throw new TypeError('Expected a finite number');
}
return randomBytes(Math.ceil(len / 2))
.toString('hex')
.slice(0, len);
};
toV3Keystore(password, options) {
options = options || {};
const salt = options.salt || randomBytes(32);
const iv = options.iv || randomBytes(16);
let derivedKey;
const kdf = options.kdf || 'scrypt';
const kdfparams = {
dklen: options.dklen || 32,
salt: salt.toString('hex')
};
if (kdf === 'pbkdf2') {
kdfparams.c = options.c || 262144;
kdfparams.prf = 'hmac-sha256';
derivedKey = pbkdf2Sync(
Buffer.from(password),
Buffer.from(kdfparams.salt, 'hex'),
kdfparams.c,
kdfparams.dklen,
export function generateCodeVerifier() {
return b64toB64UrlEncoded(
randomBytes(32).toString("base64")
)
}
generateRandomSmallA() {
const hexRandom = randomBytes(128).toString('hex');
const randomBigInt = new BigInteger(hexRandom, 16);
const smallABigInt = randomBigInt.mod(this.N);
return smallABigInt;
}
static randomBytes(size = 16) {
return randomBytes(size);
}
}
getRandomValues(byteArray) {
randomBytes(byteArray);
},
};