hedgewars/uSHA.pas
changeset 351 29bc9c36ad5f
parent 316 57d50189ad86
child 368 fe71e55d2d7b
equal deleted inserted replaced
350:c3ccec3834e8 351:29bc9c36ad5f
    33 
    33 
    34 implementation
    34 implementation
    35 
    35 
    36 function rol(x: LongWord; y: Byte): LongWord;
    36 function rol(x: LongWord; y: Byte): LongWord;
    37 begin
    37 begin
    38   Result:= (X shl y) or (X shr (32 - y))
    38   rol:= (X shl y) or (X shr (32 - y))
    39 end;
    39 end;
    40 
    40 
    41 function Ft(t, b, c, d: LongWord): LongWord;
    41 function Ft(t, b, c, d: LongWord): LongWord;
    42 begin
    42 begin
    43 case t of
    43 case t of
    44       0..19: Result := (b and c) or ((not b) and d);
    44       0..19: Ft := (b and c) or ((not b) and d);
    45      20..39: Result :=  b xor c xor d;
    45      20..39: Ft :=  b xor c xor d;
    46      40..59: Result := (b and c) or (b and d) or (c and d);
    46      40..59: Ft := (b and c) or (b and d) or (c and d);
    47      else    Result :=  b xor c xor d;
    47      else    Ft :=  b xor c xor d;
    48   end;
    48   end;
    49 end;
    49 end;
    50 
    50 
    51 function Kt(t: Byte): LongWord;
    51 function Kt(t: Byte): LongWord;
    52 begin
    52 begin
    53   case t of
    53   case t of
    54      0..19: Result := $5A827999;
    54      0..19: Kt := $5A827999;
    55     20..39: Result := $6ED9EBA1;
    55     20..39: Kt := $6ED9EBA1;
    56     40..59: Result := $8F1BBCDC;
    56     40..59: Kt := $8F1BBCDC;
    57   else
    57   else
    58     Result := $CA62C1D6
    58     Kt := $CA62C1D6
    59   end;
    59   end;
    60 end;
    60 end;
    61 
    61 
    62 
    62 
    63 procedure SHA1Hash(var Context: TSHA1Context);
    63 procedure SHA1Hash(var Context: TSHA1Context);
    65     W: array[0..79] of LongWord;
    65     W: array[0..79] of LongWord;
    66     i, t: LongWord;
    66     i, t: LongWord;
    67 begin
    67 begin
    68 move(Context.H, S, sizeof(S));
    68 move(Context.H, S, sizeof(S));
    69 for i:= 0 to 15 do
    69 for i:= 0 to 15 do
    70     SDLNet_Write32(PLongWordArray(@Context.Buf)[i], @W[i]);
    70     SDLNet_Write32(PLongWordArray(@Context.Buf)^[i], @W[i]);
    71     
    71 
    72 for i := 16 to 79 do
    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);
    73     W[i] := rol(W[i - 3] xor W[i - 8] xor W[i - 14] xor W[i - 16], 1);
    74     
    74 
    75 for i := 0 to 79 do
    75 for i := 0 to 79 do
    76     begin
    76     begin
    77     t:= rol(S[0], 5) + Ft(i, S[1], S[2], S[3]) + S[4] + W[i] + Kt(i);
    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];
    78     S[4]:= S[3];
    79     S[3]:= S[2];
    79     S[3]:= S[2];
    80     S[2]:= rol(S[1], 30);
    80     S[2]:= rol(S[1], 30);
    81     S[1]:= S[0];
    81     S[1]:= S[0];
    82     S[0]:= t
    82     S[0]:= t
    83     end;
    83     end;
    84     
    84 
    85 for i := 0 to 4 do
    85 for i := 0 to 4 do
    86     Context.H[i]:= Context.H[i] + S[i]
    86     Context.H[i]:= Context.H[i] + S[i]
    87 end;
    87 end;
    88 
    88 
    89 procedure SHA1Init(var Context: TSHA1Context);
    89 procedure SHA1Init(var Context: TSHA1Context);
   116     end
   116     end
   117 end;
   117 end;
   118 
   118 
   119 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
   119 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
   120 var i: LongWord;
   120 var i: LongWord;
       
   121     Result: TSHA1Digest;
   121 begin
   122 begin
   122 Context.Length:= Context.Length + Context.CurrLength shl 3;
   123 Context.Length:= Context.Length + Context.CurrLength shl 3;
   123 Context.Buf[Context.CurrLength]:= $80;
   124 Context.Buf[Context.CurrLength]:= $80;
   124 inc(Context.CurrLength);
   125 inc(Context.CurrLength);
   125 
   126 
   135 
   136 
   136 for i:= 56 to 63 do
   137 for i:= 56 to 63 do
   137     Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
   138     Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
   138 SHA1Hash(Context);
   139 SHA1Hash(Context);
   139 move(Context.H, Result, sizeof(TSHA1Digest));
   140 move(Context.H, Result, sizeof(TSHA1Digest));
   140 FillChar(Context, sizeof(Context), 0)
   141 FillChar(Context, sizeof(Context), 0);
       
   142 SHA1Final:= Result
   141 end;
   143 end;
   142 
   144 
   143 end.
   145 end.