1 (* |
|
2 * Hedgewars, a worms-like game |
|
3 * Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 * |
|
5 * Distributed under the terms of the BSD-modified licence: |
|
6 * |
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 * of this software and associated documentation files (the "Software"), to deal |
|
9 * with the Software without restriction, including without limitation the |
|
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 * sell copies of the Software, and to permit persons to whom the Software is |
|
12 * furnished to do so, subject to the following conditions: |
|
13 * |
|
14 * 1. Redistributions of source code must retain the above copyright notice, |
|
15 * this list of conditions and the following disclaimer. |
|
16 * 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 * this list of conditions and the following disclaimer in the documentation |
|
18 * and/or other materials provided with the distribution. |
|
19 * 3. The name of the author may not be used to endorse or promote products |
|
20 * derived from this software without specific prior written permission. |
|
21 * |
|
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 *) |
|
33 |
|
34 unit uSHA; |
|
35 interface |
|
36 |
|
37 type TSHA1Context = packed record |
|
38 H: array[0..4] of LongWord; |
|
39 Length, CurrLength: Int64; |
|
40 Buf: array[0..63] of byte; |
|
41 end; |
|
42 TSHA1Digest = record |
|
43 case byte of |
|
44 0: (LongWords: array[0.. 4] of LongWord); |
|
45 1: ( Words: array[0.. 9] of Word); |
|
46 2: ( Bytes: array[0..19] of Byte) |
|
47 end; |
|
48 |
|
49 procedure SHA1Init(var Context: TSHA1Context); |
|
50 procedure SHA1Update(var Context: TSHA1Context; Buf: Pointer; Length: LongWord); |
|
51 function SHA1Final(Context: TSHA1Context): TSHA1Digest; |
|
52 |
|
53 implementation |
|
54 uses SDLh; |
|
55 |
|
56 function rol(x: LongWord; y: Byte): LongWord; |
|
57 begin |
|
58 Result:= (X shl y) or (X shr (32 - y)) |
|
59 end; |
|
60 |
|
61 function Ft(t, b, c, d: LongWord): LongWord; |
|
62 begin |
|
63 case t of |
|
64 0..19: Result := (b and c) or ((not b) and d); |
|
65 20..39: Result := b xor c xor d; |
|
66 40..59: Result := (b and c) or (b and d) or (c and d); |
|
67 else Result := b xor c xor d; |
|
68 end; |
|
69 end; |
|
70 |
|
71 function Kt(t: Byte): LongWord; |
|
72 begin |
|
73 case t of |
|
74 0..19: Result := $5A827999; |
|
75 20..39: Result := $6ED9EBA1; |
|
76 40..59: Result := $8F1BBCDC; |
|
77 else |
|
78 Result := $CA62C1D6 |
|
79 end; |
|
80 end; |
|
81 |
|
82 |
|
83 procedure SHA1Hash(var Context: TSHA1Context); |
|
84 var S: array[0..4 ] of LongWord; |
|
85 W: array[0..79] of LongWord; |
|
86 i, t: LongWord; |
|
87 begin |
|
88 move(Context.H, S, sizeof(S)); |
|
89 for i:= 0 to 3 do |
|
90 begin |
|
91 t:= i * 4; |
|
92 with Context do |
|
93 W[i]:= Buf[t + 3] or (Buf[t + 2] shl 8) or (Buf[t + 1] shl 16) or (Buf[t] shl 24); |
|
94 end; |
|
95 for i := 16 to 79 do |
|
96 W[i] := rol(W[i - 3] xor W[i - 8] xor W[i - 14] xor W[i - 16], 1); |
|
97 for i := 0 to 79 do |
|
98 begin |
|
99 t:= rol(S[0], 5) + Ft(i, S[1], S[2], S[3]) + S[4] + W[i] + Kt(i); |
|
100 S[4]:= S[3]; |
|
101 S[3]:= S[2]; |
|
102 S[2]:= rol(S[1], 30); |
|
103 S[1]:= S[0]; |
|
104 S[0]:= t |
|
105 end; |
|
106 for i := 0 to 4 do |
|
107 Context.H[i]:= Context.H[i] + S[i] |
|
108 end; |
|
109 |
|
110 procedure SHA1Init(var Context: TSHA1Context); |
|
111 begin |
|
112 with Context do |
|
113 begin |
|
114 Length := 0; |
|
115 CurrLength:= 0; |
|
116 H[0]:= $67452301; |
|
117 H[1]:= $EFCDAB89; |
|
118 H[2]:= $98BADCFE; |
|
119 H[3]:= $10325476; |
|
120 H[4]:= $C3D2E1F0 |
|
121 end |
|
122 end; |
|
123 |
|
124 procedure SHA1Update(var Context: TSHA1Context; Buf: Pointer; Length: LongWord); |
|
125 var i: integer; |
|
126 begin |
|
127 for i:= 0 to Pred(Length) do |
|
128 begin |
|
129 Context.Buf[Context.CurrLength]:= PByteArray(Buf)^[i]; |
|
130 inc(Context.CurrLength); |
|
131 if Context.CurrLength=64 then |
|
132 begin |
|
133 SHA1Hash(Context); |
|
134 inc(Context.Length, 512); |
|
135 Context.CurrLength:=0 |
|
136 end |
|
137 end |
|
138 end; |
|
139 |
|
140 function SHA1Final(Context: TSHA1Context): TSHA1Digest; |
|
141 var i: LongWord; |
|
142 begin |
|
143 Context.Length:= Context.Length + Context.CurrLength shl 3; |
|
144 Context.Buf[Context.CurrLength]:= $80; |
|
145 inc(Context.CurrLength); |
|
146 if Context.CurrLength>56 then |
|
147 begin |
|
148 FillChar(Context.Buf[Context.CurrLength],64-Context.CurrLength,0); |
|
149 Context.CurrLength:= 64; |
|
150 SHA1Hash(Context); |
|
151 Context.CurrLength:=0 |
|
152 end; |
|
153 FillChar(Context.Buf[Context.CurrLength],56-Context.CurrLength,0); |
|
154 for i:= 56 to 63 do |
|
155 Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF; |
|
156 SHA1Hash(Context); |
|
157 move(Context.H, Result, sizeof(TSHA1Digest)); |
|
158 FillChar(Context, sizeof(Context), 0) |
|
159 end; |
|
160 |
|
161 end. |
|