Skip to content

Commit

Permalink
Refactor decipherBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Dec 20, 2018
1 parent 444d833 commit 9ee22af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function assert (val, msg) {
}
}

function decipherBuffer (decipher, data) {
return Buffer.concat([ decipher.update(data), decipher.final() ])
function runCipherBuffer (cipher, data) {
return Buffer.concat([ cipher.update(data), cipher.final() ])
}

var Wallet = function (priv, pub) {
Expand Down Expand Up @@ -140,7 +140,7 @@ Wallet.prototype.toV3 = function (password, opts) {
throw new Error('Unsupported cipher')
}

var ciphertext = Buffer.concat([ cipher.update(this.privKey), cipher.final() ])
var ciphertext = runCipherBuffer(cipher, this.privKey)

var mac = ethUtil.keccak256(Buffer.concat([ derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex') ]))

Expand Down Expand Up @@ -237,7 +237,7 @@ Wallet.fromV1 = function (input, password) {
}

var decipher = crypto.createDecipheriv('aes-128-cbc', ethUtil.keccak256(derivedKey.slice(0, 16)).slice(0, 16), Buffer.from(json.Crypto.IV, 'hex'))
var seed = decipherBuffer(decipher, ciphertext)
var seed = runCipherBuffer(decipher, ciphertext)

return new Wallet(seed)
}
Expand Down Expand Up @@ -277,7 +277,7 @@ Wallet.fromV3 = function (input, password, nonStrict) {
}

var decipher = crypto.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), Buffer.from(json.crypto.cipherparams.iv, 'hex'))
var seed = decipherBuffer(decipher, ciphertext)
var seed = runCipherBuffer(decipher, ciphertext)

return new Wallet(seed)
}
Expand All @@ -299,7 +299,7 @@ Wallet.fromEthSale = function (input, password) {
// NOTE: crypto (derived from openssl) when used with aes-*-cbc will handle PKCS#7 padding internally
// see also http://stackoverflow.com/a/31614770/4964819
var decipher = crypto.createDecipheriv('aes-128-cbc', derivedKey, encseed.slice(0, 16))
var seed = decipherBuffer(decipher, encseed.slice(16))
var seed = runCipherBuffer(decipher, encseed.slice(16))

var wallet = new Wallet(ethUtil.keccak256(seed))
if (wallet.getAddress().toString('hex') !== json.ethaddr) {
Expand Down
6 changes: 3 additions & 3 deletions src/thirdparty.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function assert (val, msg) {
}
}

function decipherBuffer (decipher, data) {
return Buffer.concat([ decipher.update(data), decipher.final() ])
function runCipherBuffer (cipher, data) {
return Buffer.concat([ cipher.update(data), cipher.final() ])
}

var Thirdparty = {}
Expand Down Expand Up @@ -118,7 +118,7 @@ Thirdparty.fromEtherWallet = function (input, password) {
var evp = evp_kdf(Buffer.from(password), cipher.salt, { keysize: 32, ivsize: 16 })

var decipher = crypto.createDecipheriv('aes-256-cbc', evp.key, evp.iv)
privKey = decipherBuffer(decipher, Buffer.from(cipher.ciphertext))
privKey = runCipherBuffer(decipher, Buffer.from(cipher.ciphertext))

// NOTE: yes, they've run it through UTF8
privKey = Buffer.from(utf8.decode(privKey.toString()), 'hex')
Expand Down

0 comments on commit 9ee22af

Please sign in to comment.