fork download
  1. #include <stdio.h>
  2.  
  3.  
  4. // Code by: B-Con (http://b...content-available-to-author-only...n.us)
  5. // Released under the GNU GPL
  6. // MD5 Hash Digest implementation (little endian byte order)
  7.  
  8. #include <stdio.h>
  9.  
  10. // Signed variables are for wimps
  11. #define uchar unsigned char
  12. #define uint unsigned int
  13.  
  14. // DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it
  15. #define ROTLEFT(a,b) ((a << b) | (a >> (32-b)))
  16. #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - c) ++b; a += c;
  17.  
  18.  
  19. typedef struct {
  20. uchar data[64];
  21. uint datalen;
  22. uint bitlen[2];
  23. uint state[5];
  24. uint k[4];
  25. } SHA1_CTX;
  26.  
  27.  
  28. void sha1_transform(SHA1_CTX *ctx, uchar data[])
  29. {
  30. uint a,b,c,d,e,i,j,t,m[80];
  31.  
  32. for (i=0,j=0; i < 16; ++i, j += 4)
  33. m[i] = (data[j] << 24) + (data[j+1] << 16) + (data[j+2] << 8) + (data[j+3]);
  34. for ( ; i < 80; ++i) {
  35. m[i] = (m[i-3] ^ m[i-8] ^ m[i-14] ^ m[i-16]);
  36. m[i] = (m[i] << 1) | (m[i] >> 31);
  37. }
  38.  
  39. a = ctx->state[0];
  40. b = ctx->state[1];
  41. c = ctx->state[2];
  42. d = ctx->state[3];
  43. e = ctx->state[4];
  44.  
  45. for (i=0; i < 20; ++i) {
  46. t = ROTLEFT(a,5) + ((b & c) ^ (~b & d)) + e + ctx->k[0] + m[i];
  47. e = d;
  48. d = c;
  49. c = ROTLEFT(b,30);
  50. b = a;
  51. a = t;
  52. }
  53. for ( ; i < 40; ++i) {
  54. t = ROTLEFT(a,5) + (b ^ c ^ d) + e + ctx->k[1] + m[i];
  55. e = d;
  56. d = c;
  57. c = ROTLEFT(b,30);
  58. b = a;
  59. a = t;
  60. }
  61. for ( ; i < 60; ++i) {
  62. t = ROTLEFT(a,5) + ((b & c) ^ (b & d) ^ (c & d)) + e + ctx->k[2] + m[i];
  63. e = d;
  64. d = c;
  65. c = ROTLEFT(b,30);
  66. b = a;
  67. a = t;
  68. }
  69. for ( ; i < 80; ++i) {
  70. t = ROTLEFT(a,5) + (b ^ c ^ d) + e + ctx->k[3] + m[i];
  71. e = d;
  72. d = c;
  73. c = ROTLEFT(b,30);
  74. b = a;
  75. a = t;
  76. }
  77.  
  78. ctx->state[0] += a;
  79. ctx->state[1] += b;
  80. ctx->state[2] += c;
  81. ctx->state[3] += d;
  82. ctx->state[4] += e;
  83. }
  84.  
  85. void sha1_init(SHA1_CTX *ctx)
  86. {
  87. ctx->datalen = 0;
  88. ctx->bitlen[0] = 0;
  89. ctx->bitlen[1] = 0;
  90. ctx->state[0] = 0x67452301;
  91. ctx->state[1] = 0xEFCDAB89;
  92. ctx->state[2] = 0x98BADCFE;
  93. ctx->state[3] = 0x10325476;
  94. ctx->state[4] = 0xc3d2e1f0;
  95. ctx->k[0] = 0x5a827999;
  96. ctx->k[1] = 0x6ed9eba1;
  97. ctx->k[2] = 0x8f1bbcdc;
  98. ctx->k[3] = 0xca62c1d6;
  99. }
  100.  
  101. void sha1_update(SHA1_CTX *ctx, uchar data[], uint len)
  102. {
  103. uint t,i;
  104.  
  105. for (i=0; i < len; ++i) {
  106. ctx->data[ctx->datalen] = data[i];
  107. ctx->datalen++;
  108. if (ctx->datalen == 64) {
  109. sha1_transform(ctx,ctx->data);
  110. DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],512);
  111. ctx->datalen = 0;
  112. }
  113. }
  114. }
  115.  
  116. void sha1_final(SHA1_CTX *ctx, uchar hash[])
  117. {
  118. uint i;
  119.  
  120. i = ctx->datalen;
  121.  
  122. // Pad whatever data is left in the buffer.
  123. if (ctx->datalen < 56) {
  124. ctx->data[i++] = 0x80;
  125. while (i < 56)
  126. ctx->data[i++] = 0x00;
  127. }
  128. else {
  129. ctx->data[i++] = 0x80;
  130. while (i < 64)
  131. ctx->data[i++] = 0x00;
  132. sha1_transform(ctx,ctx->data);
  133. memset(ctx->data,0,56);
  134. }
  135.  
  136. // Append to the padding the total message's length in bits and transform.
  137. DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],8 * ctx->datalen);
  138. ctx->data[63] = ctx->bitlen[0];
  139. ctx->data[62] = ctx->bitlen[0] >> 8;
  140. ctx->data[61] = ctx->bitlen[0] >> 16;
  141. ctx->data[60] = ctx->bitlen[0] >> 24;
  142. ctx->data[59] = ctx->bitlen[1];
  143. ctx->data[58] = ctx->bitlen[1] >> 8;
  144. ctx->data[57] = ctx->bitlen[1] >> 16;
  145. ctx->data[56] = ctx->bitlen[1] >> 24;
  146. sha1_transform(ctx,ctx->data);
  147.  
  148. // Since this implementation uses little endian byte ordering and MD uses big endian,
  149. // reverse all the bytes when copying the final state to the output hash.
  150. for (i=0; i < 4; ++i) {
  151. hash[i] = (ctx->state[0] >> (24-i*8)) & 0x000000ff;
  152. hash[i+4] = (ctx->state[1] >> (24-i*8)) & 0x000000ff;
  153. hash[i+8] = (ctx->state[2] >> (24-i*8)) & 0x000000ff;
  154. hash[i+12] = (ctx->state[3] >> (24-i*8)) & 0x000000ff;
  155. hash[i+16] = (ctx->state[4] >> (24-i*8)) & 0x000000ff;
  156. }
  157. }
  158.  
  159.  
  160. int main(void) {
  161. // your code goes here
  162. SHA1_CTX hashctx;
  163. unsigned char kisfaszom[3] = { 0x61, 0x62, 0x63 };
  164. unsigned char hash[20];
  165.  
  166. sha1_init (&hashctx);
  167. sha1_update (&hashctx, &kisfaszom[0], 2);
  168. sha1_update (&hashctx, &kisfaszom[2], 1);
  169. sha1_final(&hashctx, &hash[0]);
  170.  
  171. for (unsigned char i = 0; i<20; i++)
  172. {
  173. printf ("%x",hash[i]);
  174.  
  175. }
  176. return 0;
  177. }
  178.  
Success #stdin #stdout 0s 9416KB
stdin
Standard input is empty
stdout
a9993e36476816aba3e25717850c26c9cd0d89d