📄 frxmd5.pas
字号:
{******************************************}
{ }
{ FastReport v4.0 }
{ MD5 checksum generation }
{ }
{******************************************}
// Original RSA Data Security, Inc. Copyright notice
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
// rights reserved.
//
// License to copy and use this software is granted provided that it
// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
// Algorithm" in all material mentioning or referencing this software
// or this function.
// License is also granted to make and use derivative works provided
// that such works are identified as "derived from the RSA Data
// Security, Inc. MD5 Message-Digest Algorithm" in all material
// mentioning or referencing the derived work.
// RSA Data Security, Inc. makes no representations concerning either
// the merchantability of this software or the suitability of this
// software for any particular purpose. It is provided "as is"
// without express or implied warranty of any kind.
// These notices must be retained in any copies of any part of this
// documentation and/or software.
unit frxmd5;
{$I frx.inc}
interface
uses Classes;
const
//Magic initialization constants
MD5_INIT_STATE_0 = $67452301;
MD5_INIT_STATE_1 = $efcdab89;
MD5_INIT_STATE_2 = $98badcfe;
MD5_INIT_STATE_3 = $10325476;
//Constants for Transform routine.
MD5_S11 = 7;
MD5_S12 = 12;
MD5_S13 = 17;
MD5_S14 = 22;
MD5_S21 = 5;
MD5_S22 = 9;
MD5_S23 = 14;
MD5_S24 = 20;
MD5_S31 = 4;
MD5_S32 = 11;
MD5_S33 = 16;
MD5_S34 = 23;
MD5_S41 = 6;
MD5_S42 = 10;
MD5_S43 = 15;
MD5_S44 = 21;
//Transformation Constants - Round 1
MD5_T01 = $d76aa478;
MD5_T02 = $e8c7b756;
MD5_T03 = $242070db;
MD5_T04 = $c1bdceee;
MD5_T05 = $f57c0faf;
MD5_T06 = $4787c62a;
MD5_T07 = $a8304613;
MD5_T08 = $fd469501;
MD5_T09 = $698098d8;
MD5_T10 = $8b44f7af;
MD5_T11 = $ffff5bb1;
MD5_T12 = $895cd7be;
MD5_T13 = $6b901122;
MD5_T14 = $fd987193;
MD5_T15 = $a679438e;
MD5_T16 = $49b40821;
//Transformation Constants - Round 2
MD5_T17 = $f61e2562;
MD5_T18 = $c040b340;
MD5_T19 = $265e5a51;
MD5_T20 = $e9b6c7aa;
MD5_T21 = $d62f105d;
MD5_T22 = $02441453;
MD5_T23 = $d8a1e681;
MD5_T24 = $e7d3fbc8;
MD5_T25 = $21e1cde6;
MD5_T26 = $c33707d6;
MD5_T27 = $f4d50d87;
MD5_T28 = $455a14ed;
MD5_T29 = $a9e3e905;
MD5_T30 = $fcefa3f8;
MD5_T31 = $676f02d9;
MD5_T32 = $8d2a4c8a;
//Transformation Constants - Round 3
MD5_T33 = $fffa3942;
MD5_T34 = $8771f681;
MD5_T35 = $6d9d6122;
MD5_T36 = $fde5380c;
MD5_T37 = $a4beea44;
MD5_T38 = $4bdecfa9;
MD5_T39 = $f6bb4b60;
MD5_T40 = $bebfbc70;
MD5_T41 = $289b7ec6;
MD5_T42 = $eaa127fa;
MD5_T43 = $d4ef3085;
MD5_T44 = $04881d05;
MD5_T45 = $d9d4d039;
MD5_T46 = $e6db99e5;
MD5_T47 = $1fa27cf8;
MD5_T48 = $c4ac5665;
//Transformation Constants - Round 4
MD5_T49 = $f4292244;
MD5_T50 = $432aff97;
MD5_T51 = $ab9423a7;
MD5_T52 = $fc93a039;
MD5_T53 = $655b59c3;
MD5_T54 = $8f0ccc92;
MD5_T55 = $ffeff47d;
MD5_T56 = $85845dd1;
MD5_T57 = $6fa87e4f;
MD5_T58 = $fe2ce6e0;
MD5_T59 = $a3014314;
MD5_T60 = $4e0811a1;
MD5_T61 = $f7537e82;
MD5_T62 = $bd3af235;
MD5_T63 = $2ad7d2bb;
MD5_T64 = $eb86d391;
//Null data (except for first BYTE) used to finalise the checksum calculation
PADDING: array [0..63] 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);
type
uint4 = longword;
uchar = byte;
Puint4 = ^uint4;
Puchar = ^uchar;
TfrxMD5 = class (TObject)
private
m_State: array [0..3] of uint4;
m_Count: array [0..1] of uint4;
m_Buffer: array [0..63] of uchar;
m_Digest: array [0..15] of uchar;
procedure Transform(block: Puchar);
procedure Encode(dest: Puchar; src: Puint4; nLength: uint4);
procedure Decode(dest: Puint4; src: Puchar; nLength: uint4);
function RotateLeft(x: uint4; n: uint4): uint4;
procedure FF(var a: uint4; b: uint4; c: uint4; d: uint4; x: uint4; s: uint4; ac: uint4);
procedure GG(var a: uint4; b: uint4; c: uint4; d: uint4; x: uint4; s: uint4; ac: uint4);
procedure HH(var a: uint4; b: uint4; c: uint4; d: uint4; x: uint4; s: uint4; ac: uint4);
procedure II(var a: uint4; b: uint4; c: uint4; d: uint4; x: uint4; s: uint4; ac: uint4);
public
constructor Create;
procedure Init;
procedure Update(chInput: Puchar; nInputLen: uint4);
procedure Finalize;
function Digest: Puchar;
end;
function MD5String(szString: AnsiString): AnsiString;
function MD5File(szFilename: String): AnsiString;
function MD5Stream(Stream: TStream): AnsiString;
procedure MD5Buf(Buf: Pointer; const Len: Integer; Digest: Pointer);
function PrintMD5(md5Digest: Puchar): AnsiString;
implementation
uses SysUtils{$IFDEF Delphi12}, AnsiStrings{$ENDIF};
{$IFOPT Q+}
{$DEFINE OVERDEF}
{$Q-}
{$ENDIF}
function Byte2Hex(const b: byte): AnsiString;
var
H, L: Byte;
function HexChar(N : Byte) : AnsiChar;
begin
if (N < 10) then Result := AnsiChar(Chr(Ord('0') + N))
else Result := AnsiChar(Chr(Ord('A') + (N - 10)));
end;
begin
SetLength(Result, 2);
H := (b shr 4) and $f;
L := b and $f;
Result[1] := HexChar(H);
Result[2]:= HexChar(L);
end;
// PrintMD5: Converts a completed md5 digest into a char* string.
function PrintMD5(md5Digest: Puchar): AnsiString;
var
nCount: Integer;
tmp: Puchar;
begin
Result := '';
tmp := md5Digest;
for nCount := 0 to 15 do
begin
Result := Result + LowerCase(Byte2Hex(Byte(tmp^)));
Inc(tmp);
end;
end;
// MD5String: Performs the MD5 algorithm on a char* string, returning
// the results as a char*.
function MD5String(szString: AnsiString): AnsiString;
var
nLen: Integer;
alg: TfrxMD5;
begin
Result := '';
alg := TfrxMD5.Create;
try
nLen := Length(szString);
alg.Update(Puchar(szString), nLen);
alg.Finalize;
Result := PrintMD5(alg.Digest);
finally
alg.Free;
end;
end;
function MD5Stream(Stream: TStream): AnsiString;
var
nLen: Integer;
buf: array [0..255] of AnsiChar;
alg: TfrxMD5;
oldpos: Longint;
begin
Result := '';
alg := TfrxMD5.Create;
oldpos := Stream.Position;
try
Stream.Position := 0;
nLen := 256;
while nLen = 256 do
begin
nLen := Stream.Read(buf, nLen);
alg.Update(@buf, nLen);
end;
alg.Finalize;
Result := PrintMD5(alg.Digest);
finally
Stream.Position := oldpos;
alg.Free;
end;
end;
function MD5File(szFilename: String): AnsiString;
var
f: TFileStream;
begin
Result := '';
f := TFileStream.Create(szFilename, fmOpenRead + fmShareDenyWrite);
try
Result := MD5Stream(f);
finally
f.Free
end;
end;
procedure MD5Buf(Buf: Pointer; const Len: Integer; Digest: Pointer);
var
md5: TfrxMD5;
d: Puchar;
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -