Merge pull request #154 from dcousens/convrem

Remove dead code from Convert module
This commit is contained in:
Wei Lu 2014-04-26 12:27:08 +08:00
commit 45c1c8350e
2 changed files with 1 additions and 100 deletions

View file

@ -1,6 +1,5 @@
var Crypto = require('crypto-js') var Crypto = require('crypto-js')
var WordArray = Crypto.lib.WordArray var WordArray = Crypto.lib.WordArray
var base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
function lpad(str, padString, length) { function lpad(str, padString, length) {
while (str.length < length) str = padString + str while (str.length < length) str = padString + str
@ -24,78 +23,6 @@ function hexToBytes(hex) {
}) })
} }
function bytesToBase64(bytes) {
var base64 = []
for (var i = 0; i < bytes.length; i += 3) {
var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 <= bytes.length * 8) {
base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F))
} else {
base64.push('=')
}
}
}
return base64.join('')
}
function base64ToBytes(base64) {
// Remove non-base-64 characters
base64 = base64.replace(/[^A-Z0-9+\/]/ig, '')
var bytes = []
var imod4 = 0
for (var i = 0; i < base64.length; imod4 = ++i % 4) {
if (!imod4) continue
bytes.push(
(
(base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) <<
(imod4 * 2)
) |
(base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2))
)
}
return bytes
}
/**
* Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85)
*/
function coerceToBytes(input) {
if (typeof input != 'string') return input
return hexToBytes(input)
}
function binToBytes(bin) {
return bin.match(/......../g).map(function(x) {
return parseInt(x,2)
})
}
function bytesToBin(bytes) {
return bytes.map(function(x) {
return lpad(x.toString(2), '0', 8)
}).join('')
}
function bytesToString(bytes) {
return bytes.map(function(x){
return String.fromCharCode(x)
}).join('')
}
function stringToBytes(string) {
return string.split('').map(function(x) {
return x.charCodeAt(0)
})
}
/** /**
* Create a byte array representing a number with the given length * Create a byte array representing a number with the given length
*/ */
@ -181,13 +108,6 @@ module.exports = {
lpad: lpad, lpad: lpad,
bytesToHex: bytesToHex, bytesToHex: bytesToHex,
hexToBytes: hexToBytes, hexToBytes: hexToBytes,
bytesToBase64: bytesToBase64,
base64ToBytes: base64ToBytes,
coerceToBytes: coerceToBytes,
binToBytes: binToBytes,
bytesToBin: bytesToBin,
bytesToString: bytesToString,
stringToBytes: stringToBytes,
numToBytes: numToBytes, numToBytes: numToBytes,
bytesToNum: bytesToNum, bytesToNum: bytesToNum,
numToVarInt: numToVarInt, numToVarInt: numToVarInt,

View file

@ -1,5 +1,5 @@
var assert = require('assert') var assert = require('assert')
var convert = require('../src/convert.js') var convert = require('../').convert
describe('convert', function() { describe('convert', function() {
describe('bytesToHex', function() { describe('bytesToHex', function() {
@ -25,25 +25,6 @@ describe('convert', function() {
assert.deepEqual(convert.hexToBytes(hex), bytes) assert.deepEqual(convert.hexToBytes(hex), bytes)
}) })
describe('bytesToBase64', function() {
it('passes RFC4648 test vectors', function() {
// Test vectors from:
// http://tools.ietf.org/html/rfc4648#page-12
var b64 = function(s) {
return convert.bytesToBase64(convert.stringToBytes(s))
}
assert.equal(b64(''), '')
assert.equal(b64('f'), 'Zg==')
assert.equal(b64('fo'), 'Zm8=')
assert.equal(b64('foo'), 'Zm9v')
assert.equal(b64('foob'), 'Zm9vYg==')
assert.equal(b64('fooba'), 'Zm9vYmE=')
assert.equal(b64('foobar'), 'Zm9vYmFy')
})
})
describe('byte array and word array conversions', function(){ describe('byte array and word array conversions', function(){
var bytes, wordArray var bytes, wordArray