Internal: refactor uint8ArrayToHex for performance and to avoid branching

This commit is contained in:
larabr 2023-03-30 13:27:16 +02:00
parent 56cd448a32
commit 089a14f9e0
2 changed files with 5 additions and 13 deletions

View File

@ -154,18 +154,10 @@ const util = {
* @returns {String} Hexadecimal representation of the array.
*/
uint8ArrayToHex: function (bytes) {
const r = [];
const e = bytes.length;
let c = 0;
let h;
while (c < e) {
h = bytes[c++].toString(16);
while (h.length < 2) {
h = '0' + h;
}
r.push('' + h);
}
return r.join('');
const hexAlphabet = '0123456789abcdef';
let s = '';
bytes.forEach(v => { s += hexAlphabet[v >> 4] + hexAlphabet[v & 15]; });
return s;
},
/**

View File

@ -80,7 +80,7 @@ export default () => describe('TripleDES (EDE) cipher test with test vectors fro
expect(encr, 'vector with block ' + util.uint8ArrayToHex(testvectors[i][0]) +
' and key ' + util.uint8ArrayToHex(key) +
' should be ' + util.uint8ArrayToHex(testvectors[i][1]) +
' != ' + util.uint8ArrayToHex(encr)).to.be.equal(util.uint8ArrayToString(testvectors[i][1]));
' != ' + encr).to.be.equal(util.uint8ArrayToString(testvectors[i][1]));
}
done();
});