316
|
1 |
(*
|
1066
|
2 |
* Hedgewars, a free turn based strategy game
|
316
|
3 |
* Copyright (c) 2004-2007 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*)
|
|
18 |
|
|
19 |
unit uSHA;
|
|
20 |
interface
|
|
21 |
uses SDLh;
|
|
22 |
|
|
23 |
type TSHA1Context = packed record
|
|
24 |
H: array[0..4] of LongWord;
|
|
25 |
Length, CurrLength: Int64;
|
|
26 |
Buf: array[0..63] of byte;
|
|
27 |
end;
|
368
|
28 |
TSHA1Digest = array[0..4] of LongWord;
|
316
|
29 |
|
|
30 |
procedure SHA1Init(var Context: TSHA1Context);
|
|
31 |
procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
|
2157
|
32 |
procedure SHA1UpdateLongwords(var Context: TSHA1Context; Buf: PLongwordArray; Length: LongWord);
|
316
|
33 |
function SHA1Final(Context: TSHA1Context): TSHA1Digest;
|
|
34 |
|
|
35 |
implementation
|
|
36 |
|
|
37 |
function rol(x: LongWord; y: Byte): LongWord;
|
|
38 |
begin
|
351
|
39 |
rol:= (X shl y) or (X shr (32 - y))
|
316
|
40 |
end;
|
|
41 |
|
|
42 |
function Ft(t, b, c, d: LongWord): LongWord;
|
|
43 |
begin
|
|
44 |
case t of
|
351
|
45 |
0..19: Ft := (b and c) or ((not b) and d);
|
|
46 |
20..39: Ft := b xor c xor d;
|
|
47 |
40..59: Ft := (b and c) or (b and d) or (c and d);
|
|
48 |
else Ft := b xor c xor d;
|
316
|
49 |
end;
|
|
50 |
end;
|
|
51 |
|
|
52 |
function Kt(t: Byte): LongWord;
|
|
53 |
begin
|
|
54 |
case t of
|
351
|
55 |
0..19: Kt := $5A827999;
|
|
56 |
20..39: Kt := $6ED9EBA1;
|
|
57 |
40..59: Kt := $8F1BBCDC;
|
316
|
58 |
else
|
351
|
59 |
Kt := $CA62C1D6
|
316
|
60 |
end;
|
|
61 |
end;
|
|
62 |
|
|
63 |
|
|
64 |
procedure SHA1Hash(var Context: TSHA1Context);
|
|
65 |
var S: array[0..4 ] of LongWord;
|
|
66 |
W: array[0..79] of LongWord;
|
|
67 |
i, t: LongWord;
|
|
68 |
begin
|
|
69 |
move(Context.H, S, sizeof(S));
|
|
70 |
for i:= 0 to 15 do
|
351
|
71 |
SDLNet_Write32(PLongWordArray(@Context.Buf)^[i], @W[i]);
|
|
72 |
|
316
|
73 |
for i := 16 to 79 do
|
|
74 |
W[i] := rol(W[i - 3] xor W[i - 8] xor W[i - 14] xor W[i - 16], 1);
|
351
|
75 |
|
316
|
76 |
for i := 0 to 79 do
|
|
77 |
begin
|
|
78 |
t:= rol(S[0], 5) + Ft(i, S[1], S[2], S[3]) + S[4] + W[i] + Kt(i);
|
|
79 |
S[4]:= S[3];
|
|
80 |
S[3]:= S[2];
|
|
81 |
S[2]:= rol(S[1], 30);
|
|
82 |
S[1]:= S[0];
|
|
83 |
S[0]:= t
|
|
84 |
end;
|
351
|
85 |
|
316
|
86 |
for i := 0 to 4 do
|
|
87 |
Context.H[i]:= Context.H[i] + S[i]
|
|
88 |
end;
|
|
89 |
|
|
90 |
procedure SHA1Init(var Context: TSHA1Context);
|
|
91 |
begin
|
|
92 |
with Context do
|
|
93 |
begin
|
|
94 |
Length := 0;
|
|
95 |
CurrLength:= 0;
|
|
96 |
H[0]:= $67452301;
|
|
97 |
H[1]:= $EFCDAB89;
|
|
98 |
H[2]:= $98BADCFE;
|
|
99 |
H[3]:= $10325476;
|
|
100 |
H[4]:= $C3D2E1F0
|
|
101 |
end
|
|
102 |
end;
|
|
103 |
|
|
104 |
procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
|
368
|
105 |
var i: Longword;
|
316
|
106 |
begin
|
|
107 |
for i:= 0 to Pred(Length) do
|
|
108 |
begin
|
368
|
109 |
Context.Buf[Context.CurrLength]:= Buf^[i];
|
316
|
110 |
inc(Context.CurrLength);
|
|
111 |
if Context.CurrLength = 64 then
|
|
112 |
begin
|
|
113 |
SHA1Hash(Context);
|
|
114 |
inc(Context.Length, 512);
|
|
115 |
Context.CurrLength:= 0
|
|
116 |
end
|
|
117 |
end
|
|
118 |
end;
|
|
119 |
|
2157
|
120 |
procedure SHA1UpdateLongwords(var Context: TSHA1Context; Buf: PLongwordArray; Length: LongWord);
|
|
121 |
var i: Longword;
|
|
122 |
begin
|
|
123 |
for i:= 0 to Pred(Length div 4) do
|
|
124 |
begin
|
|
125 |
SDLNet_Write32(Buf^[i], @Context.Buf[Context.CurrLength]);
|
|
126 |
inc(Context.CurrLength, 4);
|
|
127 |
if Context.CurrLength = 64 then
|
|
128 |
begin
|
|
129 |
SHA1Hash(Context);
|
|
130 |
inc(Context.Length, 512);
|
|
131 |
Context.CurrLength:= 0
|
|
132 |
end
|
|
133 |
end
|
|
134 |
end;
|
|
135 |
|
316
|
136 |
function SHA1Final(Context: TSHA1Context): TSHA1Digest;
|
|
137 |
var i: LongWord;
|
|
138 |
begin
|
|
139 |
Context.Length:= Context.Length + Context.CurrLength shl 3;
|
|
140 |
Context.Buf[Context.CurrLength]:= $80;
|
|
141 |
inc(Context.CurrLength);
|
|
142 |
|
|
143 |
if Context.CurrLength > 56 then
|
|
144 |
begin
|
|
145 |
FillChar(Context.Buf[Context.CurrLength], 64 - Context.CurrLength, 0);
|
|
146 |
Context.CurrLength:= 64;
|
|
147 |
SHA1Hash(Context);
|
|
148 |
Context.CurrLength:=0
|
|
149 |
end;
|
|
150 |
|
|
151 |
FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
|
|
152 |
|
|
153 |
for i:= 56 to 63 do
|
|
154 |
Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
|
|
155 |
SHA1Hash(Context);
|
368
|
156 |
for i:= 0 to 4 do SHA1Final[i]:= Context.H[i];
|
|
157 |
FillChar(Context, sizeof(Context), 0)
|
316
|
158 |
end;
|
|
159 |
|
|
160 |
end.
|