Last active
November 19, 2017 02:43
-
-
Save Talv/70219cf70f1115aeb6e757d4c870a7bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//------------------------------------------------------------------------------------------------------------- | |
// MD5/SHA256 Hash Library | |
// by magnificence7 of SC2Mapster.com. | |
// Project page: <http://www.sc2mapster.com/assets/md5-hash/> | |
//------------------------------------------------------------------------------------------------------------- | |
// '>>' operator is currently broken, so we workaround that by using its custom implementation | |
// @Talv | |
int shift_right(int v, int s) | |
{ | |
if (v == -1 || v == 0) { // 0xFFFFFFFF || 0x00000000 | |
// value won't change in any case, so we just exit early | |
return v; | |
} | |
if (s >= 32) { | |
// most compilers would throw an error if we attempted to shift more bits than there really is | |
// but in case of Galaxy, compiler seems to ignore it and just performs mod 32 as a safety check | |
// so we do the same here | |
s %= 32; | |
} | |
if (v & (1 << 31)) { | |
// if MSB is set, we need to clear it | |
// in order to get the correct result from division | |
v = shift_right(v & ~(1 << 31), s); | |
// then we need to set the vacant bits to high | |
// as it would normally happen as a result of sign extension | |
v |= -1 << (31 - s); | |
return v; | |
} | |
// when shifting by more than 18 bits we need to do a second iteration | |
// because int overflows for PowI(2, 19) and above | |
for (; s > 0; s -= 18) { | |
v /= PowI(2, MinI(s, 18)); | |
} | |
return v; | |
} | |
// Hash Input | |
byte[1024] libHash_HashInputData; | |
int libHash_HashInputSize = 0; | |
int libHash_GetHashInputLittleEndianInt( int position ) | |
{ | |
int temp; | |
int result; | |
position *= 4; | |
result = libHash_HashInputData[position]; | |
temp = libHash_HashInputData[position + 1]; | |
result |= temp << 8; | |
temp = libHash_HashInputData[position + 2]; | |
result |= temp << 16; | |
temp = libHash_HashInputData[position + 3]; | |
result |= temp << 24; | |
return result; | |
} | |
int libHash_GetHashInputBigEndianInt( int position ) | |
{ | |
int temp; | |
int result; | |
position *= 4; | |
temp = libHash_HashInputData[position]; | |
result = temp << 24; | |
temp = libHash_HashInputData[position + 1]; | |
result |= temp << 16; | |
temp = libHash_HashInputData[position + 2]; | |
result |= temp << 8; | |
temp = libHash_HashInputData[position + 3]; | |
result |= temp; | |
return result; | |
} | |
// ASCII conversion | |
string libHash_asciiTable = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; | |
byte libHash_CharToAsciiCode(string charContainingString, int charPosition) | |
{ | |
return StringFind( libHash_asciiTable, StringSub( charContainingString, charPosition + 1, charPosition + 1), c_stringCase ); | |
} | |
// Hex Conversion | |
string[16] libHash_HexTable; | |
string libHash_GetHexChar( int number ) | |
{ | |
return libHash_HexTable[number]; | |
} | |
string libHash_GetUnsignedLittleEndianHexRepresentation( int unsignedInt ) | |
{ | |
string representation = ""; | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x000000F0 ), 4) ); | |
representation += libHash_GetHexChar( ( unsignedInt & 0x0000000F ) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x0000F000 ), 12) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x00000F00), 8) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x00F00000 ), 20) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x000F0000), 16) ); | |
if ( unsignedInt >= 0 ) | |
{ | |
representation += libHash_GetHexChar( shift_right(unsignedInt, 28) ); | |
} | |
else | |
{ | |
unsignedInt += 0x7FFFFFFF + 1; | |
representation += libHash_GetHexChar( 0x8 + ( shift_right(unsignedInt, 28) ) ); | |
} | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x0F000000), 24) ); | |
return representation; | |
} | |
string libHash_GetUnsignedHexRepresentation( int unsignedInt ) | |
{ | |
string representation = ""; | |
if ( unsignedInt >= 0 ) | |
{ | |
representation += libHash_GetHexChar( shift_right(unsignedInt, 28) ); | |
} | |
else | |
{ | |
unsignedInt += 0x7FFFFFFF + 1; | |
representation += libHash_GetHexChar( 0x8 + ( shift_right(unsignedInt, 28) ) ); | |
} | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x0F000000), 24) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x00F00000 ), 20) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x000F0000), 16) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x0000F000 ), 12) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x00000F00), 8) ); | |
representation += libHash_GetHexChar( shift_right(( unsignedInt & 0x000000F0 ), 4) ); | |
representation += libHash_GetHexChar( ( unsignedInt & 0x0000000F ) ); | |
return representation; | |
} | |
// Resolve data function | |
int libHash_GetFixedDataAsInt(fixed fixedVar) | |
{ | |
int data; | |
data= ( FixedToInt(fixedVar) << 12 ); | |
fixedVar -= data; | |
data|= FixedToInt( fixedVar* Pow2(12) ); | |
return data; | |
} | |
// Operations | |
int libHash_UnsignedRightShift( int input, int shift ) // Right shift pads bit 1's when the value is under zero for signed integers. So this is the work-around. | |
{ | |
return shift_right(input, shift) & ~( -1 << ( 32 - shift ) ); | |
} | |
int libHash_UnsignedLeftRotate( int input, int rotation ) | |
{ | |
rotation = ModI( rotation, 32 ); | |
return ( input << rotation ) | libHash_UnsignedRightShift (input, 32 - rotation ); | |
} | |
int libHash_UnsignedRightRotate( int input, int rotation ) | |
{ | |
rotation = ModI( rotation, 32 ); | |
return libHash_UnsignedRightShift( input, rotation ) | ( input << ( 32 - rotation ) ); | |
} | |
int libHash_UnsignedLeftRotate32( int input, int rotation ) // Optimized version for rotation within range 0 =< x =< 32 | |
{ | |
return ( input << rotation ) | libHash_UnsignedRightShift (input, 32 - rotation ); | |
} | |
int libHash_UnsignedRightRotate32( int input, int rotation ) // Optimized version for rotation within range 0 =< x =< 32 | |
{ | |
return libHash_UnsignedRightShift( input, rotation ) | ( input << ( 32 - rotation ) ); | |
} | |
//-------------------------------------------------------------------------------------------------- | |
// Custom Script: MD5 Script | |
//-------------------------------------------------------------------------------------------------- | |
int libHash_Md5F( int b, int c, int d ) | |
{ | |
return d ^ ( b & ( c ^ d ) ); | |
} | |
int libHash_Md5G( int b, int c, int d ) | |
{ | |
return c ^ ( d & ( b ^ c ) ); | |
} | |
int libHash_Md5H( int b, int c, int d ) | |
{ | |
return b ^ c ^ d; | |
} | |
int libHash_Md5I( int b, int c, int d ) | |
{ | |
return c ^ (b | (~d)); | |
} | |
int libHash_Md5FF( int a, int b, int c, int d, int w, int r, int k ) | |
{ | |
return libHash_UnsignedLeftRotate32( ( a + libHash_Md5F( b, c, d ) + k + w ), r ) + b; | |
} | |
int libHash_Md5GG( int a, int b, int c, int d, int w, int r, int k ) | |
{ | |
return libHash_UnsignedLeftRotate32( ( a + libHash_Md5G( b, c, d ) + k + w ), r ) + b; | |
} | |
int libHash_Md5HH( int a, int b, int c, int d, int w, int r, int k ) | |
{ | |
return libHash_UnsignedLeftRotate32( ( a + libHash_Md5H( b, c, d ) + k + w ), r ) + b; | |
} | |
int libHash_Md5II( int a, int b, int c, int d, int w, int r, int k ) | |
{ | |
return libHash_UnsignedLeftRotate32( ( a + libHash_Md5I( b, c, d ) + k + w ), r ) + b; | |
} | |
string libHash_GeneratelibHash_Md5HashCode() | |
{ | |
// Variables | |
int h0 = 0x67452301; | |
int h1 = 0x0E000000 << 4 | 0x0FCDAB89; | |
int h2 = 0x09000000 << 4 | 0x08BADCFE; | |
int h3 = 0x10325476; | |
int a; int b; int c; int d; | |
int i = 0; int j = 0; | |
int messageNBits; | |
int paddingLength; | |
// Prepare message | |
messageNBits = libHash_HashInputSize * 8; | |
libHash_HashInputData[libHash_HashInputSize] = 0x80; | |
libHash_HashInputSize += 1; | |
paddingLength = 56 - ModI( libHash_HashInputSize, 64 ); | |
if ( paddingLength < 0 ) | |
{ | |
paddingLength += 64; | |
} | |
while ( i < paddingLength ) | |
{ | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
i += 1; | |
} | |
//Note: You might want to change this when the hash input increases in size. | |
libHash_HashInputData[libHash_HashInputSize] = messageNBits; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = shift_right(messageNBits, 8); | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
// Loop | |
i = 0; | |
while ( i < ( libHash_HashInputSize - 1 ) ) | |
{ | |
a = h0; | |
b = h1; | |
c = h2; | |
d = h3; | |
a = libHash_Md5FF( a, b, c, d, libHash_GetHashInputLittleEndianInt( j ), 7, 0x0d000000 << 4 | 0x076aa478 ); | |
d = libHash_Md5FF( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 1 ), 12, 0x0e000000 << 4 | 0x08c7b756 ); | |
c = libHash_Md5FF( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 2 ), 17, 0x242070db ); | |
b = libHash_Md5FF( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 3 ), 22, 0x0c000000 << 4 | 0x01bdceee ); | |
a = libHash_Md5FF( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 4 ), 7, 0x0f000000 << 4 | 0x057c0faf ); | |
d = libHash_Md5FF( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 5 ), 12, 0x4787c62a ); | |
c = libHash_Md5FF( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 6 ), 17, 0x0a000000 << 4 | 0x08304613 ); | |
b = libHash_Md5FF( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 7 ), 22, 0x0f000000 << 4 | 0x0d469501 ); | |
a = libHash_Md5FF( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 8 ), 7, 0x698098d8 ); | |
d = libHash_Md5FF( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 9 ), 12, 0x08000000 << 4 | 0x0b44f7af ); | |
c = libHash_Md5FF( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 10 ), 17, 0x0f000000 << 4 | 0x0fff5bb1 ); | |
b = libHash_Md5FF( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 11 ), 22, 0x08000000 << 4 | 0x095cd7be ); | |
a = libHash_Md5FF( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 12 ), 7, 0x6b901122 ); | |
d = libHash_Md5FF( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 13 ), 12, 0x0f000000 << 4 | 0x0d987193 ); | |
c = libHash_Md5FF( c, d, a, b, libHash_GetHashInputLittleEndianInt( j+ 14 ), 17, 0x0a000000 << 4 | 0x0679438e ); | |
b = libHash_Md5FF( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 15 ), 22, 0x49b40821 ); | |
a = libHash_Md5GG( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 1 ), 5, 0x0f000000 << 4 | 0x061e2562 ); | |
d = libHash_Md5GG( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 6 ), 9, 0x0c000000 << 4 | 0x0040b340 ); | |
c = libHash_Md5GG( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 11 ), 14, 0x265e5a51 ); | |
b = libHash_Md5GG( b, c, d, a, libHash_GetHashInputLittleEndianInt( j ), 20, 0x0e000000 << 4 | 0x09b6c7aa ); | |
a = libHash_Md5GG( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 5 ), 5, 0x0d000000 << 4 | 0x062f105d ); | |
d = libHash_Md5GG( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 10 ), 9, 0x02441453 ); | |
c = libHash_Md5GG( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 15 ), 14, 0x0d000000 << 4 | 0x08a1e681 ); | |
b = libHash_Md5GG( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 4 ), 20, 0x0e000000 << 4 | 0x07d3fbc8 ); | |
a = libHash_Md5GG( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 9 ), 5, 0x21e1cde6 ); | |
d = libHash_Md5GG( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 14 ), 9, 0x0c000000 << 4 | 0x033707d6 ); | |
c = libHash_Md5GG( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 3 ), 14, 0x0f000000 << 4 | 0x04d50d87 ); | |
b = libHash_Md5GG( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 8 ), 20, 0x455a14ed ); | |
a = libHash_Md5GG( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 13 ), 5, 0x0a000000 << 4 | 0x09e3e905 ); | |
d = libHash_Md5GG( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 2 ), 9, 0x0f000000 << 4 | 0x0cefa3f8 ); | |
c = libHash_Md5GG( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 7 ), 14, 0x676f02d9 ); | |
b = libHash_Md5GG( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 12 ), 20, 0x08000000 << 4 | 0x0d2a4c8a ); | |
a = libHash_Md5HH( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 5 ), 4, 0x0f000000 << 4 | 0x0ffa3942 ); | |
d = libHash_Md5HH( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 8 ), 11, 0x08000000 << 4 | 0x0771f681 ); | |
c = libHash_Md5HH( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 11 ), 16, 0x6d9d6122 ); | |
b = libHash_Md5HH( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 14 ), 23, 0x0f000000 << 4 | 0x0de5380c ); | |
a = libHash_Md5HH( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 1 ), 4, 0x0a000000 << 4 | 0x04beea44 ); | |
d = libHash_Md5HH( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 4 ), 11, 0x4bdecfa9 ); | |
c = libHash_Md5HH( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 7 ), 16, 0x0f000000 << 4 | 0x06bb4b60 ); | |
b = libHash_Md5HH( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 10 ), 23, 0x0b000000 << 4 | 0x0ebfbc70 ); | |
a = libHash_Md5HH( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 13 ), 4, 0x289b7ec6 ); | |
d = libHash_Md5HH( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 0 ), 11, 0x0e000000 << 4 | 0x0aa127fa ); | |
c = libHash_Md5HH( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 3 ), 16, 0x0d000000 << 4 | 0x04ef3085 ); | |
b = libHash_Md5HH( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 6 ), 23, 0x04881d05 ); | |
a = libHash_Md5HH( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 9 ), 4, 0x0d000000 << 4 | 0x09d4d039 ); | |
d = libHash_Md5HH( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 12 ), 11, 0x0e000000 << 4 | 0x06db99e5 ); | |
c = libHash_Md5HH( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 15 ), 16, 0x1fa27cf8 ); | |
b = libHash_Md5HH( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 2 ), 23, 0x0c000000 << 4 | 0x04ac5665 ); | |
a = libHash_Md5II( a, b, c, d, libHash_GetHashInputLittleEndianInt( j ), 6, 0x0f000000 << 4 | 0x04292244 ); | |
d = libHash_Md5II( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 7 ), 10, 0x432aff97 ); | |
c = libHash_Md5II( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 14 ), 15, 0x0a000000 << 4 | 0x0b9423a7 ); | |
b = libHash_Md5II( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 5 ), 21, 0x0f000000 << 4 | 0x0c93a039 ); | |
a = libHash_Md5II( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 12 ), 6, 0x655b59c3 ); | |
d = libHash_Md5II( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 3 ), 10, 0x08000000 << 4 | 0x0f0ccc92 ); | |
c = libHash_Md5II( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 10 ), 15, 0x0f000000 << 4 | 0x0feff47d ); | |
b = libHash_Md5II( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 1 ), 21, 0x08000000 << 4 | 0x05845dd1 ); | |
a = libHash_Md5II( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 8 ), 6, 0x6fa87e4f ); | |
d = libHash_Md5II( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 15 ), 10, 0x0f000000 << 4 | 0x0e2ce6e0 ); | |
c = libHash_Md5II( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 6 ), 15, 0x0a000000 << 4 | 0x03014314 ); | |
b = libHash_Md5II( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 13 ), 21, 0x4e0811a1 ); | |
a = libHash_Md5II( a, b, c, d, libHash_GetHashInputLittleEndianInt( j + 4 ), 6, 0x0f000000 << 4 | 0x07537e82 ); | |
d = libHash_Md5II( d, a, b, c, libHash_GetHashInputLittleEndianInt( j + 11 ), 10, 0x0b000000 << 4 | 0x0d3af235 ); | |
c = libHash_Md5II( c, d, a, b, libHash_GetHashInputLittleEndianInt( j + 2 ), 15, 0x2ad7d2bb ); | |
b = libHash_Md5II( b, c, d, a, libHash_GetHashInputLittleEndianInt( j + 9 ), 21, 0x0e000000 << 4 | 0x0b86d391 ); | |
h0 += a; | |
h1 += b; | |
h2 += c; | |
h3 += d; | |
i += 64; | |
j += 16; | |
} | |
return libHash_GetUnsignedLittleEndianHexRepresentation( h0 ) + libHash_GetUnsignedLittleEndianHexRepresentation( h1 ) + libHash_GetUnsignedLittleEndianHexRepresentation( h2 ) + libHash_GetUnsignedLittleEndianHexRepresentation( h3 ); | |
} | |
//-------------------------------------------------------------------------------------------------- | |
// Custom Script: SHA256 Script | |
//-------------------------------------------------------------------------------------------------- | |
int libHash_Sha256Ch( int x, int y, int z ) | |
{ | |
return ( x & y ) ^ ( ~x & z ); | |
} | |
int libHash_Sha256Maj( int x, int y, int z ) | |
{ | |
return ( x & y ) ^ ( x & z ) ^ ( y & z ); | |
} | |
int libHash_Sha256CapitalSigma0( int x ) | |
{ | |
return libHash_UnsignedRightRotate32( x, 2 ) ^ libHash_UnsignedRightRotate32( x, 13 ) ^ libHash_UnsignedRightRotate32( x, 22 ); | |
} | |
int libHash_Sha256CapitalSigma1( int x ) | |
{ | |
return libHash_UnsignedRightRotate32( x, 6 ) ^ libHash_UnsignedRightRotate32( x, 11 ) ^ libHash_UnsignedRightRotate32( x, 25 ); | |
} | |
int libHash_Sha256Sigma0( int x ) | |
{ | |
return libHash_UnsignedRightRotate32( x, 7 ) ^ libHash_UnsignedRightRotate32( x, 18 ) ^ libHash_UnsignedRightShift( x, 3 ); | |
} | |
int libHash_Sha256Sigma1( int x ) | |
{ | |
return libHash_UnsignedRightRotate32( x, 17 ) ^ libHash_UnsignedRightRotate32( x, 19 ) ^ libHash_UnsignedRightShift( x, 10 ); | |
} | |
int libHash_Sha256T0( int e, int f, int g, int h, int k, int w ) | |
{ | |
return h + libHash_Sha256CapitalSigma1( e ) + libHash_Sha256Ch( e, f, g ) + k + w; | |
} | |
int libHash_Sha256T1( int a, int b, int c ) | |
{ | |
return libHash_Sha256CapitalSigma0( a ) + libHash_Sha256Maj( a, b, c ); | |
} | |
string libHash_GenerateSha256HashCode() | |
{ | |
// Variables | |
int h0 = 0x6a09e667; | |
int h1 = 0x0b000000 << 4 | 0x0b67ae85; | |
int h2 = 0x3c6ef372; | |
int h3 = 0x0a000000 << 4 | 0x054ff53a; | |
int h4 = 0x510e527f; | |
int h5 = 0x09000000 << 4 | 0x0b05688c; | |
int h6 = 0x1f83d9ab; | |
int h7 = 0x5be0cd19; | |
int a; int b; int c; int d; int e; int f; int g; int h; int t0; int t1; | |
int[64] w; | |
int i = 0; int j = 0; int k = 0; | |
int messageNBits; | |
int paddingLength; | |
// Prepare message | |
messageNBits = libHash_HashInputSize * 8; | |
libHash_HashInputData[libHash_HashInputSize] = 0x80; | |
libHash_HashInputSize += 1; | |
paddingLength = 56 - ModI( libHash_HashInputSize, 64 ); | |
if ( paddingLength < 0 ) | |
{ | |
paddingLength += 64; | |
} | |
while ( i < paddingLength ) | |
{ | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
i += 1; | |
} | |
//Note: You might want to change this when the hash input increases in size. | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = 0; | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = shift_right(messageNBits, 8); | |
libHash_HashInputSize += 1; | |
libHash_HashInputData[libHash_HashInputSize] = messageNBits; | |
libHash_HashInputSize += 1; | |
// Loop | |
i = 0; | |
while ( i < ( libHash_HashInputSize - 1 ) ) | |
{ | |
// Working Variables | |
a = h0; | |
b = h1; | |
c = h2; | |
d = h3; | |
e = h4; | |
f = h5; | |
g = h6; | |
h = h7; | |
// Preparing Extra Message Schedule | |
j = 0; | |
while ( j < 16 ) | |
{ | |
w[j] = libHash_GetHashInputBigEndianInt( k ); | |
j += 1; | |
k += 1; | |
} | |
while ( j < 64 ) | |
{ | |
w[j] = libHash_Sha256Sigma1( w[j - 2] ) + | |
w[j - 7] + | |
libHash_Sha256Sigma0( w[j - 15] ) + | |
w[j - 16]; | |
j += 1; | |
} | |
// 0 to 15 | |
t0 = libHash_Sha256T0( e, f, g, h, 0x428a2f98, w[0] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; i += 1; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x71374491, w[1] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; i += 1; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x0b000000 << 4 | 0x05c0fbcf, w[2] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; i += 1; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x0e000000 << 4 | 0x09b5dba5, w[3] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; i += 1; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x3956c25b, w[4] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; i += 1; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x59f111f1, w[5] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; i += 1; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x09000000 << 4 | 0x023f82a4, w[6] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; i += 1; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x0a000000 << 4 | 0x0b1c5ed5, w[7] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; i += 1; | |
t0 = libHash_Sha256T0( e, f, g, h, 0x0d000000 << 4 | 0x0807aa98, w[8] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; i += 1; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x12835b01, w[9] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; i += 1; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x243185be, w[10] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; i += 1; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x550c7dc3, w[11] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; i += 1; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x72be5d74, w[12] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; i += 1; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x08000000 << 4 | 0x00deb1fe, w[13] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; i += 1; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x09000000 << 4 | 0x0bdc06a7, w[14] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; i += 1; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x0c000000 << 4 | 0x019bf174, w[15] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
// 16 to 31 | |
t0 = libHash_Sha256T0( e, f, g, h, 0x0e000000 << 4 | 0x049b69c1, w[16] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x0e000000 << 4 | 0x0fbe4786, w[17] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x0fc19dc6, w[18] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x240ca1cc, w[19] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x2de92c6f, w[20] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x4a7484aa, w[21] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x5cb0a9dc, w[22] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x76f988da, w[23] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
t0 = libHash_Sha256T0( e, f, g, h, 0x09000000 << 4 | 0x083e5152, w[24] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x0a000000 << 4 | 0x0831c66d, w[25] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x0b000000 << 4 | 0x000327c8, w[26] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x0b000000 << 4 | 0x0f597fc7, w[27] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x0c000000 << 4 | 0x06e00bf3, w[28] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x0d000000 << 4 | 0x05a79147, w[29] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x06ca6351, w[30] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x14292967, w[31] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
// 32 to 47 | |
t0 = libHash_Sha256T0( e, f, g, h, 0x27b70a85, w[32] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x2e1b2138, w[33] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x4d2c6dfc, w[34] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x53380d13, w[35] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x650a7354, w[36] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x766a0abb, w[37] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x08000000 << 4 | 0x01c2c92e, w[38] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x09000000 << 4 | 0x02722c85, w[39] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
t0 = libHash_Sha256T0( e, f, g, h, 0x0a000000 << 4 | 0x02bfe8a1, w[40] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x0a000000 << 4 | 0x081a664b, w[41] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x0c000000 << 4 | 0x024b8b70, w[42] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x0c000000 << 4 | 0x076c51a3, w[43] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x0d000000 << 4 | 0x0192e819, w[44] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x0d000000 << 4 | 0x06990624, w[45] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x0f000000 << 4 | 0x040e3585, w[46] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x106aa070, w[47] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
// 48 to 63 | |
t0 = libHash_Sha256T0( e, f, g, h, 0x19a4c116, w[48] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x1e376c08, w[49] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x2748774c, w[50] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x34b0bcb5, w[51] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x391c0cb3, w[52] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x4ed8aa4a, w[53] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x5b9cca4f, w[54] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x682e6ff3, w[55] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
t0 = libHash_Sha256T0( e, f, g, h, 0x748f82ee, w[56] ); t1 = libHash_Sha256T1( a, b, c ); | |
h = t0 + t1; d += t0; | |
t0 = libHash_Sha256T0( d, e, f, g, 0x78a5636f, w[57] ); t1 = libHash_Sha256T1( h, a, b ); | |
g = t0 + t1; c += t0; | |
t0 = libHash_Sha256T0( c, d, e, f, 0x08000000 << 4 | 0x04c87814, w[58] ); t1 = libHash_Sha256T1( g, h, a ); | |
f = t0 + t1; b += t0; | |
t0 = libHash_Sha256T0( b, c, d, e, 0x08000000 << 4 | 0x0cc70208, w[59] ); t1 = libHash_Sha256T1( f, g, h ); | |
e = t0 + t1; a += t0; | |
t0 = libHash_Sha256T0( a, b, c, d, 0x09000000 << 4 | 0x00befffa, w[60] ); t1 = libHash_Sha256T1( e, f, g ); | |
d = t0 + t1; h += t0; | |
t0 = libHash_Sha256T0( h, a, b, c, 0x0a000000 << 4 | 0x04506ceb, w[61] ); t1 = libHash_Sha256T1( d, e, f ); | |
c = t0 + t1; g += t0; | |
t0 = libHash_Sha256T0( g, h, a, b, 0x0b000000 << 4 | 0x0ef9a3f7, w[62] ); t1 = libHash_Sha256T1( c, d, e ); | |
b = t0 + t1; f += t0; | |
t0 = libHash_Sha256T0( f, g, h, a, 0x0c000000 << 4 | 0x067178f2, w[63] ); t1 = libHash_Sha256T1( b, c, d ); | |
a = t0 + t1; e += t0; | |
h0 += a; | |
h1 += b; | |
h2 += c; | |
h3 += d; | |
h4 += e; | |
h5 += f; | |
h6 += g; | |
h7 += h; | |
i += 49; | |
} | |
return libHash_GetUnsignedHexRepresentation(h0) + | |
libHash_GetUnsignedHexRepresentation(h1) + | |
libHash_GetUnsignedHexRepresentation(h2) + | |
libHash_GetUnsignedHexRepresentation(h3) + | |
libHash_GetUnsignedHexRepresentation(h4) + | |
libHash_GetUnsignedHexRepresentation(h5) + | |
libHash_GetUnsignedHexRepresentation(h6) + | |
libHash_GetUnsignedHexRepresentation(h7); | |
} | |
// Functions | |
void libHash_InitializeHashInput () { | |
// Implementation | |
libHash_HashInputSize = 0; | |
if ( libHash_HexTable[0] == "" ) | |
{ | |
libHash_HexTable[0] = "0"; libHash_HexTable[1] = "1"; libHash_HexTable[2] = "2"; libHash_HexTable[3] = "3"; | |
libHash_HexTable[4] = "4"; libHash_HexTable[5] = "5"; libHash_HexTable[6] = "6"; libHash_HexTable[7] = "7"; | |
libHash_HexTable[8] = "8"; libHash_HexTable[9] = "9"; libHash_HexTable[10] = "a"; libHash_HexTable[11] = "b"; | |
libHash_HexTable[12] = "c"; libHash_HexTable[13] = "d"; libHash_HexTable[14] = "e"; libHash_HexTable[15] = "f"; | |
} | |
} | |
void libHash_AddByteToHashInput (byte lp_byte) { | |
// Implementation | |
libHash_HashInputData[libHash_HashInputSize] = lp_byte; | |
libHash_HashInputSize += 1; | |
} | |
void libHash_AddIntegerToHashInput (int lp_integer) { | |
// Implementation | |
libHash_AddByteToHashInput(lp_integer); | |
libHash_AddByteToHashInput(shift_right(lp_integer, 8)); | |
libHash_AddByteToHashInput(shift_right(lp_integer, 16)); | |
libHash_AddByteToHashInput(shift_right(lp_integer, 24)); | |
} | |
void libHash_AddRealToHashInput (fixed lp_real) { | |
// Implementation | |
libHash_AddIntegerToHashInput(libHash_GetFixedDataAsInt( lp_real )); | |
} | |
void libHash_AddStringToHashInput (string lp_string) { | |
// Implementation | |
int i; | |
for (i = 0; i < StringLength(lp_string); i += 1) { | |
libHash_AddByteToHashInput(libHash_CharToAsciiCode( lp_string, i )); | |
} | |
} | |
void libHash_InitLib() | |
{ | |
libHash_InitializeHashInput(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment