@@ -3080,6 +3080,93 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize)
30803080 return hash ;
30813081}
30823082
3083+ // Compute SHA-256 hash code
3084+ // NOTE: Returns a static int[8] array (32 bytes)
3085+ unsigned int * ComputeSHA256 (unsigned char * data , int dataSize ) {
3086+ #define SHA256_ROTR_32 (x , n ) (((x) >> (n)) | ((x) << (32 - (n))))
3087+ static unsigned int result [8 ];
3088+ if (!data || dataSize < 0 ){
3089+ return NULL ;
3090+ }
3091+
3092+ unsigned int h [8 ] = {
3093+ 0x6a09e667u , 0xbb67ae85u , 0x3c6ef372u , 0xa54ff53au ,
3094+ 0x510e527fu , 0x9b05688cu , 0x1f83d9abu , 0x5be0cd19u
3095+ };
3096+
3097+ const unsigned int k [64 ] = {
3098+ 0x428a2f98u , 0x71374491u , 0xb5c0fbcfu , 0xe9b5dba5u , 0x3956c25bu , 0x59f111f1u , 0x923f82a4u , 0xab1c5ed5u ,
3099+ 0xd807aa98u , 0x12835b01u , 0x243185beu , 0x550c7dc3u , 0x72be5d74u , 0x80deb1feu , 0x9bdc06a7u , 0xc19bf174u ,
3100+ 0xe49b69c1u , 0xefbe4786u , 0x0fc19dc6u , 0x240ca1ccu , 0x2de92c6fu , 0x4a7484aau , 0x5cb0a9dcu , 0x76f988dau ,
3101+ 0x983e5152u , 0xa831c66du , 0xb00327c8u , 0xbf597fc7u , 0xc6e00bf3u , 0xd5a79147u , 0x06ca6351u , 0x14292967u ,
3102+ 0x27b70a85u , 0x2e1b2138u , 0x4d2c6dfcu , 0x53380d13u , 0x650a7354u , 0x766a0abbu , 0x81c2c92eu , 0x92722c85u ,
3103+ 0xa2bfe8a1u , 0xa81a664bu , 0xc24b8b70u , 0xc76c51a3u , 0xd192e819u , 0xd6990624u , 0xf40e3585u , 0x106aa070u ,
3104+ 0x19a4c116u , 0x1e376c08u , 0x2748774cu , 0x34b0bcb5u , 0x391c0cb3u , 0x4ed8aa4au , 0x5b9cca4fu , 0x682e6ff3u ,
3105+ 0x748f82eeu , 0x78a5636fu , 0x84c87814u , 0x8cc70208u , 0x90befffau , 0xa4506cebu , 0xbef9a3f7u , 0xc67178f2u
3106+ };
3107+
3108+ uint64_t bit_len = (uint64_t )dataSize * 8ULL ;
3109+ int pad_len = (int )((56 - ((dataSize + 1 ) % 64 ) + 64 ) % 64 );
3110+ size_t total_len = (size_t )dataSize + 1 + pad_len + 8 ;
3111+ unsigned char * buf = (unsigned char * )RL_CALLOC (total_len , 1 );
3112+ if (!buf ){
3113+ return NULL ;
3114+ }
3115+
3116+ memcpy (buf , data , (size_t )dataSize );
3117+ buf [dataSize ] = 0x80u ;
3118+ for (int i = 0 ; i < 8 ; ++ i ){
3119+ buf [dataSize + 1 + pad_len + i ] = (unsigned char )((bit_len >> (56 - 8 * i )) & 0xFFu );
3120+ }
3121+
3122+ size_t num_blocks = total_len / 64 ;
3123+ for (size_t blk = 0 ; blk < num_blocks ; ++ blk ) {
3124+ const unsigned char * chunk = buf + blk * 64 ;
3125+ unsigned int w [64 ];
3126+ for (int t = 0 ; t < 16 ; ++ t ){
3127+ w [t ] = ((unsigned int )chunk [t * 4 + 0 ] << 24 ) | ((unsigned int )chunk [t * 4 + 1 ] << 16 ) |
3128+ ((unsigned int )chunk [t * 4 + 2 ] << 8 ) | ((unsigned int )chunk [t * 4 + 3 ]);
3129+ }
3130+ for (int t = 16 ; t < 64 ; ++ t ) {
3131+ unsigned int s0 = SHA256_ROTR_32 (w [t - 15 ], 7 ) ^ SHA256_ROTR_32 (w [t - 15 ], 18 ) ^ (w [t - 15 ] >> 3 );
3132+ unsigned int s1 = SHA256_ROTR_32 (w [t - 2 ], 17 ) ^ SHA256_ROTR_32 (w [t - 2 ], 19 ) ^ (w [t - 2 ] >> 10 );
3133+ w [t ] = w [t - 16 ] + s0 + w [t - 7 ] + s1 ;
3134+ }
3135+ unsigned int a = h [0 ], b = h [1 ], c = h [2 ], d = h [3 ], e = h [4 ], f = h [5 ], g = h [6 ], hh = h [7 ];
3136+ for (int t = 0 ; t < 64 ; ++ t ) {
3137+ unsigned int S1 = SHA256_ROTR_32 (e , 6 ) ^ SHA256_ROTR_32 (e , 11 ) ^ SHA256_ROTR_32 (e , 25 );
3138+ unsigned int ch = (e & f ) ^ ((~e ) & g );
3139+ unsigned int temp1 = hh + S1 + ch + k [t ] + w [t ];
3140+ unsigned int S0 = SHA256_ROTR_32 (a , 2 ) ^ SHA256_ROTR_32 (a , 13 ) ^ SHA256_ROTR_32 (a , 22 );
3141+ unsigned int maj = (a & b ) ^ (a & c ) ^ (b & c );
3142+ unsigned int temp2 = S0 + maj ;
3143+ hh = g ;
3144+ g = f ;
3145+ f = e ;
3146+ e = d + temp1 ;
3147+ d = c ;
3148+ c = b ;
3149+ b = a ;
3150+ a = temp1 + temp2 ;
3151+ }
3152+ h [0 ] += a ;
3153+ h [1 ] += b ;
3154+ h [2 ] += c ;
3155+ h [3 ] += d ;
3156+ h [4 ] += e ;
3157+ h [5 ] += f ;
3158+ h [6 ] += g ;
3159+ h [7 ] += hh ;
3160+ }
3161+
3162+ RL_FREE (buf );
3163+ for (int i = 0 ; i < 8 ; ++ i ){
3164+ result [i ] = (unsigned int )h [i ];
3165+ }
3166+ return result ;
3167+ #undef SHA256_ROTR_32
3168+ }
3169+
30833170//----------------------------------------------------------------------------------
30843171// Module Functions Definition: Automation Events Recording and Playing
30853172//----------------------------------------------------------------------------------
0 commit comments