Kagera Session Manager
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * sha256.c - Implementation of the Secure Hash Algorithm-256 (SHA-256).
  3. *
  4. * Implemented from the description on the NIST Web site:
  5. * http://csrc.nist.gov/cryptval/shs.html
  6. *
  7. *
  8. * Copyright (C) 2012 Rafal Kupiec (customized for sessmgr)
  9. * Copyright (C) 2009 Eric Bishop (adapted as standalone utility)
  10. * Copyright (C) 2002 Southern Storm Software, Pty Ltd.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <unistd.h>
  31. #include "sha256.h"
  32. static void ProcessBlock(SHA256Context *sha, const unsigned char *block) {
  33. u_int32_t W[64];
  34. u_int32_t a, b, c, d, e, f, g, h;
  35. u_int32_t temp, temp2;
  36. int t;
  37. for(t = 0; t < 16; ++t) {
  38. W[t] = (((u_int32_t) (block[t * 4 + 0])) << 24) | (((u_int32_t) (block[t * 4 + 1])) << 16) | (((u_int32_t) (block[t * 4 + 2])) << 8) | ((u_int32_t) (block[t * 4 + 3]));
  39. }
  40. for(t = 16; t < 64; ++t) {
  41. W[t] = TRUNCLONG(RHO1(W[t - 2]) + W[t - 7] + RHO0(W[t - 15]) + W[t - 16]);
  42. }
  43. a = sha->A;
  44. b = sha->B;
  45. c = sha->C;
  46. d = sha->D;
  47. e = sha->E;
  48. f = sha->F;
  49. g = sha->G;
  50. h = sha->H;
  51. for(t = 0; t < 64; ++t) {
  52. temp = TRUNCLONG(h + SUM1(e) + CH(e, f, g) + K[t] + W[t]);
  53. temp2 = TRUNCLONG(SUM0(a) + MAJ(a, b, c));
  54. h = g;
  55. g = f;
  56. f = e;
  57. e = TRUNCLONG(d + temp);
  58. d = c;
  59. c = b;
  60. b = a;
  61. a = TRUNCLONG(temp + temp2);
  62. }
  63. sha->A = TRUNCLONG(sha->A + a);
  64. sha->B = TRUNCLONG(sha->B + b);
  65. sha->C = TRUNCLONG(sha->C + c);
  66. sha->D = TRUNCLONG(sha->D + d);
  67. sha->E = TRUNCLONG(sha->E + e);
  68. sha->F = TRUNCLONG(sha->F + f);
  69. sha->G = TRUNCLONG(sha->G + g);
  70. sha->H = TRUNCLONG(sha->H + h);
  71. DoMemZero(W, sizeof(u_int32_t) * 64);
  72. a = b = c = d = e = f = g = h = temp = temp2 = 0;
  73. }
  74. void SHA256Data(SHA256Context *sha, const void *buffer, unsigned long len) {
  75. unsigned long templen;
  76. sha->totalLen += (u_int64_t)len;
  77. while(len > 0) {
  78. if(!(sha->inputLen) && len >= 64) {
  79. ProcessBlock(sha, (const unsigned char *) buffer);
  80. buffer = (const void *) (((const unsigned char *) buffer) + 64);
  81. len -= 64;
  82. } else {
  83. templen = len;
  84. if(templen > (64 - sha->inputLen)) {
  85. templen = 64 - sha->inputLen;
  86. }
  87. memcpy(sha->input + sha->inputLen, buffer, templen);
  88. if((sha->inputLen += templen) >= 64) {
  89. ProcessBlock(sha, sha->input);
  90. sha->inputLen = 0;
  91. }
  92. buffer = (const void *) (((const unsigned char *) buffer) + templen);
  93. len -= templen;
  94. }
  95. }
  96. }
  97. void SHA256Finalize(SHA256Context *sha, unsigned char hash[SHA256_HASH_SIZE]) {
  98. u_int64_t totalBits;
  99. if(hash) {
  100. if(sha->inputLen >= 56) {
  101. sha->input[(sha->inputLen)++] = (unsigned char) 0x80;
  102. while(sha->inputLen < 64) {
  103. sha->input[(sha->inputLen)++] = (unsigned char) 0x00;
  104. }
  105. ProcessBlock(sha, sha->input);
  106. sha->inputLen = 0;
  107. } else {
  108. sha->input[(sha->inputLen)++] = (unsigned char) 0x80;
  109. }
  110. while(sha->inputLen < 56) {
  111. sha->input[(sha->inputLen)++] = (unsigned char) 0x00;
  112. }
  113. totalBits = (sha->totalLen << 3);
  114. WriteLong(sha->input + 56, (u_int32_t) (totalBits >> 32));
  115. WriteLong(sha->input + 60, (u_int32_t) totalBits);
  116. ProcessBlock(sha, sha->input);
  117. WriteLong(hash, sha->A);
  118. WriteLong(hash + 4, sha->B);
  119. WriteLong(hash + 8, sha->C);
  120. WriteLong(hash + 12, sha->D);
  121. WriteLong(hash + 16, sha->E);
  122. WriteLong(hash + 20, sha->F);
  123. WriteLong(hash + 24, sha->G);
  124. WriteLong(hash + 28, sha->H);
  125. }
  126. DoMemZero(sha, sizeof(SHA256Context));
  127. }
  128. void SHA256Init(SHA256Context *sha) {
  129. sha->inputLen = 0;
  130. sha->A = 0x6a09e667;
  131. sha->B = 0xbb67ae85;
  132. sha->C = 0x3c6ef372;
  133. sha->D = 0xa54ff53a;
  134. sha->E = 0x510e527f;
  135. sha->F = 0x9b05688c;
  136. sha->G = 0x1f83d9ab;
  137. sha->H = 0x5be0cd19;
  138. sha->totalLen = 0;
  139. }
  140. static void WriteLong(unsigned char *buf, u_int32_t value) {
  141. buf[0] = (unsigned char)(value >> 24);
  142. buf[1] = (unsigned char)(value >> 16);
  143. buf[2] = (unsigned char)(value >> 8);
  144. buf[3] = (unsigned char)value;
  145. }
  146. unsigned char* get_sha256_hash(const char* unhashed) {
  147. SHA256Context sha;
  148. unsigned char* hash = (unsigned char*) malloc(32);
  149. SHA256Init(&sha);
  150. SHA256Data(&sha, unhashed, strlen(unhashed));
  151. SHA256Finalize(&sha, hash);
  152. return hash;
  153. }
  154. char* get_sha256_hash_hex_str(const char* unhashed) {
  155. unsigned char* hashed = get_sha256_hash(unhashed);
  156. char* hashed_hex = (char*) malloc(65);
  157. char next_hex[3];
  158. int index =0;
  159. hashed_hex[0] = '\0';
  160. for(index=0; index < 32; index++) {
  161. sprintf(next_hex, "%02X", hashed[index]);
  162. strcat(hashed_hex, next_hex);
  163. }
  164. free(hashed);
  165. return hashed_hex;
  166. }