2014-01-11 07:57:43 +01:00
|
|
|
/* global describe, it */
|
2013-02-17 06:39:15 +01:00
|
|
|
var assert = require('assert');
|
2014-01-11 07:57:43 +01:00
|
|
|
var convert = require('../src/convert.js');
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
describe('convert', function() {
|
|
|
|
describe('bytesToHex', function() {
|
|
|
|
it('handles example 1', function() {
|
|
|
|
assert.equal(convert.bytesToHex([0, 1, 2, 255]), '000102ff');
|
|
|
|
})
|
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
describe('hexToBytes', function() {
|
|
|
|
it('handles example 1', function() {
|
|
|
|
assert.deepEqual(convert.hexToBytes('000102ff'), [0, 1, 2, 255]);
|
|
|
|
})
|
|
|
|
})
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
it('converts from bytes to hex and back', function() {
|
|
|
|
var bytes = [];
|
|
|
|
for (var i=0 ; i<256 ; ++i) {
|
|
|
|
bytes.push(i);
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-01-11 07:57:43 +01:00
|
|
|
var hex = convert.bytesToHex(bytes);
|
|
|
|
assert.equal(hex.length, 512);
|
|
|
|
assert.deepEqual(convert.hexToBytes(hex), bytes);
|
|
|
|
})
|
2014-03-01 17:05:49 +01:00
|
|
|
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
})
|
2014-03-11 02:52:48 +01:00
|
|
|
|
|
|
|
describe('byte array and word array conversions', function(){
|
|
|
|
var bytes, wordArray;
|
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
bytes = [
|
|
|
|
98, 233, 7, 177, 92, 191, 39, 213, 66, 83,
|
|
|
|
153, 235, 246, 240, 251, 80, 235, 184, 143, 24
|
|
|
|
]
|
|
|
|
wordArray = {
|
|
|
|
words: [1659439025, 1556031445, 1112775147, -151979184, -340226280],
|
|
|
|
sigBytes: 20
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('bytesToWords', function() {
|
|
|
|
it('works', function() {
|
|
|
|
assert.deepEqual(convert.bytesToWordArray(bytes), wordArray)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('bytesToWords', function() {
|
|
|
|
it('works', function() {
|
|
|
|
assert.deepEqual(convert.wordArrayToBytes(wordArray), bytes)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-01-11 07:57:43 +01:00
|
|
|
})
|