IdHashSHA1.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.6 2003-10-12 15:25:50 HHellström
  18. Comments added
  19. Rev 1.5 2003-10-12 03:08:24 HHellström
  20. New implementation; copyright changed. The source code formatting has been
  21. adjusted to fit the margins. The new implementation is faster on dotNet
  22. compared to the old one, but is slightly slower on Win32.
  23. Rev 1.4 2003-10-11 18:44:54 HHellström
  24. Range checking and overflow checking disabled in the Coder method only. The
  25. purpose of this setting is to force the arithmetic operations performed on
  26. LongWord variables to be modulo $100000000. This hack entails reasonable
  27. performance on both Win32 and dotNet.
  28. Rev 1.3 10/10/2003 2:20:56 PM GGrieve
  29. turn range checking off
  30. Rev 1.2 2003-09-21 17:31:02 HHellström Version: 1.2
  31. DotNET compatibility
  32. Rev 1.1 2/16/2003 03:19:18 PM JPMugaas
  33. Should now compile on D7 better.
  34. Rev 1.0 11/13/2002 07:53:48 AM JPMugaas
  35. }
  36. unit IdHashSHA1;
  37. interface
  38. {$i IdCompilerDefines.inc}
  39. uses
  40. Classes,
  41. IdGlobal, IdHash;
  42. type
  43. T5x4LongWordRecord = array[0..4] of UInt32;
  44. T512BitRecord = array [0..63] of Byte;
  45. TIdHashSHA1 = class(TIdHash)
  46. protected
  47. FCheckSum: T5x4LongWordRecord;
  48. FCBuffer: TIdBytes;
  49. procedure Coder;
  50. function GetHashBytes(AStream: TStream; ASize: TIdStreamSize): TIdBytes; override;
  51. function HashToHex(const AHash: TIdBytes): String; override;
  52. public
  53. constructor Create; override;
  54. end;
  55. implementation
  56. {$IFNDEF DOTNET}
  57. uses
  58. IdStreamVCL;
  59. {$ENDIF}
  60. { TIdHashSHA1 }
  61. function SwapLongWord(const AValue: UInt32): UInt32;
  62. begin
  63. Result := ((AValue and $FF) shl 24) or ((AValue and $FF00) shl 8) or ((AValue and $FF0000) shr 8) or ((AValue and $FF000000) shr 24);
  64. end;
  65. constructor TIdHashSHA1.Create;
  66. begin
  67. inherited Create;
  68. SetLength(FCBuffer, 64);
  69. end;
  70. {$i IdOverflowCheckingOff.inc} // Operations performed modulo $100000000
  71. {$i IdRangeCheckingOff.inc}
  72. procedure TIdHashSHA1.Coder;
  73. var
  74. T, A, B, C, D, E: UInt32;
  75. { The size of the W variable has been reduced to make the Coder method
  76. consume less memory on dotNet. This change has been tested with the v1.1
  77. framework and entails a general increase of performance by >50%. }
  78. W: array [0..19] of UInt32;
  79. i: UInt32;
  80. begin
  81. { The first 16 W values are identical to the input block with endian
  82. conversion. }
  83. for i := 0 to 15 do
  84. begin
  85. W[i]:= (FCBuffer[i*4] shl 24) or
  86. (FCBuffer[i*4+1] shl 16) or
  87. (FCBuffer[i*4+2] shl 8) or
  88. FCBuffer[i*4+3];
  89. end;
  90. { In normal x86 code all of the remaining 64 W values would be calculated
  91. here. Here only the four next values are calculated, to reduce the code
  92. size of the first of the four loops below. }
  93. for i := 16 to 19 do
  94. begin
  95. T := W[i-3] xor W[i-8] xor W[i-14] xor W[i-16];
  96. W[i] := (T shl 1) or (T shr 31);
  97. end;
  98. A := FCheckSum[0];
  99. B := FCheckSum[1];
  100. C := FCheckSum[2];
  101. D := FCheckSum[3];
  102. E := FCheckSum[4];
  103. { The following loop could be expanded, but has been kept together to reduce
  104. the code size. A small code size entails better performance due to CPU
  105. caching.
  106. Note that the code size could be reduced further by using the SHA-1
  107. reference code:
  108. for i := 0 to 19 do begin
  109. T := E + (A shl 5) + (A shr 27) + (D xor (B and (C xor D))) + W[i];
  110. Inc(T,$5A827999);
  111. E := D;
  112. D := C;
  113. C := (B shl 30) + (B shr 2);
  114. B := A;
  115. A := T;
  116. end;
  117. The reference code is usually (at least partly) expanded, mostly because
  118. the assignments that circle the state variables A, B, C, D and E are costly,
  119. in particular on dotNET. (In x86 code further optimization can be achieved
  120. by eliminating the loop variable, which occupies a CPU register that is
  121. better used by one of the state variables, plus by expanding the W array
  122. at the beginning.) }
  123. i := 0;
  124. repeat
  125. Inc(E,(A shl 5) + (A shr 27) + (D xor (B and (C xor D))) + W[i+0]);
  126. Inc(E,$5A827999);
  127. B := (B shl 30) + (B shr 2);
  128. Inc(D,(E shl 5) + (E shr 27) + (C xor (A and (B xor C))) + W[i+1]);
  129. Inc(D,$5A827999);
  130. A := (A shl 30) + (A shr 2);
  131. Inc(C,(D shl 5) + (D shr 27) + (B xor (E and (A xor B))) + W[i+2]);
  132. Inc(C,$5A827999);
  133. E := (E shl 30) + (E shr 2);
  134. Inc(B,(C shl 5) + (C shr 27) + (A xor (D and (E xor A))) + W[i+3]);
  135. Inc(B,$5A827999);
  136. D := (D shl 30) + (D shr 2);
  137. Inc(A,(B shl 5) + (B shr 27) + (E xor (C and (D xor E))) + W[i+4]);
  138. Inc(A,$5A827999);
  139. C := (C shl 30) + (C shr 2);
  140. Inc(i,5);
  141. until i = 20;
  142. { The following three loops will only use the first 16 elements of the W
  143. array in a circular, recursive pattern. The following assignments are a
  144. trade-off to avoid having to split up the first loop. }
  145. W[0] := W[16];
  146. W[1] := W[17];
  147. W[2] := W[18];
  148. W[3] := W[19];
  149. { In the following three loops the recursive W array expansion is performed
  150. "just in time" following a circular pattern. Using circular indicies (e.g.
  151. (i+2) and $F) is not free, but the cost of declaring a large W array would
  152. be higher on dotNET. Before attempting to optimize this code, please note
  153. that the following language features are also costly:
  154. * Assignments and moves/copies, in particular on dotNET
  155. * Constant lookup tables, in particular on dotNET
  156. * Sub functions, in particular on x86
  157. * if..then and case..of. }
  158. i := 20;
  159. repeat
  160. T := W[(i+13) and $F] xor W[(i+8) and $F];
  161. T := T xor W[(i+2) and $F] xor W[i and $F];
  162. T := (T shl 1) or (T shr 31);
  163. W[i and $F] := T;
  164. Inc(E,(A shl 5) + (A shr 27) + (B xor C xor D) + T + $6ED9EBA1);
  165. B := (B shl 30) + (B shr 2);
  166. T := W[(i+14) and $F] xor W[(i+9) and $F];
  167. T := T xor W[(i+3) and $F] xor W[(i+1) and $F];
  168. T := (T shl 1) or (T shr 31);
  169. W[(i+1) and $F] := T;
  170. Inc(D,(E shl 5) + (E shr 27) + (A xor B xor C) + T + $6ED9EBA1);
  171. A := (A shl 30) + (A shr 2);
  172. T := W[(i+15) and $F] xor W[(i+10) and $F];
  173. T := T xor W[(i+4) and $F] xor W[(i+2) and $F];
  174. T := (T shl 1) or (T shr 31);
  175. W[(i+2) and $F] := T;
  176. Inc(C,(D shl 5) + (D shr 27) + (E xor A xor B) + T + $6ED9EBA1);
  177. E := (E shl 30) + (E shr 2);
  178. T := W[i and $F] xor W[(i+11) and $F];
  179. T := T xor W[(i+5) and $F] xor W[(i+3) and $F];
  180. T := (T shl 1) or (T shr 31);
  181. W[(i+3) and $F] := T;
  182. Inc(B,(C shl 5) + (C shr 27) + (D xor E xor A) + T + $6ED9EBA1);
  183. D := (D shl 30) + (D shr 2);
  184. T := W[(i+1) and $F] xor W[(i+12) and $F];
  185. T := T xor W[(i+6) and $F] xor W[(i+4) and $F];
  186. T := (T shl 1) or (T shr 31);
  187. W[(i+4) and $F] := T;
  188. Inc(A,(B shl 5) + (B shr 27) + (C xor D xor E) + T + $6ED9EBA1);
  189. C := (C shl 30) + (C shr 2);
  190. Inc(i,5);
  191. until i = 40;
  192. { Note that the constant $70E44324 = $100000000 - $8F1BBCDC has been selected
  193. to slightly reduce the probability that the CPU flag C (Carry) is set. This
  194. trick is taken from the StreamSec(R) StrSecII(TM) implementation of SHA-1.
  195. It entails a marginal but measurable performance gain on some CPUs. }
  196. i := 40;
  197. repeat
  198. T := W[(i+13) and $F] xor W[(i+8) and $F];
  199. T := T xor W[(i+2) and $F] xor W[i and $F];
  200. T := (T shl 1) or (T shr 31);
  201. W[i and $F] := T;
  202. Inc(E,(A shl 5) + (A shr 27) + ((B and C) or (D and (B or C))) + T);
  203. Dec(E,$70E44324);
  204. B := (B shl 30) + (B shr 2);
  205. T := W[(i+14) and $F] xor W[(i+9) and $F];
  206. T := T xor W[(i+3) and $F] xor W[(i+1) and $F];
  207. T := (T shl 1) or (T shr 31);
  208. W[(i+1) and $F] := T;
  209. Inc(D,(E shl 5) + (E shr 27) + ((A and B) or (C and (A or B))) + T);
  210. Dec(D,$70E44324);
  211. A := (A shl 30) + (A shr 2);
  212. T := W[(i+15) and $F] xor W[(i+10) and $F];
  213. T := T xor W[(i+4) and $F] xor W[(i+2) and $F];
  214. T := (T shl 1) or (T shr 31);
  215. W[(i+2) and $F] := T;
  216. Inc(C,(D shl 5) + (D shr 27) + ((E and A) or (B and (E or A))) + T);
  217. Dec(C,$70E44324);
  218. E := (E shl 30) + (E shr 2);
  219. T := W[i and $F] xor W[(i+11) and $F];
  220. T := T xor W[(i+5) and $F] xor W[(i+3) and $F];
  221. T := (T shl 1) or (T shr 31);
  222. W[(i+3) and $F] := T;
  223. Inc(B,(C shl 5) + (C shr 27) + ((D and E) or (A and (D or E))) + T);
  224. Dec(B,$70E44324);
  225. D := (D shl 30) + (D shr 2);
  226. T := W[(i+1) and $F] xor W[(i+12) and $F];
  227. T := T xor W[(i+6) and $F] xor W[(i+4) and $F];
  228. T := (T shl 1) or (T shr 31);
  229. W[(i+4) and $F] := T;
  230. Inc(A,(B shl 5) + (B shr 27) + ((C and D) or (E and (C or D))) + T);
  231. Dec(A,$70E44324);
  232. C := (C shl 30) + (C shr 2);
  233. Inc(i,5);
  234. until i = 60;
  235. { Note that the constant $359D3E2A = $100000000 - $CA62C1D6 has been selected
  236. to slightly reduce the probability that the CPU flag C (Carry) is set. This
  237. trick is taken from the StreamSec(R) StrSecII(TM) implementation of SHA-1.
  238. It entails a marginal but measurable performance gain on some CPUs. }
  239. repeat
  240. T := W[(i+13) and $F] xor W[(i+8) and $F];
  241. T := T xor W[(i+2) and $F] xor W[i and $F];
  242. T := (T shl 1) or (T shr 31);
  243. W[i and $F] := T;
  244. Inc(E,(A shl 5) + (A shr 27) + (B xor C xor D) + T - $359D3E2A);
  245. B := (B shl 30) + (B shr 2);
  246. T := W[(i+14) and $F] xor W[(i+9) and $F];
  247. T := T xor W[(i+3) and $F] xor W[(i+1) and $F];
  248. T := (T shl 1) or (T shr 31);
  249. W[(i+1) and $F] := T;
  250. Inc(D,(E shl 5) + (E shr 27) + (A xor B xor C) + T - $359D3E2A);
  251. A := (A shl 30) + (A shr 2);
  252. T := W[(i+15) and $F] xor W[(i+10) and $F];
  253. T := T xor W[(i+4) and $F] xor W[(i+2) and $F];
  254. T := (T shl 1) or (T shr 31);
  255. W[(i+2) and $F] := T;
  256. Inc(C,(D shl 5) + (D shr 27) + (E xor A xor B) + T - $359D3E2A);
  257. E := (E shl 30) + (E shr 2);
  258. T := W[i and $F] xor W[(i+11) and $F];
  259. T := T xor W[(i+5) and $F] xor W[(i+3) and $F];
  260. T := (T shl 1) or (T shr 31);
  261. W[(i+3) and $F] := T;
  262. Inc(B,(C shl 5) + (C shr 27) + (D xor E xor A) + T - $359D3E2A);
  263. D := (D shl 30) + (D shr 2);
  264. T := W[(i+1) and $F] xor W[(i+12) and $F];
  265. T := T xor W[(i+6) and $F] xor W[(i+4) and $F];
  266. T := (T shl 1) or (T shr 31);
  267. W[(i+4) and $F] := T;
  268. Inc(A,(B shl 5) + (B shr 27) + (C xor D xor E) + T - $359D3E2A);
  269. C := (C shl 30) + (C shr 2);
  270. Inc(i,5);
  271. until i = 80;
  272. FCheckSum[0]:= FCheckSum[0] + A;
  273. FCheckSum[1]:= FCheckSum[1] + B;
  274. FCheckSum[2]:= FCheckSum[2] + C;
  275. FCheckSum[3]:= FCheckSum[3] + D;
  276. FCheckSum[4]:= FCheckSum[4] + E;
  277. end;
  278. function TIdHashSHA1.GetHashBytes(AStream: TStream; ASize: TIdStreamSize): TIdBytes;
  279. var
  280. LSize: Integer;
  281. LLenHi: UInt32;
  282. LLenLo: UInt32;
  283. I: Integer;
  284. begin
  285. Result := nil;
  286. FCheckSum[0] := $67452301;
  287. FCheckSum[1] := $EFCDAB89;
  288. FCheckSum[2] := $98BADCFE;
  289. FCheckSum[3] := $10325476;
  290. FCheckSum[4] := $C3D2E1F0;
  291. LLenHi := 0;
  292. LLenLo := 0;
  293. // Code the entire file in complete 64-byte chunks.
  294. while ASize >= 64 do begin
  295. LSize := ReadTIdBytesFromStream(AStream, FCBuffer, 64);
  296. // TODO: handle stream read error
  297. Inc(LLenLo, LSize * 8);
  298. if LLenLo < UInt32(LSize * 8) then begin
  299. Inc(LLenHi);
  300. end;
  301. Coder;
  302. Dec(ASize, LSize);
  303. end;
  304. // Read the last set of bytes.
  305. LSize := ReadTIdBytesFromStream(AStream, FCBuffer, ASize);
  306. // TODO: handle stream read error
  307. Inc(LLenLo, LSize * 8);
  308. if LLenLo < UInt32(LSize * 8) then begin
  309. Inc(LLenHi);
  310. end;
  311. FCBuffer[LSize] := $80;
  312. if LSize >= 56 then begin
  313. for I := (LSize + 1) to 63 do begin
  314. FCBuffer[i] := 0;
  315. end;
  316. Coder;
  317. LSize := -1;
  318. end;
  319. for I := (LSize + 1) to 55 do begin
  320. FCBuffer[i] := 0;
  321. end;
  322. FCBuffer[56] := (LLenHi shr 24);
  323. FCBuffer[57] := (LLenHi shr 16) and $FF;
  324. FCBuffer[58] := (LLenHi shr 8) and $FF;
  325. FCBuffer[59] := (LLenHi and $FF);
  326. FCBuffer[60] := (LLenLo shr 24);
  327. FCBuffer[61] := (LLenLo shr 16) and $FF;
  328. FCBuffer[62] := (LLenLo shr 8) and $FF;
  329. FCBuffer[63] := (LLenLo and $FF);
  330. Coder;
  331. FCheckSum[0] := SwapLongWord(FCheckSum[0]);
  332. FCheckSum[1] := SwapLongWord(FCheckSum[1]);
  333. FCheckSum[2] := SwapLongWord(FCheckSum[2]);
  334. FCheckSum[3] := SwapLongWord(FCheckSum[3]);
  335. FCheckSum[4] := SwapLongWord(FCheckSum[4]);
  336. SetLength(Result, SizeOf(UInt32)*5);
  337. for I := 0 to 4 do begin
  338. CopyTIdUInt32(FCheckSum[I], Result, SizeOf(UInt32)*I);
  339. end;
  340. end;
  341. function TIdHashSHA1.HashToHex(const AHash: TIdBytes): String;
  342. begin
  343. Result := LongWordHashToHex(AHash, 5);
  344. end;
  345. end.