Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
window.go = function(){
var password='holamundo'
var enc=toV3('If the text is very very long?', password)
console.log( enc );
var ciphertext = new Buffer(enc.ciphertext, 'hex')
var derivedKey = scrypt(new Buffer(password), new Buffer(enc.kdfparams.salt, 'hex'), enc.kdfparams.n, enc.kdfparams.r, enc.kdfparams.p, enc.kdfparams.dklen)
var decipher = createDecipheriv(enc.cipher, derivedKey.slice(0, 16), new Buffer(enc.cipherparams.iv, 'hex'))
var seed = decipherBuffer(decipher, ciphertext, 'hex')
console.log( 'DECRYPTED yeah!', seed.toString() );
}
kdfparams.c,
kdfparams.dklen,
'sha256'
);
} else {
throw new Error('Unsupported key derivation scheme');
}
const ciphertext = Buffer.from(json.crypto.ciphertext, 'hex');
const mac = keccak256(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).replace('0x', '');
if (mac !== json.crypto.mac) {
throw new Error('Key derivation failed - possibly wrong password');
}
const decipher = createDecipheriv(
json.crypto.cipher,
derivedKey.slice(0, 16),
Buffer.from(json.crypto.cipherparams.iv, 'hex')
);
const seed = `0x${Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('hex')}`;
return Account.fromPrivateKey(seed, accounts);
}
}
kdfparams.c,
kdfparams.dklen,
'sha256'
);
} else {
throw new Error('Unsupported key derivation scheme');
}
const ciphertext = Buffer.from(json.crypto.ciphertext, 'hex');
const mac = keccak256(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).replace('0x', '');
if (mac !== json.crypto.mac) {
throw new Error('Key derivation failed - possibly wrong password');
}
const decipher = createDecipheriv(
json.crypto.cipher,
derivedKey.slice(0, 16),
Buffer.from(json.crypto.cipherparams.iv, 'hex')
);
const seed = `0x${Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('hex')}`;
return Account.fromPrivateKey(seed, accounts);
}
}
export function decryptAES128CTR(encryption, password, hex=false) {
const ciphertype = 'aes-128-ctr'
const ciphertext = new Buffer(encryption.ciphertext, 'hex')
const derivedKey = encryption.kdf==='scrypt' ?
scrypt(new Buffer(password), new Buffer(encryption.kdfparams.salt, 'hex'), encryption.kdfparams.n, encryption.kdfparams.r, encryption.kdfparams.p, encryption.kdfparams.dklen)
:
pbkdf2.pbkdf2Sync(new Buffer(password), new Buffer(encryption.kdfparams.salt, 'hex'), encryption.kdfparams.c, encryption.kdfparams.dklen, 'sha256')
const decipher = createDecipheriv(ciphertype, derivedKey.slice(0, 16), new Buffer(encryption.cipherparams.iv, 'hex'))
let seed = Buffer.concat([decipher.update(ciphertext), decipher.final()])
while (seed.length < 32)
seed = Buffer.concat([new Buffer([0x00]), seed]);
return seed.toString(hex?'hex':undefined) //ethereum seed.toString('hex')
}