⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 haval.pas

📁 超级Delphi函数包,包括编程时常需要的一些函数
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  FF_3(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +30 * 4)^, $55605C60, PASS);
  FF_3(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +16 * 4)^, $E65525F3, PASS);
  FF_3(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +26 * 4)^, $AA55AB94, PASS);

  FF_3(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +31 * 4)^, $57489862, PASS);
  FF_3(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +15 * 4)^, $63E81440, PASS);
  FF_3(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) + 7 * 4)^, $55CA396A, PASS);
  FF_3(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) + 3 * 4)^, $2AAB10B6, PASS);
  FF_3(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 1 * 4)^, $B4CC5C34, PASS);
  FF_3(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(w)^, $1141E8CE, PASS);
  FF_3(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +18 * 4)^, $A15486AF, PASS);
  FF_3(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +27 * 4)^, $7C72E993, PASS);

  FF_3(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +13 * 4)^, $B3EE1411, PASS);
  FF_3(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 6 * 4)^, $636FBC2A, PASS);
  FF_3(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +21 * 4)^, $2BA9C55D, PASS);
  FF_3(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +10 * 4)^, $741831F6, PASS);
  FF_3(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +23 * 4)^, $CE5C3E16, PASS);
  FF_3(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +11 * 4)^, $9B87931E, PASS);
  FF_3(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) + 5 * 4)^, $AFD6BA33, PASS);
  FF_3(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) + 2 * 4)^, $6C24CF5C, PASS);

  // Pass 4. executed only when PASS = 4 or 5
  if PASS > 3 then
  begin
    FF_4(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +24 * 4)^, $7A325381, PASS);
    FF_4(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 4 * 4)^, $28958677, PASS);
    FF_4(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(w)^, $3B8F4898, PASS);
    FF_4(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +14 * 4)^, $6B4BB9AF, PASS);
    FF_4(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 2 * 4)^, $C4BFE81B, PASS);
    FF_4(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) + 7 * 4)^, $66282193, PASS);
    FF_4(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +28 * 4)^, $61D809CC, PASS);
    FF_4(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +23 * 4)^, $FB21A991, PASS);

    FF_4(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +26 * 4)^, $487CAC60, PASS);
    FF_4(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 6 * 4)^, $5DEC8032, PASS);
    FF_4(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +30 * 4)^, $EF845D5D, PASS);
    FF_4(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +20 * 4)^, $E98575B1, PASS);
    FF_4(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +18 * 4)^, $DC262302, PASS);
    FF_4(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +25 * 4)^, $EB651B88, PASS);
    FF_4(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +19 * 4)^, $23893E81, PASS);
    FF_4(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) + 3 * 4)^, $D396ACC5, PASS);

    FF_4(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +22 * 4)^, $0F6D6FF3, PASS);
    FF_4(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +11 * 4)^, $83F44239, PASS);
    FF_4(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +31 * 4)^, $2E0B4482, PASS);
    FF_4(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +21 * 4)^, $A4842004, PASS);
    FF_4(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 8 * 4)^, $69C8F04A, PASS);
    FF_4(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +27 * 4)^, $9E1F9B5E, PASS);
    FF_4(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +12 * 4)^, $21C66842, PASS);
    FF_4(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) + 9 * 4)^, $F6E96C9A, PASS);

    FF_4(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) + 1 * 4)^, $670C9C61, PASS);
    FF_4(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +29 * 4)^, $ABD388F0, PASS);
    FF_4(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) + 5 * 4)^, $6A51A0D2, PASS);
    FF_4(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +15 * 4)^, $D8542F68, PASS);
    FF_4(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +17 * 4)^, $960FA728, PASS);
    FF_4(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +10 * 4)^, $AB5133A3, PASS);
    FF_4(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +16 * 4)^, $6EEF0B6C, PASS);
    FF_4(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +13 * 4)^, $137A3BE4, PASS);
  end;

  // Pass 5. executed only when PASS = 5
  if PASS = 5 then
  begin
    FF_5(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +27 * 4)^, $BA3BF050);
    FF_5(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 3 * 4)^, $7EFB2A98);
    FF_5(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +21 * 4)^, $A1F1651D);
    FF_5(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +26 * 4)^, $39AF0176);
    FF_5(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +17 * 4)^, $66CA593E);
    FF_5(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +11 * 4)^, $82430E88);
    FF_5(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +20 * 4)^, $8CEE8619);
    FF_5(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +29 * 4)^, $456F9FB4);

    FF_5(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +19 * 4)^, $7D84A5C3);
    FF_5(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(w)^, $3B8B5EBE);
    FF_5(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +12 * 4)^, $E06F75D8);
    FF_5(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) + 7 * 4)^, $85C12073);
    FF_5(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +13 * 4)^, $401A449F);
    FF_5(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) + 8 * 4)^, $56C16AA6);
    FF_5(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +31 * 4)^, $4ED3AA62);
    FF_5(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +10 * 4)^, $363F7706);

    FF_5(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) + 5 * 4)^, $1BFEDF72);
    FF_5(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 9 * 4)^, $429B023D);
    FF_5(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +14 * 4)^, $37D0D724);
    FF_5(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +30 * 4)^, $D00A1248);
    FF_5(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +18 * 4)^, $DB0FEAD3);
    FF_5(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) + 6 * 4)^, $49F1C09B);
    FF_5(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +28 * 4)^, $075372C9);
    FF_5(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +24 * 4)^, $80991B7B);

    FF_5(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) + 2 * 4)^, $25D479D8);
    FF_5(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +23 * 4)^, $F6E8DEF7);
    FF_5(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +16 * 4)^, $E3FE501A);
    FF_5(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +22 * 4)^, $B6794C3B);
    FF_5(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 4 * 4)^, $976CE0BD);
    FF_5(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) + 1 * 4)^, $04C006BA);
    FF_5(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +25 * 4)^, $C1A94FB6);
    FF_5(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +15 * 4)^, $409F60C4);
  end;

  Inc(state.fingerprint[0], t0);
  Inc(state.fingerprint[1], t1);
  Inc(state.fingerprint[2], t2);
  Inc(state.fingerprint[3], t3);
  Inc(state.fingerprint[4], t4);
  Inc(state.fingerprint[5], t5);
  Inc(state.fingerprint[6], t6);
  Inc(state.fingerprint[7], t7);
end;

procedure HavalInit(var state: THavalCtx);
begin
  state.count[0] := 0;
  state.count[1] := 0;
  state.fingerprint[0] := $243F6A88;
  state.fingerprint[1] := $85A308D3;
  state.fingerprint[2] := $13198A2E;
  state.fingerprint[3] := $03707344;
  state.fingerprint[4] := $A4093822;
  state.fingerprint[5] := $299F31D0;
  state.fingerprint[6] := $082EFA98;
  state.fingerprint[7] := $EC4E6C89;
end;


procedure HavalUpdate(var state: THavalCtx; str: Pointer; str_len, PASS: LongWord);
var
  i, rmd_len, fill_len: LongWord;
begin
  if PASS < 3 then PASS := 3;
  if PASS > 5 then PASS := 5;

  rmd_len  := (state.count[0] shr 3) and $7F;
  fill_len := 128 - rmd_len;

  state.count[0] := state.count[0] + (str_len shl 3);
  if state.count[0] < (str_len shl 3) then
    Inc(state.count[1]);

  state.count[1] := state.count[1] + (str_len shr 29);
  if (rmd_len + str_len >= 128) then
  begin
    Move(str^, Ptr(LongWord(@state.block) + rmd_len)^, fill_len);
    haval_hash_block (state, PASS);
    i := fill_len;
    while i + 127 < str_len do
    begin
      Move(Ptr(LongWord(str) + i)^, state.block, 128);
      haval_hash_block(state, PASS);
      Inc(i, 128);
    end;
    rmd_len := 0;
  end
  else
    i := 0;
  Move(Ptr(LongWord(str) + i)^, Ptr(LongWord(@state.block) + rmd_len)^, str_len - i);
end;

procedure haval_tailor(var state: THavalCtx; FPTLEN: LongWord);
var
  temp: LongWord;
begin
  if FPTLEN = 128 then
  begin
    temp := (state.fingerprint[7] and $000000FF) or
            (state.fingerprint[6] and $FF000000) or
            (state.fingerprint[5] and $00FF0000) or
            (state.fingerprint[4] and $0000FF00);
    state.fingerprint[0] := state.fingerprint[0] + ror(temp,  8);

    temp := (state.fingerprint[7] and $0000FF00) or
            (state.fingerprint[6] and $000000FF) or
            (state.fingerprint[5] and $FF000000) or
            (state.fingerprint[4] and $00FF0000);
    state.fingerprint[1] := state.fingerprint[1] + ror(temp, 16);

    temp := (state.fingerprint[7] and $00FF0000) or
            (state.fingerprint[6] and $0000FF00) or
            (state.fingerprint[5] and $000000FF) or
            (state.fingerprint[4] and $FF000000);
    state.fingerprint[2] := state.fingerprint[2] + ror(temp, 24);

    temp := (state.fingerprint[7] and $FF000000) or
            (state.fingerprint[6] and $00FF0000) or
            (state.fingerprint[5] and $0000FF00) or
            (state.fingerprint[4] and $000000FF);
    state.fingerprint[3] := state.fingerprint[3] + temp;
  end
  else if FPTLEN = 160 then
  begin
    temp := (state.fingerprint[7] and  $3F) or
            (state.fingerprint[6] and ($7F shl 25)) or
            (state.fingerprint[5] and ($3F shl 19));
    state.fingerprint[0] := state.fingerprint[0] + ror(temp, 19);
    temp := (state.fingerprint[7] and ($3F shl  6)) or
            (state.fingerprint[6] and  $3F) or
            (state.fingerprint[5] and ($7F shl 25));
    state.fingerprint[1] := state.fingerprint[1] + ror(temp, 25);
    temp := (state.fingerprint[7] and ($7F shl 12)) or
            (state.fingerprint[6] and ($3F shl  6)) or
            (state.fingerprint[5] and  $3F);
    state.fingerprint[2] := state.fingerprint[2] + temp;
    temp := (state.fingerprint[7] and ($3F shl 19)) or
            (state.fingerprint[6] and ($7F shl 12)) or
            (state.fingerprint[5] and ($3F shl  6));
    state.fingerprint[3] := state.fingerprint[3] + (temp shr 6);
    temp := (state.fingerprint[7] and ($7F shl 25)) or
            (state.fingerprint[6] and ($3F shl 19)) or
            (state.fingerprint[5] and ($7F shl 12));
    state.fingerprint[4] := state.fingerprint[4] + (temp shr 12);
  end
  else if FPTLEN = 192 then
  begin
    temp := (state.fingerprint[7] and  $1F) or
            (state.fingerprint[6] and ($3F shl 26));
    state.fingerprint[0] := state.fingerprint[0] + ror(temp, 26);
    temp := (state.fingerprint[7] and ($1F shl  5)) or
            (state.fingerprint[6] and  $1F);
    state.fingerprint[1] := state.fingerprint[1] + temp;
    temp := (state.fingerprint[7] and ($3F shl 10)) or
           (state.fingerprint[6] and ($1F shl  5));
    state.fingerprint[2] := state.fingerprint[2] + (temp shr 5);
    temp := (state.fingerprint[7] and ($1F shl 16)) or
            (state.fingerprint[6] and ($3F shl 10));
    state.fingerprint[3] := state.fingerprint[3] + (temp shr 10);
    temp := (state.fingerprint[7] and ($1F shl 21)) or
            (state.fingerprint[6] and ($1F shl 16));
    state.fingerprint[4] := state.fingerprint[4] + (temp shr 16);
    temp := (state.fingerprint[7] and ($3F shl 26)) or
            (state.fingerprint[6] and ($1F shl 21));
    state.fingerprint[5] := state.fingerprint[5] + (temp shr 21);
  end
  else if FPTLEN = 224 then
  begin
    Inc(state.fingerprint[0], ((state.fingerprint[7] shr 27) and $1F));
    Inc(state.fingerprint[1], ((state.fingerprint[7] shr 22) and $1F));
    Inc(state.fingerprint[2], ((state.fingerprint[7] shr 18) and $0F));
    Inc(state.fingerprint[3], ((state.fingerprint[7] shr 13) and $1F));
    Inc(state.fingerprint[4], ((state.fingerprint[7] shr  9) and $0F));
    Inc(state.fingerprint[5], ((state.fingerprint[7] shr  4) and $1F));
    Inc(state.fingerprint[6], (state.fingerprint[7] and $0F));
  end;
end;

procedure uint2ch(_word, _string: Pointer; wlen: LongWord);
var
  wp: PLongWord;
  sp: PByte;
begin
  wp := _word;
  sp := _string;
  while (LongWord(wp) < LongWord(_word) + wlen * 4 {!}) do
  begin
    sp^ := Byte((wp^)        and $FF); Inc(sp);
    sp^ := Byte((wp^ shr  8) and $FF); Inc(sp);
    sp^ := Byte((wp^ shr 16) and $FF); Inc(sp);
    sp^ := Byte((wp^ shr 24) and $FF); Inc(sp);
    Inc(wp);
  end;
end;

procedure haval_end(var state: THavalCtx; var final_fpt: array of Byte; PASS: LongWord; FPTLEN: LongWord);
var
  tail: array[0..9] of Byte;
  rmd_len, pad_len: LongWord;
begin
  tail[0] := Byte(((FPTLEN and $3) shl 6) or
    ((PASS and $7) shl 3) or
    (VERSION and $7));
  tail[1] := Byte((FPTLEN shr 2) and $FF);

  uint2ch(@state.count, @tail[2], 2);

  // pad out to 118 mod 128
  rmd_len := Byte((state.count[0] shr 3) and $7f);
  if rmd_len < 118 then
    pad_len := 118 - rmd_len
  else
    pad_len := 246 - rmd_len;
  HavalUpdate(state, @padding, pad_len, PASS);

  HavalUpdate(state, @tail, 10, PASS);

  haval_tailor(state, FPTLEN);

  //uint2ch(@state.fingerprint, @final_fpt, FPTLEN shr 5);
  Move(state.fingerprint, final_fpt, FPTLEN shr 3);

  FillChar(state, SizeOf(State), 0);
end;

function HavalFinal(var state: THavalCtx; PASS, HashLen: LongWord): String;
var
  fingerprint: array[0..256 shr 3 - 1] of Byte;
  i: Byte;
begin
  if PASS < 3 then PASS := 3;
  if PASS > 5 then PASS := 5;
  haval_end(state, fingerprint, PASS, HashLen);  
  Result := '';
  for i := 0 to HashLen shr 3 - 1 do
    Result := Result + IntToHex(fingerprint[i], 2);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -