Skip to content

Instantly share code, notes, and snippets.

@ammaraskar
Last active June 21, 2022 01:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ammaraskar/7b4a3f73bee9dc4136539644a0f27e63 to your computer and use it in GitHub Desktop.
Save ammaraskar/7b4a3f73bee9dc4136539644a0f27e63 to your computer and use it in GitHub Desktop.
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