Skip to content

Instantly share code, notes, and snippets.

@timmyRS
Last active January 20, 2023 02:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmyRS/e1922033029e9171c00b21c4a19c7c81 to your computer and use it in GitHub Desktop.
Save timmyRS/e1922033029e9171c00b21c4a19c7c81 to your computer and use it in GitHub Desktop.
Generates Minecraft-style SHA1 hashes in PHP.
<?php
/**
* Generates a Minecraft-style SHA1 hash.
* @param string $str
* @return string
*/
function mcsha1($str)
{
$gmp = gmp_import(sha1($str, true));
if(gmp_cmp($gmp, gmp_init("0x8000000000000000000000000000000000000000")) >= 0)
{
$gmp = gmp_mul(gmp_add(gmp_xor($gmp, gmp_init("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")), gmp_init(1)), gmp_init(-1));
}
return gmp_strval($gmp, 16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment