hedgewars/uSHA.pas
changeset 3369 c7289e42f0ee
parent 3236 4ab3917d7d44
child 3407 dcc129c4352e
equal deleted inserted replaced
3368:791fa4664209 3369:c7289e42f0ee
   120 end;
   120 end;
   121 
   121 
   122 procedure SHA1UpdateLongwords(var Context: TSHA1Context; Buf: PLongwordArray; Length: LongWord);
   122 procedure SHA1UpdateLongwords(var Context: TSHA1Context; Buf: PLongwordArray; Length: LongWord);
   123 var i: Longword;
   123 var i: Longword;
   124 begin
   124 begin
   125 for i:= 0 to Pred(Length div 4) do
   125     for i:= 0 to Pred(Length div 4) do
   126     begin
   126     begin
   127     SDLNet_Write32(Buf^[i], @Context.Buf[Context.CurrLength]);
   127         SDLNet_Write32(Buf^[i], @Context.Buf[Context.CurrLength]);
   128     inc(Context.CurrLength, 4);
   128         inc(Context.CurrLength, 4);
   129     if Context.CurrLength = 64 then
   129         if Context.CurrLength = 64 then
   130        begin
   130         begin
   131        SHA1Hash(Context);
   131             SHA1Hash(Context);
   132        inc(Context.Length, 512);
   132             inc(Context.Length, 512);
   133        Context.CurrLength:= 0
   133             Context.CurrLength:= 0
   134        end
   134         end
   135     end
   135     end
   136 end;
   136 end;
   137 
   137 
   138 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
   138 function  SHA1Final(Context: TSHA1Context): TSHA1Digest;
   139 var i: LongWord;
   139 var i: LongWord;
   140 begin
   140 begin
   141 Context.Length:= Context.Length + Context.CurrLength shl 3;
   141     Context.Length:= Context.Length + Context.CurrLength shl 3;
   142 Context.Buf[Context.CurrLength]:= $80;
   142     Context.Buf[Context.CurrLength]:= $80;
   143 inc(Context.CurrLength);
   143     inc(Context.CurrLength);
   144 
   144 
   145 if Context.CurrLength > 56 then
   145     if Context.CurrLength > 56 then
   146    begin
   146     begin
   147    FillChar(Context.Buf[Context.CurrLength], 64 - Context.CurrLength, 0);
   147         FillChar(Context.Buf[Context.CurrLength], 64 - Context.CurrLength, 0);
   148    Context.CurrLength:= 64;
   148         Context.CurrLength:= 64;
   149    SHA1Hash(Context);
   149         SHA1Hash(Context);
   150    Context.CurrLength:=0
   150         Context.CurrLength:=0
   151    end;
   151     end;
   152 
   152 
   153 FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
   153     FillChar(Context.Buf[Context.CurrLength], 56 - Context.CurrLength, 0);
   154 
   154 
   155 for i:= 56 to 63 do
   155     for i:= 56 to 63 do
   156     Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
   156         Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
   157 SHA1Hash(Context);
   157     SHA1Hash(Context);
   158 for i:= 0 to 4 do SHA1Final[i]:= Context.H[i];
   158     for i:= 0 to 4 do
   159 FillChar(Context, sizeof(Context), 0)
   159         SHA1Final[i]:= Context.H[i];
       
   160     
       
   161     FillChar(Context, sizeof(Context), 0)
   160 end;
   162 end;
   161 
   163 
   162 end.
   164 end.