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

📄 haval.pas

📁 超级Delphi函数包,包括编程时常需要的一些函数
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit HAVAL;

interface
uses
  CryptoUtils;

type
  PHavalCtx = ^THavalCtx;
  THavalCtx = record
    count: array[0..1] of LongWord;                 // number of bits in a message
    fingerprint: array[0..7] of LongWord;           // current state of fingerprint
    block: array[0..31] of LongWord;                // buffer for a 32-word block
  end;


  procedure HavalInit(var state: THavalCtx);
  procedure HavalUpdate(var state: THavalCtx; str: Pointer; str_len, PASS: LongWord);
  function HavalFinal(var state: THavalCtx; PASS, HashLen: LongWord): String;


implementation
const
  VERSION = 1;                                      //Haval version, could be changed if necessary

  padding: array[0..127] of Byte = (
    $80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  );

function f_1(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := ((x1) and ((x0) xor (x4)) xor (x2) and (x5) xor
    (x3) and (x6) xor (x0));
end;

function f_2(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := ((x2) and ((x1) and (not x3) xor (x4) and (x5) xor (x6) xor (x0)) xor
    (x4) and ((x1) xor (x5)) xor (x3) and (x5) xor (x0));
end;

function f_3(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := ((x3) and ((x1) and (x2) xor (x6) xor (x0)) xor
    (x1) and (x4) xor (x2) and (x5) xor (x0));
end;

function f_4(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := ((x4) and ((x5) and (not x2) xor (x3) and (not x6) xor (x1) xor (x6) xor (x0)) xor
    (x3) and ((x1) and (x2) xor (x5) xor (x6)) xor
    (x2) and (x6) xor (x0));
end;

function f_5(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := ((x0) and ((x1) and (x2) and (x3) xor (not x5)) xor
    (x1) and (x4) xor (x2) and (x5) xor (x3) and (x6));
end;

function Fphi_1p3(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_1(x1, x0, x3, x5, x6, x2, x4);
end;

function Fphi_1p4(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_1(x2, x6, x1, x4, x5, x3, x0);
end;

function Fphi_1neut(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_1(x3, x4, x1, x0, x5, x2, x6);
end;

function Fphi_1(x6, x5, x4, x3, x2, x1, x0, PASS: LongWord): LongWord;
begin
  if PASS = 3 then
    Result := Fphi_1p3(x6, x5, x4, x3, x2, x1, x0)
  else if PASS = 4 then
    Result := Fphi_1p4(x6, x5, x4, x3, x2, x1, x0)
  else
    Result := Fphi_1neut(x6, x5, x4, x3, x2, x1, x0);
end;

function Fphi_2p3(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_2(x4, x2, x1, x0, x5, x3, x6);
end;

function Fphi_2p4(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_2(x3, x5, x2, x0, x1, x6, x4)
end;

function Fphi_2neut(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_2(x6, x2, x1, x0, x3, x4, x5);
end;

function Fphi_2(x6, x5, x4, x3, x2, x1, x0, PASS: LongWord): LongWord;
begin
  if PASS = 3 then
    Result := Fphi_2p3(x6, x5, x4, x3, x2, x1, x0)
  else if PASS = 4 then
    Result := Fphi_2p4(x6, x5, x4, x3, x2, x1, x0)
  else
    Result := Fphi_2neut(x6, x5, x4, x3, x2, x1, x0);
end;

function Fphi_3p3(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_3(x6, x1, x2, x3, x4, x5, x0);
end;

function Fphi_3p4(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_3(x1, x4, x3, x6, x0, x2, x5);
end;

function Fphi_3neut(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_3(x2, x6, x0, x4, x3, x1, x5)
end;

function Fphi_3(x6, x5, x4, x3, x2, x1, x0, PASS: LongWord): LongWord;
begin
  if PASS = 3 then
    Result := Fphi_3p3(x6, x5, x4, x3, x2, x1, x0)
  else if PASS = 4 then
    Result := Fphi_3p4(x6, x5, x4, x3, x2, x1, x0)
  else
    Result := Fphi_3neut(x6, x5, x4, x3, x2, x1, x0);
end;

function Fphi_4p4(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_4(x6, x4, x0, x5, x2, x1, x3)
end;

function Fphi_4neut(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_4(x1, x5, x3, x2, x0, x4, x6);
end;

function Fphi_4(x6, x5, x4, x3, x2, x1, x0, PASS: LongWord): LongWord;
begin
  if PASS = 4 then
    Result := Fphi_4p4(x6, x5, x4, x3, x2, x1, x0)
  else
    Result := Fphi_4neut(x6, x5, x4, x3, x2, x1, x0);
end;

function Fphi_5(x6, x5, x4, x3, x2, x1, x0: LongWord): LongWord;
begin
  Result := f_5(x2, x5, x0, x6, x4, x3, x1);
end;

procedure FF_1(var x7: LongWord; x6, x5, x4, x3, x2, x1, x0, w, PASS: LongWord);
var
  temp: LongWord;
begin
  temp := Fphi_1(x6, x5, x4, x3, x2, x1, x0, PASS);
  x7 := ror(temp, 7) + ror(x7, 11) + w;
end;

procedure FF_2(var x7: LongWord; x6, x5, x4, x3, x2, x1, x0, w, c, PASS: LongWord);
var
  temp: LongWord;
begin
  temp := Fphi_2(x6, x5, x4, x3, x2, x1, x0, PASS);
  x7 := ror(temp, 7) + ror(x7, 11) + w + c;
end;

procedure FF_3(var x7: LongWord; x6, x5, x4, x3, x2, x1, x0, w, c, PASS: LongWord);
var
  temp: LongWord;
begin
  temp := Fphi_3(x6, x5, x4, x3, x2, x1, x0, PASS);
  x7 := ror(temp, 7) + ror(x7, 11) + w + c;
end;

procedure FF_4(var x7: LongWord; x6, x5, x4, x3, x2, x1, x0, w, c, PASS: LongWord);
var
  temp: LongWord;
begin
  temp := Fphi_4(x6, x5, x4, x3, x2, x1, x0, PASS);
  x7 := ror(temp, 7) + ror(x7, 11) + w + c;
end;

procedure FF_5(var x7: LongWord; x6, x5, x4, x3, x2, x1, x0, w, c: LongWord);
var
  temp: LongWord;
begin
  temp := Fphi_5(x6, x5, x4, x3, x2, x1, x0);
  x7 := ror(temp, 7) + ror(x7, 11) + w + c;
end;

procedure haval_hash_block(var state: THavalCtx; PASS: LongWord);
var
  t0, t1, t2, t3, t4, t5, t6, t7: LongWord;
  w: Pointer;
begin
  t0 := state.fingerprint[0];
  t1 := state.fingerprint[1];
  t2 := state.fingerprint[2];
  t3 := state.fingerprint[3];
  t4 := state.fingerprint[4];
  t5 := state.fingerprint[5];
  t6 := state.fingerprint[6];
  t7 := state.fingerprint[7];
  w := @state.block;

  // Pass 1
  FF_1(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(w)^, PASS);
  FF_1(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 1 * 4)^, PASS);
  FF_1(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) + 2 * 4)^, PASS);
  FF_1(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) + 3 * 4)^, PASS);
  FF_1(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 4 * 4)^, PASS);
  FF_1(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) + 5 * 4)^, PASS);
  FF_1(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) + 6 * 4)^, PASS);
  FF_1(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) + 7 * 4)^, PASS);

  FF_1(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) + 8 * 4)^, PASS);
  FF_1(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 9 * 4)^, PASS);
  FF_1(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +10 * 4)^, PASS);
  FF_1(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +11 * 4)^, PASS);
  FF_1(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +12 * 4)^, PASS);
  FF_1(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +13 * 4)^, PASS);
  FF_1(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +14 * 4)^, PASS);
  FF_1(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +15 * 4)^, PASS);

  FF_1(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +16 * 4)^, PASS);
  FF_1(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +17 * 4)^, PASS);
  FF_1(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +18 * 4)^, PASS);
  FF_1(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +19 * 4)^, PASS);
  FF_1(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +20 * 4)^, PASS);
  FF_1(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +21 * 4)^, PASS);
  FF_1(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +22 * 4)^, PASS);
  FF_1(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +23 * 4)^, PASS);

  FF_1(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +24 * 4)^, PASS);
  FF_1(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +25 * 4)^, PASS);
  FF_1(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +26 * 4)^, PASS);
  FF_1(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +27 * 4)^, PASS);
  FF_1(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +28 * 4)^, PASS);
  FF_1(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +29 * 4)^, PASS);
  FF_1(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +30 * 4)^, PASS);
  FF_1(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +31 * 4)^, PASS);

  // Pass 2
  FF_2(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) + 5 * 4)^, $452821E6, PASS);
  FF_2(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +14 * 4)^, $38D01377, PASS);
  FF_2(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +26 * 4)^, $BE5466CF, PASS);
  FF_2(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +18 * 4)^, $34E90C6C, PASS);
  FF_2(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +11 * 4)^, $C0AC29B7, PASS);
  FF_2(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +28 * 4)^, $C97C50DD, PASS);
  FF_2(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) + 7 * 4)^, $3F84D5B5, PASS);
  FF_2(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +16 * 4)^, $B5470917, PASS);

  FF_2(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(w)^, $9216D5D9, PASS);
  FF_2(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +23 * 4)^, $8979FB1B, PASS);
  FF_2(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +20 * 4)^, $D1310BA6, PASS);
  FF_2(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +22 * 4)^, $98DFB5AC, PASS);
  FF_2(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 1 * 4)^, $2FFD72DB, PASS);
  FF_2(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +10 * 4)^, $D01ADFB7, PASS);
  FF_2(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) + 4 * 4)^, $B8E1AFED, PASS);
  FF_2(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) + 8 * 4)^, $6A267E96, PASS);

  FF_2(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +30 * 4)^, $BA7C9045, PASS);
  FF_2(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 3 * 4)^, $F12C7F99, PASS);
  FF_2(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +21 * 4)^, $24A19947, PASS);
  FF_2(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) + 9 * 4)^, $B3916CF7, PASS);
  FF_2(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +17 * 4)^, $0801F2E2, PASS);
  FF_2(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +24 * 4)^, $858EFC16, PASS);
  FF_2(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +29 * 4)^, $636920D8, PASS);
  FF_2(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) + 6 * 4)^, $71574E69, PASS);

  FF_2(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +19 * 4)^, $A458FEA3, PASS);
  FF_2(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +12 * 4)^, $F4933D7E, PASS);
  FF_2(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +15 * 4)^, $0D95748F, PASS);
  FF_2(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +13 * 4)^, $728EB658, PASS);
  FF_2(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) + 2 * 4)^, $718BCD58, PASS);
  FF_2(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +25 * 4)^, $82154AEE, PASS);
  FF_2(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) +31 * 4)^, $7B54A41D, PASS);
  FF_2(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +27 * 4)^, $C25A59B5, PASS);

  // Pass 3
  FF_3(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +19 * 4)^, $9C30D539, PASS);
  FF_3(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) + 9 * 4)^, $2AF26013, PASS);
  FF_3(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) + 4 * 4)^, $C5D1B023, PASS);
  FF_3(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +20 * 4)^, $286085F0, PASS);
  FF_3(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +28 * 4)^, $CA417918, PASS);
  FF_3(t2, t1, t0, t7, t6, t5, t4, t3, PLongWord(LongWord(w) +17 * 4)^, $B8DB38EF, PASS);
  FF_3(t1, t0, t7, t6, t5, t4, t3, t2, PLongWord(LongWord(w) + 8 * 4)^, $8E79DCB0, PASS);
  FF_3(t0, t7, t6, t5, t4, t3, t2, t1, PLongWord(LongWord(w) +22 * 4)^, $603A180E, PASS);

  FF_3(t7, t6, t5, t4, t3, t2, t1, t0, PLongWord(LongWord(w) +29 * 4)^, $6C9E0E8B, PASS);
  FF_3(t6, t5, t4, t3, t2, t1, t0, t7, PLongWord(LongWord(w) +14 * 4)^, $B01E8A3E, PASS);
  FF_3(t5, t4, t3, t2, t1, t0, t7, t6, PLongWord(LongWord(w) +25 * 4)^, $D71577C1, PASS);
  FF_3(t4, t3, t2, t1, t0, t7, t6, t5, PLongWord(LongWord(w) +12 * 4)^, $BD314B27, PASS);
  FF_3(t3, t2, t1, t0, t7, t6, t5, t4, PLongWord(LongWord(w) +24 * 4)^, $78AF2FDA, PASS);

⌨️ 快捷键说明

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