Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_handleNotify(event) {
const value = Buffer.from(event.target.value.buffer);
if (event.target.uuid === this.char.auth.uuid) {
const cmd = value.slice(0,3).toString('hex');
if (cmd === '100101') { // Set New Key OK
this.authReqRandomKey()
} else if (cmd === '100201') { // Req Random Number OK
let rdn = value.slice(3)
let cipher = crypto.createCipheriv('aes-128-ecb', this.key, '').setAutoPadding(false)
let encrypted = Buffer.concat([cipher.update(rdn), cipher.final()])
this.authSendEncKey(encrypted)
} else if (cmd === '100301') {
debug('Authenticated')
this.emit('authenticated')
} else if (cmd === '100104') { // Set New Key FAIL
this.emit('error', 'Key Sending failed')
} else if (cmd === '100204') { // Req Random Number FAIL
this.emit('error', 'Key Sending failed')
} else if (cmd === '100304') {
debug('Encryption Key Auth Fail, sending new key...')
this.authSendNewKey(this.key)
} else {
debug('Unhandled auth rsp:', value);
}
function cryptoJsEncrypt(message, key, iv) {
assert(message, "Missing plain text")
message = toBinaryBuffer(message)
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv)
// cipher.setAutoPadding(true)
message = Buffer.concat([cipher.update(message), cipher.final()])
return message
}
function cryptoJsEncrypt(message, key, iv) {
assert(message, "Missing plain text")
message = toBinaryBuffer(message)
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv)
// cipher.setAutoPadding(true)
message = Buffer.concat([cipher.update(message), cipher.final()])
return message
}
var d = BigInteger.fromBuffer(buffer)
var address = getAddress(d, compressed)
var secret = Buffer.from(passphrase.normalize('NFC'), 'utf8')
var salt = hash256(address).slice(0, 4)
var N = scryptParams.N
var r = scryptParams.r
var p = scryptParams.p
var scryptBuf = scrypt(secret, salt, N, r, p, 64, progressCallback)
var derivedHalf1 = scryptBuf.slice(0, 32)
var derivedHalf2 = scryptBuf.slice(32, 64)
var xorBuf = xor(derivedHalf1, buffer)
var cipher = aes.createCipheriv('aes-256-ecb', derivedHalf2, NULL)
cipher.setAutoPadding(false)
cipher.end(xorBuf)
var cipherText = cipher.read()
// 0x01 | 0x42 | flagByte | salt (4) | cipherText (32)
var result = Buffer.allocUnsafe(7 + 32)
result.writeUInt8(0x01, 0)
result.writeUInt8(0x42, 1)
result.writeUInt8(compressed ? 0xe0 : 0xc0, 2)
salt.copy(result, 3)
cipherText.copy(result, 7)
return result
}
const encryptRaw = (buffer: Buffer, compressed: boolean, passphrase: string): Buffer => {
if (buffer.length !== 32) {
throw new PrivateKeyLengthError(32, buffer.length);
}
const address = getAddressPrivate(buffer, compressed);
const secret = Buffer.from(passphrase, "utf8");
const salt = HashAlgorithms.hash256(address).slice(0, 4);
const scryptBuf = crypto.scryptSync(secret, salt, 64, SCRYPT_PARAMS);
const derivedHalf1 = scryptBuf.slice(0, 32);
const derivedHalf2 = scryptBuf.slice(32, 64);
const xorBuf = xor(derivedHalf1, buffer);
const cipher = aes.createCipheriv("aes-256-ecb", derivedHalf2, NULL);
cipher.setAutoPadding(false);
cipher.end(xorBuf);
const cipherText = cipher.read();
// 0x01 | 0x42 | flagByte | salt (4) | cipherText (32)
const result = Buffer.allocUnsafe(7 + 32);
result.writeUInt8(0x01, 0);
result.writeUInt8(0x42, 1);
result.writeUInt8(compressed ? 0xe0 : 0xc0, 2);
salt.copy(result, 3);
cipherText.copy(result, 7);
return result;
};