Skip to content

Commit 17bc628

Browse files
[rcore] Add ComputeSHA256() function (#5264)
* [rcore] Add `ComputeSHA256()` function * adjust function signatures * review issues * fix repeating 0 * fix mistake * fixed macro * remove undefs * review styling mismatches * rename `A0,1` to `SHA256_A0,1` --------- Co-authored-by: CrackedPixel <[email protected]>
1 parent 7191749 commit 17bc628

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

src/raylib.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,9 +1161,10 @@ RLAPI unsigned char *CompressData(const unsigned char *data, int dataSize, int *
11611161
RLAPI unsigned char *DecompressData(const unsigned char *compData, int compDataSize, int *dataSize); // Decompress data (DEFLATE algorithm), memory must be MemFree()
11621162
RLAPI char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize); // Encode data to Base64 string (includes NULL terminator), memory must be MemFree()
11631163
RLAPI unsigned char *DecodeDataBase64(const char *text, int *outputSize); // Decode Base64 string (expected NULL terminated), memory must be MemFree()
1164-
RLAPI unsigned int ComputeCRC32(unsigned char *data, int dataSize); // Compute CRC32 hash code
1165-
RLAPI unsigned int *ComputeMD5(unsigned char *data, int dataSize); // Compute MD5 hash code, returns static int[4] (16 bytes)
1166-
RLAPI unsigned int *ComputeSHA1(unsigned char *data, int dataSize); // Compute SHA1 hash code, returns static int[5] (20 bytes)
1164+
RLAPI unsigned int ComputeCRC32(unsigned char *data, int dataSize); // Compute CRC32 hash code
1165+
RLAPI unsigned int *ComputeMD5(unsigned char *data, int dataSize); // Compute MD5 hash code, returns static int[4] (16 bytes)
1166+
RLAPI unsigned int *ComputeSHA1(unsigned char *data, int dataSize); // Compute SHA1 hash code, returns static int[5] (20 bytes)
1167+
RLAPI unsigned int *ComputeSHA256(unsigned char *data, int dataSize); // Compute SHA256 hash code, returns static int[8] (32 bytes)
11671168

11681169
// Automation events functionality
11691170
RLAPI AutomationEventList LoadAutomationEventList(const char *fileName); // Load automation events list from file, NULL for empty list, capacity = MAX_AUTOMATION_EVENTS

src/rcore.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,108 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize)
30783078
return hash;
30793079
}
30803080

3081+
// Compute SHA-256 hash code
3082+
// NOTE: Returns a static int[8] array (32 bytes)
3083+
unsigned int *ComputeSHA256(unsigned char *data, int dataSize)
3084+
{
3085+
#define ROTATE_RIGHT(x, c) ((x >> c) | (x << ((sizeof(unsigned int) * 8) - c)))
3086+
#define SHA256_A0(x) (ROTATE_RIGHT(x, 7) ^ ROTATE_RIGHT(x, 18) ^ (x >> 3))
3087+
#define SHA256_A1(x) (ROTATE_RIGHT(x, 17) ^ ROTATE_RIGHT(x, 19) ^ (x >> 10))
3088+
3089+
static const unsigned int k[64] = {
3090+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
3091+
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
3092+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
3093+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
3094+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
3095+
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
3096+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
3097+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
3098+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
3099+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
3100+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
3101+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
3102+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
3103+
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
3104+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
3105+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
3106+
};
3107+
3108+
static unsigned int hash[8];
3109+
hash[0] = 0x6A09e667;
3110+
hash[1] = 0xbb67ae85;
3111+
hash[2] = 0x3c6ef372;
3112+
hash[3] = 0xa54ff53a;
3113+
hash[4] = 0x510e527f;
3114+
hash[5] = 0x9b05688c;
3115+
hash[6] = 0x1f83d9ab;
3116+
hash[7] = 0x5be0cd19;
3117+
3118+
const unsigned long long int bitLen = ((unsigned long long int)dataSize)*8;
3119+
unsigned long long int paddedSize = dataSize + sizeof(dataSize);
3120+
paddedSize += (64 - (paddedSize%64));
3121+
unsigned char *buffer = RL_CALLOC(paddedSize, sizeof(unsigned char));
3122+
3123+
memcpy(buffer, data, dataSize);
3124+
buffer[dataSize] = 0x80;
3125+
for (int i = 1; i <= sizeof(bitLen); i++)
3126+
buffer[(paddedSize - sizeof(bitLen)) + (i - 1)] = (bitLen >> (8*(sizeof(bitLen) - i))) & 0xFF;
3127+
3128+
for (unsigned long long int blockN = 0; blockN < paddedSize/64; blockN++)
3129+
{
3130+
unsigned int a = hash[0];
3131+
unsigned int b = hash[1];
3132+
unsigned int c = hash[2];
3133+
unsigned int d = hash[3];
3134+
unsigned int e = hash[4];
3135+
unsigned int f = hash[5];
3136+
unsigned int g = hash[6];
3137+
unsigned int h = hash[7];
3138+
3139+
unsigned char *block = buffer + (blockN*64);
3140+
unsigned int w[64];
3141+
for (int i = 0; i < 16; i++)
3142+
{
3143+
w[i] =
3144+
((unsigned int)block[i*4 + 0] << 24) |
3145+
((unsigned int)block[i*4 + 1] << 16) |
3146+
((unsigned int)block[i*4 + 2] << 8) |
3147+
((unsigned int)block[i*4 + 3]);
3148+
}
3149+
for (int t = 16; t < 64; t++) w[t] = SHA256_A1(w[t - 2]) + w[t - 7] + SHA256_A0(w[t - 15]) + w[t - 16];
3150+
3151+
for (unsigned long long int t = 0; t < 64; t++)
3152+
{
3153+
unsigned int e1 = (ROTATE_RIGHT(e, 6) ^ ROTATE_RIGHT(e, 11) ^ ROTATE_RIGHT(e, 25));
3154+
unsigned int ch = ((e & f) ^ (~e & g));
3155+
unsigned int t1 = (h + e1 + ch + k[t] + w[t]);
3156+
unsigned int e0 = (ROTATE_RIGHT(a, 2) ^ ROTATE_RIGHT(a, 13) ^ ROTATE_RIGHT(a, 22));
3157+
unsigned int maj = ((a & b) ^ (a & c) ^ (b & c));
3158+
unsigned int t2 = e0 + maj;
3159+
3160+
h = g;
3161+
g = f;
3162+
f = e;
3163+
e = d + t1;
3164+
d = c;
3165+
c = b;
3166+
b = a;
3167+
a = t1 + t2;
3168+
}
3169+
3170+
hash[0] += a;
3171+
hash[1] += b;
3172+
hash[2] += c;
3173+
hash[3] += d;
3174+
hash[4] += e;
3175+
hash[5] += f;
3176+
hash[6] += g;
3177+
hash[7] += h;
3178+
}
3179+
RL_FREE(buffer);
3180+
return hash;
3181+
}
3182+
30813183
//----------------------------------------------------------------------------------
30823184
// Module Functions Definition: Automation Events Recording and Playing
30833185
//----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)