Merge pull request #745 from pinheadmz/hashstring
util: reverse buffers instead of strings
This commit is contained in:
commit
7136c256fc
17
bench/hexstring.js
Normal file
17
bench/hexstring.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const random = require('bcrypto/lib/random');
|
||||||
|
const util = require('../lib/utils/util');
|
||||||
|
const bench = require('./bench');
|
||||||
|
|
||||||
|
const hashes = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < 100000; i++)
|
||||||
|
hashes.push(random.randomBytes(32));
|
||||||
|
|
||||||
|
{
|
||||||
|
const end = bench('hexstring');
|
||||||
|
for (let i = 0; i < hashes.length; i++)
|
||||||
|
util.fromRev(util.revHex(hashes[i]));
|
||||||
|
end(100000);
|
||||||
|
}
|
||||||
@ -96,31 +96,19 @@ util.time = function time(date) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverse a hex-string.
|
* Reverse a hex-string.
|
||||||
* @param {String} str - Hex string.
|
* @param {Buffer}
|
||||||
* @returns {String} Reversed hex string.
|
* @returns {String} Reversed hex string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
util.revHex = function revHex(buf) {
|
util.revHex = function revHex(buf) {
|
||||||
assert(Buffer.isBuffer(buf));
|
assert(Buffer.isBuffer(buf));
|
||||||
|
|
||||||
const str = buf.toString('hex');
|
return Buffer.from(buf).reverse().toString('hex');
|
||||||
|
|
||||||
let out = '';
|
|
||||||
|
|
||||||
for (let i = str.length - 2; i >= 0; i -= 2)
|
|
||||||
out += str[i] + str[i + 1];
|
|
||||||
|
|
||||||
return out;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
util.fromRev = function fromRev(str) {
|
util.fromRev = function fromRev(str) {
|
||||||
assert(typeof str === 'string');
|
assert(typeof str === 'string');
|
||||||
assert((str.length & 1) === 0);
|
assert((str.length & 1) === 0);
|
||||||
|
|
||||||
let out = '';
|
return Buffer.from(str, 'hex').reverse();
|
||||||
|
|
||||||
for (let i = str.length - 2; i >= 0; i -= 2)
|
|
||||||
out += str[i] + str[i + 1];
|
|
||||||
|
|
||||||
return Buffer.from(out, 'hex');
|
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user