@@ -2740,6 +2740,102 @@ unsigned int *ComputeMD5(unsigned char *data, int dataSize)
2740
2740
return hash ;
2741
2741
}
2742
2742
2743
+ // Compute SHA-1 hash code
2744
+ // NOTE: Returns a static int[5] array (20 bytes)
2745
+ unsigned int * ComputeSHA1 (unsigned char * data , int dataSize ) {
2746
+ #define ROTATE_LEFT (x , c ) (((x) << (c)) | ((x) >> (32 - (c))))
2747
+
2748
+ static unsigned int hash [5 ] = { 0 }; // Hash to be returned
2749
+
2750
+ // Initialize hash values
2751
+ hash [0 ] = 0x67452301 ;
2752
+ hash [1 ] = 0xEFCDAB89 ;
2753
+ hash [2 ] = 0x98BADCFE ;
2754
+ hash [3 ] = 0x10325476 ;
2755
+ hash [4 ] = 0xC3D2E1F0 ;
2756
+
2757
+ // Pre-processing: adding a single 1 bit
2758
+ // Append '1' bit to message
2759
+ // NOTE: The input bytes are considered as bits strings,
2760
+ // where the first bit is the most significant bit of the byte
2761
+
2762
+ // Pre-processing: padding with zeros
2763
+ // Append '0' bit until message length in bit 448 (mod 512)
2764
+ // Append length mod (2 pow 64) to message
2765
+
2766
+ int newDataSize = ((((dataSize + 8 )/64 ) + 1 )* 64 );
2767
+
2768
+ unsigned char * msg = RL_CALLOC (newDataSize , 1 ); // Initialize with '0' bits
2769
+ memcpy (msg , data , dataSize );
2770
+ msg [dataSize ] = 128 ; // Write the '1' bit
2771
+
2772
+ unsigned int bitsLen = 8 * dataSize ;
2773
+ msg [newDataSize - 1 ] = bitsLen ;
2774
+
2775
+ // Process the message in successive 512-bit chunks
2776
+ for (int offset = 0 ; offset < newDataSize ; offset += (512 /8 ))
2777
+ {
2778
+ // Break chunk into sixteen 32-bit words w[j], 0 <= j <= 15
2779
+ unsigned int w [80 ] = {0 };
2780
+ for (int i = 0 ; i < 16 ; i ++ ) {
2781
+ w [i ] = (msg [offset + (i * 4 ) + 0 ] << 24 ) |
2782
+ (msg [offset + (i * 4 ) + 1 ] << 16 ) |
2783
+ (msg [offset + (i * 4 ) + 2 ] << 8 ) |
2784
+ (msg [offset + (i * 4 ) + 3 ]);
2785
+ }
2786
+
2787
+ // Message schedule: extend the sixteen 32-bit words into eighty 32-bit words:
2788
+ for (int i = 16 ; i < 80 ; ++ i ) {
2789
+ w [i ] = ROTATE_LEFT (w [i - 3 ] ^ w [i - 8 ] ^ w [i - 14 ] ^ w [i - 16 ], 1 );
2790
+ }
2791
+
2792
+ // Initialize hash value for this chunk
2793
+ unsigned int a = hash [0 ];
2794
+ unsigned int b = hash [1 ];
2795
+ unsigned int c = hash [2 ];
2796
+ unsigned int d = hash [3 ];
2797
+ unsigned int e = hash [4 ];
2798
+
2799
+ for (int i = 0 ; i < 80 ; i ++ )
2800
+ {
2801
+ unsigned int f = 0 ;
2802
+ unsigned int k = 0 ;
2803
+
2804
+ if (i < 20 ) {
2805
+ f = (b & c ) | ((~b ) & d );
2806
+ k = 0x5A827999 ;
2807
+ } else if (i < 40 ) {
2808
+ f = b ^ c ^ d ;
2809
+ k = 0x6ED9EBA1 ;
2810
+ } else if (i < 60 ) {
2811
+ f = (b & c ) | (b & d ) | (c & d );
2812
+ k = 0x8F1BBCDC ;
2813
+ } else {
2814
+ f = b ^ c ^ d ;
2815
+ k = 0xCA62C1D6 ;
2816
+ }
2817
+
2818
+ unsigned int temp = ROTATE_LEFT (a , 5 ) + f + e + k + w [i ];
2819
+ e = d ;
2820
+ d = c ;
2821
+ c = ROTATE_LEFT (b , 30 );
2822
+ b = a ;
2823
+ a = temp ;
2824
+ }
2825
+
2826
+ // Add this chunk's hash to result so far
2827
+ hash [0 ] += a ;
2828
+ hash [1 ] += b ;
2829
+ hash [2 ] += c ;
2830
+ hash [3 ] += d ;
2831
+ hash [4 ] += e ;
2832
+ }
2833
+
2834
+ free (msg );
2835
+
2836
+ return hash ;
2837
+ }
2838
+
2743
2839
//----------------------------------------------------------------------------------
2744
2840
// Module Functions Definition: Automation Events Recording and Playing
2745
2841
//----------------------------------------------------------------------------------
0 commit comments