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