convert: remove hex functions
This commit is contained in:
parent
614a213d44
commit
f70ccc9215
2 changed files with 5 additions and 59 deletions
|
@ -2,36 +2,6 @@ var assert = require('assert')
|
||||||
var Crypto = require('crypto-js')
|
var Crypto = require('crypto-js')
|
||||||
var WordArray = Crypto.lib.WordArray
|
var WordArray = Crypto.lib.WordArray
|
||||||
|
|
||||||
function lpad(str, padString, length) {
|
|
||||||
while (str.length < length) str = padString + str
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
function bytesToHex(bytes) {
|
|
||||||
// FIXME: transitionary fix
|
|
||||||
if (Buffer.isBuffer(bytes)) {
|
|
||||||
return bytes.toString('hex')
|
|
||||||
}
|
|
||||||
|
|
||||||
return bytes.map(function(x) {
|
|
||||||
return lpad(x.toString(16), '0', 2)
|
|
||||||
}).join('')
|
|
||||||
}
|
|
||||||
|
|
||||||
function hexToBytes(hex) {
|
|
||||||
return hex.match(/../g).map(function(x) {
|
|
||||||
return parseInt(x,16)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a byte array to the number that it represents
|
|
||||||
*/
|
|
||||||
function bytesToNum(bytes) {
|
|
||||||
if (bytes.length === 0) return 0
|
|
||||||
return bytes[0] + 256 * bytesToNum(bytes.slice(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
function bytesToWords(bytes) {
|
function bytesToWords(bytes) {
|
||||||
assert(Array.isArray(bytes) || Buffer.isBuffer(bytes), 'Input must be a byte array')
|
assert(Array.isArray(bytes) || Buffer.isBuffer(bytes), 'Input must be a byte array')
|
||||||
var words = []
|
var words = []
|
||||||
|
@ -57,15 +27,14 @@ function wordArrayToBytes(wordArray) {
|
||||||
return wordsToBytes(wordArray.words)
|
return wordsToBytes(wordArray.words)
|
||||||
}
|
}
|
||||||
|
|
||||||
function reverseEndian (hex) {
|
function reverseEndian(hex) {
|
||||||
return bytesToHex(hexToBytes(hex).reverse())
|
var buffer = new Buffer(hex, 'hex')
|
||||||
|
Array.prototype.reverse.call(buffer)
|
||||||
|
|
||||||
|
return buffer.toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
lpad: lpad,
|
|
||||||
bytesToHex: bytesToHex,
|
|
||||||
hexToBytes: hexToBytes,
|
|
||||||
bytesToNum: bytesToNum,
|
|
||||||
bytesToWords: bytesToWords,
|
bytesToWords: bytesToWords,
|
||||||
wordsToBytes: wordsToBytes,
|
wordsToBytes: wordsToBytes,
|
||||||
bytesToWordArray: bytesToWordArray,
|
bytesToWordArray: bytesToWordArray,
|
||||||
|
|
|
@ -2,29 +2,6 @@ var assert = require('assert')
|
||||||
var convert = require('../').convert
|
var convert = require('../').convert
|
||||||
|
|
||||||
describe('convert', function() {
|
describe('convert', function() {
|
||||||
describe('bytesToHex', function() {
|
|
||||||
it('handles example 1', function() {
|
|
||||||
assert.equal(convert.bytesToHex([0, 1, 2, 255]), '000102ff')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('hexToBytes', function() {
|
|
||||||
it('handles example 1', function() {
|
|
||||||
assert.deepEqual(convert.hexToBytes('000102ff'), [0, 1, 2, 255])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('converts from bytes to hex and back', function() {
|
|
||||||
var bytes = []
|
|
||||||
for (var i=0 ; i<256 ; ++i) {
|
|
||||||
bytes.push(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
var hex = convert.bytesToHex(bytes)
|
|
||||||
assert.equal(hex.length, 512)
|
|
||||||
assert.deepEqual(convert.hexToBytes(hex), bytes)
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('byte array and word array conversions', function(){
|
describe('byte array and word array conversions', function(){
|
||||||
var bytes, wordArray
|
var bytes, wordArray
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue