unit uMD5;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics; type
MD5Count = array[..] of DWORD;
MD5State = array[..] of DWORD;
MD5Block = array[..] of DWORD;
MD5CBits = array[..] of Byte;
MD5Digest = array[..] of Byte;
MD5Buffer = array[..] of Byte; MD5Context = record
State: MD5State;
Count: MD5Count;
Buffer: MD5Buffer;
end; procedure MD5Init(var Context: MD5Context);
procedure MD5Update(var Context: MD5Context; Input: PAnsiChar; Length: longword);
procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);
function MD5File(N: string): MD5Digest;
function MD5Print(D: MD5Digest): AnsiString;
function MD5F(FileName: AnsiString): AnsiString;
function MD5S(Str: AnsiString): AnsiString; function MD5FileText(filename: AnsiString): AnsiString; var
PADDING: MD5Buffer = ($, $, $, $, $, $, $, $, $, $, $,
$, $, $, $, $, $, $, $, $, $, $, $, $, $, $,
$, $, $, $, $, $, $, $, $, $, $, $, $, $, $,
$, $, $, $, $, $, $, $, $, $, $, $, $, $, $,
$, $, $, $, $, $, $, $); implementation function F(x, y, z: DWORD): DWORD;
begin
Result := (x and y) or ((not x) and z);
end; function G(x, y, z: DWORD): DWORD;
begin
Result := (x and z) or (y and (not z));
end; function H(x, y, z: DWORD): DWORD;
begin
Result := x xor y xor z;
end; function I(x, y, z: DWORD): DWORD;
var
I: TObject;
begin
Result := y xor (x or (not z));
end; procedure rot(var x: DWORD; N: Byte);
begin
x := (x shl N) or (x shr ( - N));
end; procedure FF(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);
begin
inc(a, F(b, c, D) + x + ac);
rot(a, s);
inc(a, b);
end; procedure GG(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);
begin
inc(a, G(b, c, D) + x + ac);
rot(a, s);
inc(a, b);
end; procedure HH(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);
begin
inc(a, H(b, c, D) + x + ac);
rot(a, s);
inc(a, b);
end; procedure II(var a: DWORD; b, c, D, x: DWORD; s: Byte; ac: DWORD);
begin
inc(a, I(b, c, D) + x + ac);
rot(a, s);
inc(a, b);
end; procedure Encode(Source, Target: pointer; Count: longword);
var
s: PByte;
T: PDWORD;
I: longword;
begin
s := Source;
T := Target;
for I := to Count div do
begin
T^ := s^;
inc(s);
T^ := T^ or (s^ shl );
inc(s);
T^ := T^ or (s^ shl );
inc(s);
T^ := T^ or (s^ shl );
inc(s);
inc(T);
end;
end; procedure Decode(Source, Target: pointer; Count: longword);
var
s: PDWORD;
T: PByte;
I: longword;
begin
s := Source;
T := Target;
for I := to Count do
begin
T^ := s^ and $FF;
inc(T);
T^ := (s^ shr ) and $FF;
inc(T);
T^ := (s^ shr ) and $FF;
inc(T);
T^ := (s^ shr ) and $FF;
inc(T);
inc(s);
end;
end; procedure Transform(Buffer: pointer; var State: MD5State);
var
a, b, c, D: DWORD;
Block: MD5Block;
begin
Encode(Buffer, @Block, );
a := State[];
b := State[];
c := State[];
D := State[];
FF(a, b, c, D, Block[], , $D76AA478);
FF(D, a, b, c, Block[], , $E8C7B756);
FF(c, D, a, b, Block[], , $242070DB);
FF(b, c, D, a, Block[], , $C1BDCEEE);
FF(a, b, c, D, Block[], , $F57C0FAF);
FF(D, a, b, c, Block[], , $4787C62A);
FF(c, D, a, b, Block[], , $A8304613);
FF(b, c, D, a, Block[], , $FD469501);
FF(a, b, c, D, Block[], , $698098D8);
FF(D, a, b, c, Block[], , $8B44F7AF);
FF(c, D, a, b, Block[], , $FFFF5BB1);
FF(b, c, D, a, Block[], , $895CD7BE);
FF(a, b, c, D, Block[], , $6B901122);
FF(D, a, b, c, Block[], , $FD987193);
FF(c, D, a, b, Block[], , $A679438E);
FF(b, c, D, a, Block[], , $49B40821);
GG(a, b, c, D, Block[], , $F61E2562);
GG(D, a, b, c, Block[], , $C040B340);
GG(c, D, a, b, Block[], , $265E5A51);
GG(b, c, D, a, Block[], , $E9B6C7AA);
GG(a, b, c, D, Block[], , $D62F105D);
GG(D, a, b, c, Block[], , $);
GG(c, D, a, b, Block[], , $D8A1E681);
GG(b, c, D, a, Block[], , $E7D3FBC8);
GG(a, b, c, D, Block[], , $21E1CDE6);
GG(D, a, b, c, Block[], , $C33707D6);
GG(c, D, a, b, Block[], , $F4D50D87);
GG(b, c, D, a, Block[], , $455A14ED);
GG(a, b, c, D, Block[], , $A9E3E905);
GG(D, a, b, c, Block[], , $FCEFA3F8);
GG(c, D, a, b, Block[], , $676F02D9);
GG(b, c, D, a, Block[], , $8D2A4C8A);
HH(a, b, c, D, Block[], , $FFFA3942);
HH(D, a, b, c, Block[], , $8771F681);
HH(c, D, a, b, Block[], , $6D9D6122);
HH(b, c, D, a, Block[], , $FDE5380C);
HH(a, b, c, D, Block[], , $A4BEEA44);
HH(D, a, b, c, Block[], , $4BDECFA9);
HH(c, D, a, b, Block[], , $F6BB4B60);
HH(b, c, D, a, Block[], , $BEBFBC70);
HH(a, b, c, D, Block[], , $289B7EC6);
HH(D, a, b, c, Block[], , $EAA127FA);
HH(c, D, a, b, Block[], , $D4EF3085);
HH(b, c, D, a, Block[], , $4881D05);
HH(a, b, c, D, Block[], , $D9D4D039);
HH(D, a, b, c, Block[], , $E6DB99E5);
HH(c, D, a, b, Block[], , $1FA27CF8);
HH(b, c, D, a, Block[], , $C4AC5665);
II(a, b, c, D, Block[], , $F4292244);
II(D, a, b, c, Block[], , $432AFF97);
II(c, D, a, b, Block[], , $AB9423A7);
II(b, c, D, a, Block[], , $FC93A039);
II(a, b, c, D, Block[], , $655B59C3);
II(D, a, b, c, Block[], , $8F0CCC92);
II(c, D, a, b, Block[], , $FFEFF47D);
II(b, c, D, a, Block[], , $85845DD1);
II(a, b, c, D, Block[], , $6FA87E4F);
II(D, a, b, c, Block[], , $FE2CE6E0);
II(c, D, a, b, Block[], , $A3014314);
II(b, c, D, a, Block[], , $4E0811A1);
II(a, b, c, D, Block[], , $F7537E82);
II(D, a, b, c, Block[], , $BD3AF235);
II(c, D, a, b, Block[], , $2AD7D2BB);
II(b, c, D, a, Block[], , $EB86D391);
inc(State[], a);
inc(State[], b);
inc(State[], c);
inc(State[], D);
end; procedure MD5Init(var Context: MD5Context);
begin
with Context do
begin
State[] := $;
State[] := $EFCDAB89;
State[] := $98BADCFE;
State[] := $;
Count[] := ;
Count[] := ;
ZeroMemory(@Buffer, SizeOf(MD5Buffer));
end;
end; procedure MD5Update(var Context: MD5Context; Input: PAnsiChar; Length: longword);
var
Index: longword;
PartLen: longword;
I: longword;
begin
with Context do
begin
Index := (Count[] shr ) and $3F;
inc(Count[], Length shl );
if Count[] < (Length shl ) then
inc(Count[]);
inc(Count[], Length shr );
end;
PartLen := - Index;
if Length >= PartLen then
begin
CopyMemory(@Context.Buffer[Index], Input, PartLen);
Transform(@Context.Buffer, Context.State);
I := PartLen;
while I + < Length do
begin
Transform(@Input[I], Context.State);
inc(I, );
end;
Index := ;
end
else
I := ;
CopyMemory(@Context.Buffer[Index], @Input[I], Length - I);
end; procedure MD5Final(var Context: MD5Context; var Digest: MD5Digest);
var
Bits: MD5CBits;
Index: longword;
PadLen: longword;
begin
Decode(@Context.Count, @Bits, );
Index := (Context.Count[] shr ) and $3F;
if Index < then
PadLen := - Index
else
PadLen := - Index;
MD5Update(Context, @PADDING, PadLen);
MD5Update(Context, @Bits, );
Decode(@Context.State, @Digest, );
ZeroMemory(@Context, SizeOf(MD5Context));
end; function MD5String(M: AnsiString): MD5Digest;
var
Context: MD5Context;
begin
MD5Init(Context);
MD5Update(Context, PAnsiChar(M), Length(M));
MD5Final(Context, Result);
end; function MD5File(N: string): MD5Digest;
var
FileHandle: THandle;
MapHandle: THandle;
ViewPointer: pointer;
Context: MD5Context;
begin
MD5Init(Context);
FileHandle := CreateFile(PWideChar(WideString(N)), GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, );
if FileHandle <> INVALID_HANDLE_VALUE then
try
MapHandle := CreateFileMapping(FileHandle, nil, PAGE_READONLY, , , nil);
if MapHandle <> then
try
ViewPointer := MapViewOfFile(MapHandle, FILE_MAP_READ, , , );
if ViewPointer <> nil then
try
MD5Update(Context, ViewPointer, GetFileSize(FileHandle, nil));
finally
UnmapViewOfFile(ViewPointer);
end;
finally
CloseHandle(MapHandle);
end;
finally
CloseHandle(FileHandle);
end;
MD5Final(Context, Result);
end; function MD5Print(D: MD5Digest): AnsiString;
var
I: Byte;
const
Digits: array[..] of Ansichar = ('', '', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f');
begin
Result := '';
for I := to do
Result := Result + Digits[(D[I] shr ) and $0F] + Digits[D[I] and $0F];
end; function MD5Match(D1, D2: MD5Digest): boolean;
var
I: Byte;
begin
I := ;
Result := TRUE;
while Result and (I < ) do
begin
Result := D1[I] = D2[I];
inc(I);
end;
end; function MD5F(FileName: AnsiString): AnsiString;
begin
Result := MD5Print(MD5File(string(FileName)));
end; function MD5S(Str: AnsiString): AnsiString;
begin
Result := MD5Print(MD5String(Str));
end; function GetRamdomText(len: Integer): string;
var
I: Integer;
begin
Result := '';
Randomize;
for I := to Len - do
begin
Result := Result + Char( + Random());
end;
end; function MD5FileText(filename: AnsiString): AnsiString;
var
buf: array[..MAX_PATH - ] of Char;
path: AnsiString;
stream: TFileStream;
destStream: TMemoryStream;
destfile: string;
tmpText: string; strstream: TStringStream;
begin
GetTempPath(Length(buf), @buf[]);
path := AnsiString(string(buf));
tmpText := ExtractFileName(filename);
tmpText := Copy(tmpText, , Length(tmpText) - ); strstream := TStringStream.Create(tmpText, TEncoding.UTF8);
strstream.Position := ; stream := TFileStream.Create(filename, fmOpenRead);
try
destStream := TMemoryStream.Create;
try
stream.Position := ;
destStream.CopyFrom(stream, stream.Size); destfile := path + '\' + GetRamdomText();
if FileExists(destfile) then
DeleteFile(destfile); destStream.CopyFrom(strstream, strstream.Size);
destStream.SaveToFile(destfile);
strstream.Free; Result := MD5F(destfile);
finally
destStream.Free;
end;
finally
stream.Free;
end; if FileExists(destfile) then
DeleteFile(destfile);
end; end.

Delphi MD5的更多相关文章

  1. Delphi MD5加密

    Delphi MD5加密   1. 引用两个单元        uses   IdHash,IdHashMessageDigest;    2.编写加密函数    function TEncrypti ...

  2. Delphi 中的MD5实现方法(转)

    在Delphi自带的Indy控件中其实是提供了MD2,MD4,MD5对象的,我们可以直接使用它们来完成MD5的签名算法.而不需要再去找其它的DLL或是Pas了. 在Uses单元中引用 IdHashMe ...

  3. delphi 10.1 Berlin 中使用自带的 MD5 校验

    uses System.Hash;//要引用这个单元哈 var Digest: TBytes; MD5: THashMD5; MD5Buf: TBytes; params: string; begin ...

  4. MD5 与 SHA 在 Delphi 中函数实现,加密密码

    MD5 与 SHA 在 Delphi 中函数实现. 为了加密密码,必须使用一种算法,查询资料,比较好的方法是使用:MD5等算法,参考:Delphi XE8 支持MD5 第一种方式是:引用 System ...

  5. Delphi中MD5实现方法(转)

    原来写过一个计算MD5的程序,是用了一个叫MD5.pas的单元,使用起来还算简单,但还有更简单的办法,安装了indy就会有IdHashMessageDigest单元(delphi 7默认安装indy) ...

  6. Delphi微信支付【支持MD5和HMAC-SHA256签名与验签】

    作者QQ:(648437169) 点击下载➨微信支付            微信支付api文档 [Delphi 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...

  7. Delphi编码与签名【URL编码与解码,Base64编码与解码,MD5加密,HMAC-SHA1、HMAC-SHA224、HMAC-SHA256、HMAC-SHA384和HMAC-SHA512签名】

    作者QQ:(648437169) 点击下载➨delphi编码与签名 [Delphi编码与签名]URL编码与解码,Base64编码与解码,MD5加密,HMAC-SHA1.HMAC-SHA224.HMAC ...

  8. Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】

    作者QQ:(648437169) 点击下载➨delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA ...

  9. Delphi 使用MD5 比对文件

    使用MD5的方法比对CXimage里图片是否改变: Delphi7实现方法: uses IdHashMessageDigest function TForm1.GetImageMD5(cxImage: ...

随机推荐

  1. Sqlserver2012 使用sql语句增加(或删除)表一个字段

    前言 Mark在SqlServer 2012 的数据库使用sql语句增加(或删除)一张表的一个字段. 使用Sql语句增加表的一个字段 [1]语法: alter table table_name add ...

  2. runas的替代品CPAU使用

    runas替代软件CPAU 在windows系统下,想要实现某个程序不论何时都以指定的用户身份登录,因此找到了CPAU这个软件 cpau官方网站:https://www.joeware.net/fre ...

  3. 敌兵布阵-HDU1166 点修改+区间查询

    题目:C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  4. Linux系统资深运维工程师的进阶秘籍

    2010年毕业,从事IT行业已经接近7个年头,一路走来有很多不足,不论是技术上的还是工作当中的待人接事等,但正是这些不足让我有了现在的进步,技术上从最初的做水晶头,综合布线到服务器上架,网络设备调试, ...

  5. 第1节 storm编程:1、storm第一天上次课程内容回顾

    上次课程内容回顾:1.kafka的基本介绍:kafka是一个消息队列2.消息队列的作用:解耦3.kafka与传统消息队列的对比: 传统消息队列:支持事务 kafka的特点:比较快,比较快的两个原因:顺 ...

  6. 使用Linux命令修改数据库密码

    通过登录mysql系统,# mysql -uroot -pEnter password: [输入原来的密码]mysql>use mysql;mysql> update user set p ...

  7. Day9 - A - Apple Catching POJ - 2385

    Description 有两棵APP树,编号为1,2.每一秒,这两棵APP树中的其中一棵会掉一个APP.每一秒,你可以选择在当前APP树下接APP,或者迅速移动到另外一棵APP树下接APP(移动时间可 ...

  8. PHP实现微信网页登陆授权开发

    这篇文章主要介绍了关于PHP实现微信网页登陆授权开发,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 更多PHP相关知识请关注我的专栏PHP​zhuanlan.zhihu.com 微信开 ...

  9. Html5 -- 语义标签兼容性处理

    方法一:通过js处理 方法二:完美的处理方式 no.1 !--[if lte IE 8]> <script type="text/javascript" src=&qu ...

  10. loadrunner11完整卸载

    1.在控制面板中卸载掉loadrunner11的程序 2.删除loadrunner11安装目录 3.删除C盘(和安装目录下)   wlrun.*和vugen.* 4.删除回收站 5.清除注册表(运行r ...