Skip to content

Instantly share code, notes, and snippets.

@andrewrk andrewrk/test.js
Created Jan 1, 2013

Embed
What would you like to do?
how to generate minecraft hex digests
var crypto = require('crypto');
var assert = require('assert');
function mcHexDigest(str) {
var hash = new Buffer(crypto.createHash('sha1').update(str).digest(), 'binary');
// check for negative hashes
var negative = hash.readInt8(0) < 0;
if (negative) performTwosCompliment(hash);
var digest = hash.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, i);
} else {
buffer.writeUInt8(newByte, i);
}
}
}
assert.strictEqual(mcHexDigest('Notch'), "4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48");
assert.strictEqual(mcHexDigest('jeb_'), "-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1");
assert.strictEqual(mcHexDigest('simon'), "88e16a1019277b15d58faf0541e11910eb756f6");
@jonathanperret

This comment has been minimized.

Copy link

commented Jun 9, 2017

Please note that there are cases where this code tries to write 256 as a byte and writeUInt8 rejects it. Line 25 should read like this (or something equivalent):

      buffer.writeUInt8(carry ? 0 : newByte + 1, i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.