hedgewars/uSHA.pas
changeset 316 57d50189ad86
child 351 29bc9c36ad5f
equal deleted inserted replaced
315:73003488240b 316:57d50189ad86
       
     1 (*
       
     2  * Hedgewars, a worms-like game
       
     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;
       
    28      TSHA1Digest =  array[0.. 4] of LongWord;
       
    29 
       
    30 procedure SHA1Init(var Context: TSHA1Context);
       
    31 procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
       
    32 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
       
    33 
       
    34 implementation
       
    35 
       
    36 function rol(x: LongWord; y: Byte): LongWord;
       
    37 begin
       
    38   Result:= (X shl y) or (X shr (32 - y))
       
    39 end;
       
    40 
       
    41 function Ft(t, b, c, d: LongWord): LongWord;
       
    42 begin
       
    43 case t of
       
    44       0..19: Result := (b and c) or ((not b) and d);
       
    45      20..39: Result :=  b xor c xor d;
       
    46      40..59: Result := (b and c) or (b and d) or (c and d);
       
    47      else    Result :=  b xor c xor d;
       
    48   end;
       
    49 end;
       
    50 
       
    51 function Kt(t: Byte): LongWord;
       
    52 begin
       
    53   case t of
       
    54      0..19: Result := $5A827999;
       
    55     20..39: Result := $6ED9EBA1;
       
    56     40..59: Result := $8F1BBCDC;
       
    57   else
       
    58     Result := $CA62C1D6
       
    59   end;
       
    60 end;
       
    61 
       
    62 
       
    63 procedure SHA1Hash(var Context: TSHA1Context);
       
    64 var S: array[0..4 ] of LongWord;
       
    65     W: array[0..79] of LongWord;
       
    66     i, t: LongWord;
       
    67 begin
       
    68 move(Context.H, S, sizeof(S));
       
    69 for i:= 0 to 15 do
       
    70     SDLNet_Write32(PLongWordArray(@Context.Buf)[i], @W[i]);
       
    71     
       
    72 for i := 16 to 79 do
       
    73     W[i] := rol(W[i - 3] xor W[i - 8] xor W[i - 14] xor W[i - 16], 1);
       
    74     
       
    75 for i := 0 to 79 do
       
    76     begin
       
    77     t:= rol(S[0], 5) + Ft(i, S[1], S[2], S[3]) + S[4] + W[i] + Kt(i);
       
    78     S[4]:= S[3];
       
    79     S[3]:= S[2];
       
    80     S[2]:= rol(S[1], 30);
       
    81     S[1]:= S[0];
       
    82     S[0]:= t
       
    83     end;
       
    84     
       
    85 for i := 0 to 4 do
       
    86     Context.H[i]:= Context.H[i] + S[i]
       
    87 end;
       
    88 
       
    89 procedure SHA1Init(var Context: TSHA1Context);
       
    90 begin
       
    91   with Context do
       
    92        begin
       
    93        Length    := 0;
       
    94        CurrLength:= 0;
       
    95        H[0]:= $67452301;
       
    96        H[1]:= $EFCDAB89;
       
    97        H[2]:= $98BADCFE;
       
    98        H[3]:= $10325476;
       
    99        H[4]:= $C3D2E1F0
       
   100   end
       
   101 end;
       
   102 
       
   103 procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
       
   104 var i: integer;
       
   105 begin
       
   106 for i:= 0 to Pred(Length) do
       
   107     begin
       
   108     Context.Buf[Context.CurrLength]:= PByteArray(Buf)^[i];
       
   109     inc(Context.CurrLength);
       
   110     if Context.CurrLength = 64 then
       
   111        begin
       
   112        SHA1Hash(Context);
       
   113        inc(Context.Length, 512);
       
   114        Context.CurrLength:= 0
       
   115        end
       
   116     end
       
   117 end;
       
   118 
       
   119 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
       
   120 var i: LongWord;
       
   121 begin
       
   122 Context.Length:= Context.Length + Context.CurrLength shl 3;
       
   123 Context.Buf[Context.CurrLength]:= $80;
       
   124 inc(Context.CurrLength);
       
   125 
       
   126 if Context.CurrLength > 56 then
       
   127    begin
       
   128    FillChar(Context.Buf[Context.CurrLength], 64 - Context.CurrLength, 0);
       
   129    Context.CurrLength:= 64;
       
   130    SHA1Hash(Context);
       
   131    Context.CurrLength:=0
       
   132    end;
       
   133 
       
   134 FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
       
   135 
       
   136 for i:= 56 to 63 do
       
   137     Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
       
   138 SHA1Hash(Context);
       
   139 move(Context.H, Result, sizeof(TSHA1Digest));
       
   140 FillChar(Context, sizeof(Context), 0)
       
   141 end;
       
   142 
       
   143 end.