Skip to content

Instantly share code, notes, and snippets.

@ammaraskar ammaraskar/MinecraftSha.cs Secret
Last active May 31, 2020

Embed
What would you like to do?
using System;
using System.Text;
using System.Security.Cryptography;
using System.Numerics;
public class Test
{
public static void Main()
{
Console.WriteLine("Notch: " + MinecraftShaDigest("Notch"));
Console.WriteLine("jeb_: " + MinecraftShaDigest("jeb_"));
Console.WriteLine("simon: " + MinecraftShaDigest("simon"));
}
public static String MinecraftShaDigest(String input)
{
var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input));
// Reverse the bytes since BigInteger uses little endian
Array.Reverse(hash);
BigInteger b = new BigInteger(hash);
// very annoyingly, BigInteger in C# tries to be smart and puts in
// a leading 0 when formatting as a hex number to allow roundtripping
// of negative numbers, thus we have to trim it off.
if (b < 0)
{
// toss in a negative sign if the interpreted number is negative
return "-" + (-b).ToString("x").TrimStart('0');
}
else
{
return b.ToString("x").TrimStart('0');
}
}
}
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.