Advertisement
Guest User

2

a guest
Mar 17th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. /*
  2. SHA-1 in C
  3. By Steve Reid <steve@edmweb.com>
  4. 100% Public Domain
  5.  
  6. Test Vectors (from FIPS PUB 180-1)
  7. "abc"
  8. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  9. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  10. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  11. A million repetitions of "a"
  12. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  13. */
  14.  
  15. /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
  16. /* #define SHA1HANDSOFF * Copies data before messing with it. */
  17.  
  18. #define SHA1HANDSOFF
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. /* for uint32_t */
  24. #include <stdint.h>
  25.  
  26. //#include "sha1.h" Содержимое файла sha1.h, кроме прототипов функций, вставлено сюда
  27. #ifndef SHA1_H
  28. #define SHA1_H
  29.  
  30. #include "stdint.h"
  31.  
  32. typedef struct
  33. {
  34. uint32_t state[5];
  35. uint32_t count[2];
  36. unsigned char buffer[64];
  37. } SHA1_CTX;
  38.  
  39. #endif /* SHA1_H */
  40.  
  41.  
  42. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  43.  
  44. /* blk0() and blk() perform the initial expand. */
  45. /* I got the idea of expanding during the round function from SSLeay */
  46. #if BYTE_ORDER == LITTLE_ENDIAN
  47. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  48. |(rol(block->l[i],8)&0x00FF00FF))
  49. #elif BYTE_ORDER == BIG_ENDIAN
  50. #define blk0(i) block->l[i]
  51. #else
  52. #error "Endianness not defined!"
  53. #endif
  54. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  55. ^block->l[(i+2)&15]^block->l[i&15],1))
  56.  
  57. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  58. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  59. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  60. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  61. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  62. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  63.  
  64.  
  65. /* Hash a single 512-bit block. This is the core of the algorithm. */
  66.  
  67. void SHA1Transform(
  68. uint32_t state[5],
  69. const unsigned char buffer[64]
  70. )
  71. {
  72. uint32_t a, b, c, d, e;
  73.  
  74. typedef union
  75. {
  76. unsigned char c[64];
  77. uint32_t l[16];
  78. } CHAR64LONG16;
  79.  
  80. #ifdef SHA1HANDSOFF
  81. CHAR64LONG16 block[1]; /* use array to appear as a pointer */
  82.  
  83. memcpy(block, buffer, 64);
  84. #else
  85. /* The following had better never be used because it causes the
  86. * pointer-to-const buffer to be cast into a pointer to non-const.
  87. * And the result is written through. I threw a "const" in, hoping
  88. * this will cause a diagnostic.
  89. */
  90. CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
  91. #endif
  92. /* Copy context->state[] to working vars */
  93. a = state[0];
  94. b = state[1];
  95. c = state[2];
  96. d = state[3];
  97. e = state[4];
  98. /* 4 rounds of 20 operations each. Loop unrolled. */
  99. R0(a, b, c, d, e, 0);
  100. R0(e, a, b, c, d, 1);
  101. R0(d, e, a, b, c, 2);
  102. R0(c, d, e, a, b, 3);
  103. R0(b, c, d, e, a, 4);
  104. R0(a, b, c, d, e, 5);
  105. R0(e, a, b, c, d, 6);
  106. R0(d, e, a, b, c, 7);
  107. R0(c, d, e, a, b, 8);
  108. R0(b, c, d, e, a, 9);
  109. R0(a, b, c, d, e, 10);
  110. R0(e, a, b, c, d, 11);
  111. R0(d, e, a, b, c, 12);
  112. R0(c, d, e, a, b, 13);
  113. R0(b, c, d, e, a, 14);
  114. R0(a, b, c, d, e, 15);
  115. R1(e, a, b, c, d, 16);
  116. R1(d, e, a, b, c, 17);
  117. R1(c, d, e, a, b, 18);
  118. R1(b, c, d, e, a, 19);
  119. R2(a, b, c, d, e, 20);
  120. R2(e, a, b, c, d, 21);
  121. R2(d, e, a, b, c, 22);
  122. R2(c, d, e, a, b, 23);
  123. R2(b, c, d, e, a, 24);
  124. R2(a, b, c, d, e, 25);
  125. R2(e, a, b, c, d, 26);
  126. R2(d, e, a, b, c, 27);
  127. R2(c, d, e, a, b, 28);
  128. R2(b, c, d, e, a, 29);
  129. R2(a, b, c, d, e, 30);
  130. R2(e, a, b, c, d, 31);
  131. R2(d, e, a, b, c, 32);
  132. R2(c, d, e, a, b, 33);
  133. R2(b, c, d, e, a, 34);
  134. R2(a, b, c, d, e, 35);
  135. R2(e, a, b, c, d, 36);
  136. R2(d, e, a, b, c, 37);
  137. R2(c, d, e, a, b, 38);
  138. R2(b, c, d, e, a, 39);
  139. R3(a, b, c, d, e, 40);
  140. R3(e, a, b, c, d, 41);
  141. R3(d, e, a, b, c, 42);
  142. R3(c, d, e, a, b, 43);
  143. R3(b, c, d, e, a, 44);
  144. R3(a, b, c, d, e, 45);
  145. R3(e, a, b, c, d, 46);
  146. R3(d, e, a, b, c, 47);
  147. R3(c, d, e, a, b, 48);
  148. R3(b, c, d, e, a, 49);
  149. R3(a, b, c, d, e, 50);
  150. R3(e, a, b, c, d, 51);
  151. R3(d, e, a, b, c, 52);
  152. R3(c, d, e, a, b, 53);
  153. R3(b, c, d, e, a, 54);
  154. R3(a, b, c, d, e, 55);
  155. R3(e, a, b, c, d, 56);
  156. R3(d, e, a, b, c, 57);
  157. R3(c, d, e, a, b, 58);
  158. R3(b, c, d, e, a, 59);
  159. R4(a, b, c, d, e, 60);
  160. R4(e, a, b, c, d, 61);
  161. R4(d, e, a, b, c, 62);
  162. R4(c, d, e, a, b, 63);
  163. R4(b, c, d, e, a, 64);
  164. R4(a, b, c, d, e, 65);
  165. R4(e, a, b, c, d, 66);
  166. R4(d, e, a, b, c, 67);
  167. R4(c, d, e, a, b, 68);
  168. R4(b, c, d, e, a, 69);
  169. R4(a, b, c, d, e, 70);
  170. R4(e, a, b, c, d, 71);
  171. R4(d, e, a, b, c, 72);
  172. R4(c, d, e, a, b, 73);
  173. R4(b, c, d, e, a, 74);
  174. R4(a, b, c, d, e, 75);
  175. R4(e, a, b, c, d, 76);
  176. R4(d, e, a, b, c, 77);
  177. R4(c, d, e, a, b, 78);
  178. R4(b, c, d, e, a, 79);
  179. /* Add the working vars back into context.state[] */
  180. state[0] += a;
  181. state[1] += b;
  182. state[2] += c;
  183. state[3] += d;
  184. state[4] += e;
  185. /* Wipe variables */
  186. a = b = c = d = e = 0;
  187. #ifdef SHA1HANDSOFF
  188. memset(block, '\0', sizeof(block));
  189. #endif
  190. }
  191.  
  192.  
  193. /* SHA1Init - Initialize new context */
  194.  
  195. void SHA1Init(
  196. SHA1_CTX * context
  197. )
  198. {
  199. /* SHA1 initialization constants */
  200. context->state[0] = 0x67452301;
  201. context->state[1] = 0xEFCDAB89;
  202. context->state[2] = 0x98BADCFE;
  203. context->state[3] = 0x10325476;
  204. context->state[4] = 0xC3D2E1F0;
  205. context->count[0] = context->count[1] = 0;
  206. }
  207.  
  208.  
  209. /* Run your data through this. */
  210.  
  211. void SHA1Update(
  212. SHA1_CTX * context,
  213. const unsigned char *data,
  214. uint32_t len
  215. )
  216. {
  217. uint32_t i;
  218.  
  219. uint32_t j;
  220.  
  221. j = context->count[0];
  222. if ((context->count[0] += len << 3) < j)
  223. context->count[1]++;
  224. context->count[1] += (len >> 29);
  225. j = (j >> 3) & 63;
  226. if ((j + len) > 63)
  227. {
  228. memcpy(&context->buffer[j], data, (i = 64 - j));
  229. SHA1Transform(context->state, context->buffer);
  230. for (; i + 63 < len; i += 64)
  231. {
  232. SHA1Transform(context->state, &data[i]);
  233. }
  234. j = 0;
  235. }
  236. else
  237. i = 0;
  238. memcpy(&context->buffer[j], &data[i], len - i);
  239. }
  240.  
  241.  
  242. /* Add padding and return the message digest. */
  243.  
  244. void SHA1Final(
  245. unsigned char digest[20],
  246. SHA1_CTX * context
  247. )
  248. {
  249. unsigned i;
  250.  
  251. unsigned char finalcount[8];
  252.  
  253. unsigned char c;
  254.  
  255. #if 0 /* untested "improvement" by DHR */
  256. /* Convert context->count to a sequence of bytes
  257. * in finalcount. Second element first, but
  258. * big-endian order within element.
  259. * But we do it all backwards.
  260. */
  261. unsigned char *fcp = &finalcount[8];
  262.  
  263. for (i = 0; i < 2; i++)
  264. {
  265. uint32_t t = context->count[i];
  266.  
  267. int j;
  268.  
  269. for (j = 0; j < 4; t >>= 8, j++)
  270. *--fcp = (unsigned char) t}
  271. #else
  272. for (i = 0; i < 8; i++)
  273. {
  274. finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
  275. }
  276. #endif
  277. c = 0200;
  278. SHA1Update(context, &c, 1);
  279. while ((context->count[0] & 504) != 448)
  280. {
  281. c = 0000;
  282. SHA1Update(context, &c, 1);
  283. }
  284. SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
  285. for (i = 0; i < 20; i++)
  286. {
  287. digest[i] = (unsigned char)
  288. ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  289. }
  290. /* Wipe variables */
  291. memset(context, '\0', sizeof(*context));
  292. memset(&finalcount, '\0', sizeof(finalcount));
  293. }
  294.  
  295. void SHA1(
  296. char *hash_out,
  297. const char *str,
  298. int len)
  299. {
  300. SHA1_CTX ctx;
  301. unsigned int ii;
  302.  
  303. SHA1Init(&ctx);
  304. for (ii=0; ii<len; ii+=1)
  305. SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
  306. SHA1Final((unsigned char *)hash_out, &ctx);
  307. hash_out[20] = '\0';
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement