node-minecraft-protocol/src/mcHexDigest.js
Romain Beaumont 1226f0f520 split index.js into mainly createClient.js and createServer.js
also create a few other files containing common information and functions :
* mcHexDigest.js which contain the function of the same name used in both createClient.js and createServer.js
* ursa.js which handle the abstraction on top of ursa and ursa-purejs : used in both createClient.js and createServer.js
* version.js used in index.js, createClient.js and createServer.js
2015-07-27 02:26:36 +02:00

30 lines
777 B
JavaScript

module.exports=mcHexDigest;
function mcHexDigest(hash) {
var buffer = new Buffer(hash.digest(), 'binary');
// check for negative hashes
var negative = buffer.readInt8(0) < 0;
if(negative)
performTwosCompliment(buffer);
var digest = buffer.toString('hex');
// trim leading zeroes
digest = digest.replace(/^0+/g, '');
if(negative)
digest = '-' + digest;
return digest;
function performTwosCompliment(buffer) {
var carry = true;
var i, newByte, value;
for(i = buffer.length - 1; i >= 0; --i) {
value = buffer.readUInt8(i);
newByte = ~value & 0xff;
if(carry) {
carry = newByte === 0xff;
buffer.writeUInt8((newByte + 1) & 0xff, i);
} else {
buffer.writeUInt8(newByte, i);
}
}
}
}