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);
}
//----------- yz-fix add
if(match2&&match2[1]===match2[3]){// add
match2.pop()
}else{
match2=null
}
//------------
decrypted = new Buffer(match2[2].replace(/[\r\n]/g, ''), 'base64')
} else {
var suite = 'aes' + match[1]
var iv = new Buffer(match[2], 'hex')
var cipherText = new Buffer(match[3].replace(/[\r\n]/g, ''), 'base64')
var cipherKey = evp(password, iv.slice(0, 8), parseInt(match[1], 10)).key
var out = []
var cipher = ciphers.createDecipheriv(suite, cipherKey, iv)
out.push(cipher.update(cipherText))
out.push(cipher.final())
decrypted = Buffer.concat(out)
}
var tag = key.match(startRegex)[1]
return {
tag: tag,
data: decrypted
}
}
var passFactor
if (hasLotSeq) {
var hashTarget = Buffer.concat([preFactor, ownerEntropy])
passFactor = hash256(hashTarget)
} else {
passFactor = preFactor
}
var passInt = BigInteger.fromBuffer(passFactor)
var passPoint = curve.G.multiply(passInt).getEncoded(true)
var seedBPass = scrypt(passPoint, Buffer.concat([addressHash, ownerEntropy]), 1024, 1, 1, 64)
var derivedHalf1 = seedBPass.slice(0, 32)
var derivedHalf2 = seedBPass.slice(32, 64)
var decipher = aes.createDecipheriv('aes-256-ecb', derivedHalf2, Buffer.alloc(0))
decipher.setAutoPadding(false)
decipher.end(encryptedPart2)
var decryptedPart2 = decipher.read()
var tmp = xor(decryptedPart2, derivedHalf1.slice(16, 32))
var seedBPart2 = tmp.slice(8, 16)
var decipher2 = aes.createDecipheriv('aes-256-ecb', derivedHalf2, Buffer.alloc(0))
decipher2.setAutoPadding(false)
decipher2.write(encryptedPart1) // first 8 bytes
decipher2.end(tmp.slice(0, 8)) // last 8 bytes
var seedBPart1 = xor(decipher2.read(), derivedHalf1.slice(0, 16))
var seedB = Buffer.concat([seedBPart1, seedBPart2], 24)
var factorB = BigInteger.fromBuffer(hash256(seedB))
var flagByte = buffer.readUInt8(2)
var compressed = flagByte === 0xe0
if (!compressed && flagByte !== 0xc0) throw new Error('Invalid BIP38 compression flag')
var N = scryptParams.N
var r = scryptParams.r
var p = scryptParams.p
var salt = buffer.slice(3, 7)
var scryptBuf = scrypt(passphrase, salt, N, r, p, 64, progressCallback)
var derivedHalf1 = scryptBuf.slice(0, 32)
var derivedHalf2 = scryptBuf.slice(32, 64)
var privKeyBuf = buffer.slice(7, 7 + 32)
var decipher = aes.createDecipheriv('aes-256-ecb', derivedHalf2, NULL)
decipher.setAutoPadding(false)
decipher.end(privKeyBuf)
var plainText = decipher.read()
var privateKey = xor(derivedHalf1, plainText)
// verify salt matches address
var d = BigInteger.fromBuffer(privateKey)
var address = getAddress(d, compressed)
var checksum = hash256(address).slice(0, 4)
assert.deepStrictEqual(salt, checksum)
return {
privateKey: privateKey,
compressed: compressed
}
function cryptoJsDecrypt(message, key, iv) {
assert(message, "Missing cipher text")
message = toBinaryBuffer(message)
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv)
// decipher.setAutoPadding(true)
message = Buffer.concat([decipher.update(message), decipher.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
}
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;
};
function decrypt (data, password) {
var salt = data.algorithm.decrypt.kde.kdeparams.salt
var iters = parseInt(data.algorithm.decrypt.kde.kdeparams.iters.toString(), 10)
var algo = aesid[data.algorithm.decrypt.cipher.algo.join('.')]
var iv = data.algorithm.decrypt.cipher.iv
var cipherText = data.subjectPrivateKey
var keylen = parseInt(algo.split('-')[1], 10) / 8
var key = compat.pbkdf2Sync(password, salt, iters, keylen)
var cipher = ciphers.createDecipheriv(algo, key, iv)
var out = []
out.push(cipher.update(cipherText))
out.push(cipher.final())
return Buffer.concat(out)
}