You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
972 B
37 lines
972 B
var Buffer = require('buffer').Buffer;
|
|
var intSize = 4;
|
|
var zeroBuffer = Buffer.alloc(intSize);
|
|
zeroBuffer.fill(0);
|
|
var chrsz = 8;
|
|
|
|
function toArray(buf, bigEndian) {
|
|
if (buf.length % intSize !== 0) {
|
|
var len = buf.length + (intSize - (buf.length % intSize));
|
|
buf = Buffer.concat([buf, zeroBuffer], len);
|
|
}
|
|
|
|
var arr = [];
|
|
var fn = bigEndian ? buf.readInt32BE : buf.readInt32LE;
|
|
for (var i = 0; i < buf.length; i += intSize) {
|
|
arr.push(fn.call(buf, i));
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
function toBuffer(arr, size, bigEndian) {
|
|
var buf = Buffer.alloc(size);
|
|
var fn = bigEndian ? buf.writeInt32BE : buf.writeInt32LE;
|
|
for (var i = 0; i < arr.length; i++) {
|
|
fn.call(buf, arr[i], i * 4, true);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
function hash(buf, fn, hashSize, bigEndian) {
|
|
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
|
|
var arr = fn(toArray(buf, bigEndian), buf.length * chrsz);
|
|
return toBuffer(arr, hashSize, bigEndian);
|
|
}
|
|
|
|
module.exports = { hash: hash };
|