hedgewars/uSHA.pas
changeset 368 fe71e55d2d7b
parent 351 29bc9c36ad5f
child 1066 1f1b3686a2b0
equal deleted inserted replaced
367:bc3c3edc5ce1 368:fe71e55d2d7b
    23 type TSHA1Context = packed record
    23 type TSHA1Context = packed record
    24                     H: array[0..4] of LongWord;
    24                     H: array[0..4] of LongWord;
    25                     Length, CurrLength: Int64;
    25                     Length, CurrLength: Int64;
    26                     Buf: array[0..63] of byte;
    26                     Buf: array[0..63] of byte;
    27                     end;
    27                     end;
    28      TSHA1Digest =  array[0.. 4] of LongWord;
    28      TSHA1Digest =  array[0..4] of LongWord;
    29 
    29 
    30 procedure SHA1Init(var Context: TSHA1Context);
    30 procedure SHA1Init(var Context: TSHA1Context);
    31 procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
    31 procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
    32 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
    32 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
    33 
    33 
    99        H[4]:= $C3D2E1F0
    99        H[4]:= $C3D2E1F0
   100   end
   100   end
   101 end;
   101 end;
   102 
   102 
   103 procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
   103 procedure SHA1Update(var Context: TSHA1Context; Buf: PByteArray; Length: LongWord);
   104 var i: integer;
   104 var i: Longword;
   105 begin
   105 begin
   106 for i:= 0 to Pred(Length) do
   106 for i:= 0 to Pred(Length) do
   107     begin
   107     begin
   108     Context.Buf[Context.CurrLength]:= PByteArray(Buf)^[i];
   108     Context.Buf[Context.CurrLength]:= Buf^[i];
   109     inc(Context.CurrLength);
   109     inc(Context.CurrLength);
   110     if Context.CurrLength = 64 then
   110     if Context.CurrLength = 64 then
   111        begin
   111        begin
   112        SHA1Hash(Context);
   112        SHA1Hash(Context);
   113        inc(Context.Length, 512);
   113        inc(Context.Length, 512);
   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;
       
   122 begin
   121 begin
   123 Context.Length:= Context.Length + Context.CurrLength shl 3;
   122 Context.Length:= Context.Length + Context.CurrLength shl 3;
   124 Context.Buf[Context.CurrLength]:= $80;
   123 Context.Buf[Context.CurrLength]:= $80;
   125 inc(Context.CurrLength);
   124 inc(Context.CurrLength);
   126 
   125 
   135 FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
   134 FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
   136 
   135 
   137 for i:= 56 to 63 do
   136 for i:= 56 to 63 do
   138     Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
   137     Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
   139 SHA1Hash(Context);
   138 SHA1Hash(Context);
   140 move(Context.H, Result, sizeof(TSHA1Digest));
   139 for i:= 0 to 4 do SHA1Final[i]:= Context.H[i];
   141 FillChar(Context, sizeof(Context), 0);
   140 FillChar(Context, sizeof(Context), 0)
   142 SHA1Final:= Result
       
   143 end;
   141 end;
   144 
   142 
   145 end.
   143 end.