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

📄 idhashmessagedigest.pas

📁 Indy控件的使用源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{ $HDR$}
{**********************************************************************}
{ Unit archived using Team Coherence                                   }
{ Team Coherence is Copyright 2002 by Quality Software Components      }
{                                                                      }
{ For further information / comments, visit our WEB site at            }
{ http://www.TeamCoherence.com                                         }
{**********************************************************************}
{}
{ $Log:  10183: IdHashMessageDigest.pas 
{
{   Rev 1.0    2002.11.12 10:40:22 PM  czhower
}
{

  Implementation of the MD2, MD4 and MD5 Message-Digest Algorithm
  as specified in RFC 1319 (1115), 1320 (1186), 1321
 (See NOTE below for details of what is exactly implemented)

  Author: Pete Mee
  Port to Indy 8.1 Doychin Bondzhev (doychin@dsoft-bg.com)
  Copyright: (c) Chad Z. Hower and The Winshoes Working Group.

  NOTE:
  All MDx are ready and bug free.
}

unit IdHashMessageDigest;

{
2002-Feb-07 Pete Me
 - Fixed MD4 and MD5 for cases where n mod 512 = 448 where n is the number of
    bits processed.  An extra zero byte was being added to the input which
    offset the bit-size entry. 
2001-Oct-24 Pete Mee
 - Fixed MD4 and MD5 for cases where n mod 512 >= 448 where n is the number of
    bits processed.  This situation requires an additional block to be
    processed.
}

interface

uses
  Classes,
  IdGlobal,
  IdHash;

type
  T16x4LongWordRecord = array[0..15] of LongWord;
  T4x4x4LongWordRecord = array[0..3] of T4x4LongWordRecord;

  T384BitRecord = array [0..47] of byte;
  T128BitRecord = array [0..15] of byte;


  TIdHashMessageDigest = class(TIdHash128);

  TIdHashMessageDigest2 = class(TIdHashMessageDigest)
  protected
    FX: T384BitRecord;
    FCBuffer: T128BitRecord;
    FCheckSum: T128BitRecord;

    procedure MDCoder;
    procedure Reset;
  public
    function HashValue(AStream: TStream): T4x4LongWordRecord; override;
  end;

  TIdHashMessageDigest4 = class(TIdHashMessageDigest)
  protected
    FBuffer: T4x4LongWordRecord;
    FCBuffer: T16x4LongWordRecord;

    procedure MDCoder; virtual;

    function func_f(x, y, z : LongWord) : LongWord; virtual;
    function func_g(x, y, z : LongWord) : LongWord; virtual;
    function func_h(x, y, z : LongWord) : LongWord; virtual;
  public
    function HashValue(AStream: TStream): T4x4LongWordRecord; override;
  end;

  TIdHashMessageDigest5 = class(TIdHashMessageDigest4)
  protected
    procedure MDCoder; override;

    function func_g(x, y, z : LongWord) : LongWord; override;
    function func_i(x, y, z : LongWord) : LongWord; virtual;
  public

  end;

implementation

{ TIdHashMessageDigest2 }

const
  MD2_PI_SUBST : array [0..255] of byte = (
     41,  46,  67, 201, 162, 216, 124,   1,  61,  54,  84, 161, 236, 240,
      6,  19,  98, 167,   5, 243, 192, 199, 115, 140, 152, 147,  43, 217,
    188,  76, 130, 202,  30, 155,  87,  60, 253, 212, 224,  22, 103,  66,
    111,  24, 138,  23, 229,  18, 190,  78, 196, 214, 218, 158, 222,  73,
    160, 251, 245, 142, 187,  47, 238, 122, 169, 104, 121, 145,  21, 178,
      7,  63, 148, 194,  16, 137,  11,  34,  95,  33, 128, 127,  93, 154,
     90, 144,  50,  39,  53,  62, 204, 231, 191, 247, 151,   3, 255,  25,
     48, 179, 72, 165,  181, 209, 215,  94, 146,  42, 172,  86, 170, 198,
     79, 184,  56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116,
      4, 241,  69, 157, 112,  89, 100, 113, 135,  32, 134,  91, 207, 101,
    230,  45, 168,   2,  27,  96,  37, 173, 174, 176, 185, 246,  28,  70,
     97, 105,  52,  64, 126, 15,   85,  71, 163,  35, 221,  81, 175,  58,
    195,  92, 249, 206, 186, 197, 234,  38,  44,  83,  13, 110, 133,  40,
    132,   9, 211, 223, 205, 244, 65,  129,  77,  82, 106, 220,  55, 200,
    108, 193, 171, 250,  36, 225, 123,   8,  12, 189, 177,  74, 120, 136,
    149, 139, 227,  99, 232, 109, 233, 203, 213, 254,  59,   0,  29,  57,
    242, 239, 183,  14, 102,  88, 208, 228, 166, 119, 114, 248, 235, 117,
     75,  10,  49,  68,  80, 180, 143, 237,  31,  26, 219, 153, 141,  51,
     159,  17, 131, 20);

procedure TIdHashMessageDigest2.MDCoder;
const
  NumRounds = 18;
Var
  i, j: Byte;
  T: Word;
  LCheckSumScore: Byte;
begin
  // Move the next 16 bytes into the second 16 bytes of X.
  Move(FCBuffer[0], FX[16], 16);
  for i := 0 to 15 do
  begin
    FX[i + 32] := FCBuffer[i] xor FX[i];
  end;

  { Do 18 rounds. }
  T := 0;
  for i := 0 to NumRounds - 1 do
  begin
    for j := 0 to 47 do
    begin
      T := FX[j] xor MD2_PI_SUBST[T];
      FX[j] := T and $FF;
    end;
    T := (T + i) and $FF;
  end;

  LCheckSumScore := FChecksum[15];
  for i := 0 to 15 do
  begin
    LCheckSumScore := FChecksum[i] xor MD2_PI_SUBST[FCBuffer[i]
      xor LCheckSumScore];
    FChecksum[i] := LCheckSumScore;
  end;
end;

// Clear Buffer and Checksum arrays
procedure TIdHashMessageDigest2.Reset;
begin
  FillChar(FCheckSum[0], 16, 0);
  FillChar(FCBuffer, 16, 0);
  // Initialise the X buffer to zero.
  FillChar(FX[0], 48, 0);
end;

function TIdHashMessageDigest2.HashValue(AStream: TStream): T4x4LongWordRecord;
Var
  LStartPos: Integer;
  LSize: Int64;
  S1: String;
begin
  Reset;

  LStartPos := AStream.Position;
  LSize := AStream.Size - LStartPos;

  // Code the entire file in complete 16-byte chunks.
  while LSize - AStream.Position >= SizeOf(FCBuffer) do
  begin
    AStream.Read(FCBuffer[0], SizeOf(FCBuffer));
    MDCoder;
  end;

  SetLength(S1, SizeOf(FCBuffer));

  LStartPos := AStream.Read(S1[1], 16);
  // Step 1
  FillChar(S1[LStartPos + 1], 16 - LStartPos, Byte(16 - LStartPos));
  Move(S1[1], FCBuffer[0], 16);
  MDCoder;
  // Step 2
  Move(FCheckSUm[0], FCBuffer[0], 16);
  MDCoder;

  Move(FX[0], result[0], 16);
end;

{ TIdHashMessageDigest4 }

const
  MD4_INIT_VALUES: T4x4LongWordRecord = (
    $67452301, $EFCDAB89, $98BADCFE, $10325476);

procedure TIdHashMessageDigest4.MDCoder;
var
  A, B, C, D, i : LongWord;
  I64 : Int64;
  buff : T4x4x4LongWordRecord; // 64-byte buffer

  function DoAdd(const AOne, ATwo, AThree, AFour : LongWord) : LongWord;
  begin
    I64 := AOne;
    I64 := ((I64 + ATwo) and $FFFFFFFF);
    I64 := ((I64 + AThree) and $FFFFFFFF) + AFour;
    result := I64 and $FFFFFFFF;
  end;
  
begin
  A := FBuffer[0];
  B := FBuffer[1];
  C := FBuffer[2];
  D := FBuffer[3];

  System.Move(FCBuffer[0], buff[0], SizeOf(buff));

  // The following additions utilise Int64 to avoid integer overflow

  // Round 1
  for i := 0 to 3 do
  begin 
    A := ROL(DoAdd(func_f(B, C, D), A, buff[i,0], 0), 3);
    D := ROL(DoAdd(func_f(A, B, C), D, buff[i,1], 0), 7);
    C := ROL(DoAdd(func_f(D, A, B), C, buff[i,2], 0), 11);
    B := ROL(DoAdd(func_f(C, D, A), B, buff[i,3], 0), 19);
  end;

  // Round 2
  for i := 0 to 3 do
  begin
    A := ROL(DoAdd(func_g(B, C, D), A, buff[0,i], $5A827999), 3);
    D := ROL(DoAdd(func_g(A, B, C), D, buff[1,i], $5A827999), 5);
    C := ROL(DoAdd(func_g(D, A, B), C, buff[2,i], $5A827999), 9);
    B := ROL(DoAdd(func_g(C, D, A), B, buff[3,i], $5A827999), 13);
  end;

  // Round 3
  A := ROL(DoAdd(func_h(B, C, D), A, T16x4LongWordRecord(buff)[0], $6ED9EBA1), 3);
  D := ROL(DoAdd(func_h(A, B, C), D, T16x4LongWordRecord(buff)[8], $6ED9EBA1), 9);
  C := ROL(DoAdd(func_h(D, A, B), C, T16x4LongWordRecord(buff)[4], $6ED9EBA1), 11);
  B := ROL(DoAdd(func_h(C, D, A), B, T16x4LongWordRecord(buff)[12], $6ED9EBA1), 15);
  A := ROL(DoAdd(func_h(B, C, D), A, T16x4LongWordRecord(buff)[2], $6ED9EBA1), 3);
  D := ROL(DoAdd(func_h(A, B, C), D, T16x4LongWordRecord(buff)[10], $6ED9EBA1), 9);

⌨️ 快捷键说明

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