Skip to content

Commit

Permalink
refactor: turn crackBlock into a private method
Browse files Browse the repository at this point in the history
  • Loading branch information
imyelo committed May 30, 2019
1 parent 1d7a50b commit d268dfa
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,7 @@ class Cracker {
this.broadcast = new EventEmitter()
}

async crack (iv, cipher, challenge, concurrency = Infinity) {
let { state } = this

state.iv = iv
state.cipher = cipher

const original = Buffer.concat([iv, cipher])
const size = iv.length

state.intermediary = Buffer.alloc(cipher.length)

this.broadcast.emit(EVENTS.CRACK_START)

await pAll(range(cipher.length / size).map((block) => async () => {
const im = await this.crackBlock(iv, cipher, challenge, block)
state.intermediary = replace(state.intermediary, block * size, im)
}), { concurrency })

state.plain = xor(original.slice(0, state.intermediary.length), state.intermediary)

this.broadcast.emit(EVENTS.CRACK_END)
return toJS(state)
}

async crackBlock (iv, cipher, challenge, block) {
async _crackBlock (iv, cipher, challenge, block) {
const original = Buffer.concat([iv, cipher])
const size = iv.length

Expand Down Expand Up @@ -102,6 +78,30 @@ class Cracker {
return intermediary
}

async crack (iv, cipher, challenge, concurrency = Infinity) {
let { state } = this

state.iv = iv
state.cipher = cipher

const original = Buffer.concat([iv, cipher])
const size = iv.length

state.intermediary = Buffer.alloc(cipher.length)

this.broadcast.emit(EVENTS.CRACK_START)

await pAll(range(cipher.length / size).map((block) => async () => {
const im = await this._crackBlock(iv, cipher, challenge, block)
state.intermediary = replace(state.intermediary, block * size, im)
}), { concurrency })

state.plain = xor(original.slice(0, state.intermediary.length), state.intermediary)

this.broadcast.emit(EVENTS.CRACK_END)
return toJS(state)
}

async modify (target, size, challenge) {
const PLACEHOLDER = 255

Expand All @@ -116,7 +116,7 @@ class Cracker {
const start = block * size
const end = (block + 1) * size

intermediary = replace(intermediary, block * size, await this.crackBlock(iv, cipher, challenge, block))
intermediary = replace(intermediary, block * size, await this._crackBlock(iv, cipher, challenge, block))

const vector = xor(
intermediary.slice(start, end),
Expand Down

0 comments on commit d268dfa

Please sign in to comment.