Skip to content

Commit

Permalink
refactor: mv utils func
Browse files Browse the repository at this point in the history
  • Loading branch information
imyelo committed May 9, 2019
1 parent f51ed06 commit bf19b44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
24 changes: 2 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
const log = require('log-update')
const pad = require('left-pad')
const { replace, xor } = require('./utils')

const ALL_HEX = Array.from(Array(256)).map((v, i) => pad(i.toString(16), 2, 0))
// <- ['00', '01', ... 'fe', 'ff']

function replace (buf, position, replacement) {
let end = position + replacement.length
if (position < 0) {
return replace(buf, buf.length + position, replacement)
}
if (end > buf.length) {
throw new Error('The replacement will cause the result to overflow.')
}
return Buffer.from(buf).fill(replacement, position, end)
}

function xor (x, y) {
if (x.length !== y.length) {
throw new Error('The length of two buffers is different')
}
let z = Buffer.alloc(x.length)
for (let i = 0; i < z.length; i ++) {
z[i] = x[i] ^ y[i]
}
return z
}

async function crack (iv, cipher, challenge) {
let original = Buffer.concat([iv, cipher])
let size = iv.length
Expand Down
24 changes: 24 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function replace (buf, position, replacement) {
let end = position + replacement.length
if (position < 0) {
return replace(buf, buf.length + position, replacement)
}
if (end > buf.length) {
throw new Error('The replacement will cause the result to overflow.')
}
return Buffer.from(buf).fill(replacement, position, end)
}

function xor (x, y) {
if (x.length !== y.length) {
throw new Error('The length of two buffers is different')
}
let z = Buffer.alloc(x.length)
for (let i = 0; i < z.length; i ++) {
z[i] = x[i] ^ y[i]
}
return z
}

exports.replace = replace
exports.xor = xor

0 comments on commit bf19b44

Please sign in to comment.