-
-
Save ammaraskar/7b4a3f73bee9dc4136539644a0f27e63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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