mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-12-02 12:06:53 -05:00
1226f0f520
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
30 lines
777 B
JavaScript
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);
|
|
}
|
|
}
|
|
}
|
|
}
|