4
|
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 |
|
100
|
55 |
function _bswap(X: LongWord): LongWord;
|
|
56 |
begin
|
|
57 |
Result:= (X shr 24) or ((X shr 8) and $FF00) or ((X shl 8) and $FF0000) or (X shl 24)
|
4
|
58 |
end;
|
|
59 |
|
100
|
60 |
function rol(x: LongWord; y: Byte): LongWord;
|
|
61 |
begin
|
|
62 |
Result:= (X shl y) or (X shr (32 - y))
|
4
|
63 |
end;
|
|
64 |
|
|
65 |
function Ft(t, b, c, d: LongWord): LongWord;
|
|
66 |
begin
|
|
67 |
case t of
|
|
68 |
0..19: Result := (b and c) or ((not b) and d);
|
|
69 |
20..39: Result := b xor c xor d;
|
|
70 |
40..59: Result := (b and c) or (b and d) or (c and d);
|
|
71 |
else Result := b xor c xor d;
|
|
72 |
end;
|
|
73 |
end;
|
|
74 |
|
|
75 |
function Kt(t: Byte): LongWord;
|
|
76 |
begin
|
|
77 |
case t of
|
|
78 |
0..19: Result := $5A827999;
|
|
79 |
20..39: Result := $6ED9EBA1;
|
|
80 |
40..59: Result := $8F1BBCDC;
|
|
81 |
else
|
|
82 |
Result := $CA62C1D6
|
|
83 |
end;
|
|
84 |
end;
|
|
85 |
|
|
86 |
|
|
87 |
procedure SHA1Hash(var Context: TSHA1Context);
|
|
88 |
var S: array[0..4 ] of LongWord;
|
|
89 |
W: array[0..79] of LongWord;
|
|
90 |
i, t: LongWord;
|
|
91 |
begin
|
|
92 |
move(Context.H, S, sizeof(S));
|
|
93 |
for i:= 0 to 15 do
|
|
94 |
W[i]:= _bswap(PLongWord(LongWord(@Context.Buf)+i*4)^);
|
|
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:= 1 to Length do
|
|
128 |
begin
|
|
129 |
Context.Buf[Context.CurrLength]:= PByte(Buf)^;
|
|
130 |
inc(Context.CurrLength);
|
|
131 |
inc(LongWord(Buf));
|
|
132 |
if Context.CurrLength=64 then
|
|
133 |
begin
|
|
134 |
SHA1Hash(Context);
|
|
135 |
inc(Context.Length, 512);
|
|
136 |
Context.CurrLength:=0
|
|
137 |
end
|
|
138 |
end
|
|
139 |
end;
|
|
140 |
|
|
141 |
function SHA1Final(Context: TSHA1Context): TSHA1Digest;
|
|
142 |
var i: LongWord;
|
|
143 |
begin
|
|
144 |
Context.Length:= Context.Length + Context.CurrLength shl 3;
|
|
145 |
Context.Buf[Context.CurrLength]:= $80;
|
|
146 |
inc(Context.CurrLength);
|
|
147 |
if Context.CurrLength>56 then
|
|
148 |
begin
|
|
149 |
FillChar(Context.Buf[Context.CurrLength],64-Context.CurrLength,0);
|
|
150 |
Context.CurrLength:= 64;
|
|
151 |
SHA1Hash(Context);
|
|
152 |
Context.CurrLength:=0
|
|
153 |
end;
|
|
154 |
FillChar(Context.Buf[Context.CurrLength],56-Context.CurrLength,0);
|
|
155 |
for i:= 56 to 63 do
|
|
156 |
Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
|
|
157 |
SHA1Hash(Context);
|
|
158 |
move(Context.H, Result, sizeof(TSHA1Digest));
|
|
159 |
FillChar(Context, sizeof(Context), 0)
|
|
160 |
end;
|
|
161 |
|
|
162 |
end.
|