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. 吴裕雄--天生自然HADOOP操作实验学习笔记:ETL案例

    实验目的 熟悉hadoop生态系统 初步了解大数据点击流分析业务 学会使用hadoop进行数据分析统计 实验原理 hadoop主要有三部分,hdfs做数据存储.mapreduce做数据计算.yarn做 ...

  2. 【剑指Offer面试编程题】题目1519:合并两个排序的链表--九度OJ

    题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. (hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每 ...

  3. myeclipse汉化

    MyEclipse默认安装在计算机用户目录下面,安装完成后对MyEclipse快捷方式使用鼠标右键属性---打开文件位置--进入安装的目录下面即可看到 zh_CN.7z解压缩将zh_CN目录文件放到 ...

  4. tomcat#结构

    下面是一个tomcat的配置文件,通过分析tomcat配置文件的结构,和相关注释,可以大致了解tomcat的结构 <?xml version="1.0" encoding=& ...

  5. Day9 - I - 不要62 HDU - 2089

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍, ...

  6. DataReader和DataAdapter的区别

    SqlDataReader是一个向前的指针,本身并不包含数据,调用一次Read()方法它就向前到下一条记录,一个SqlDataReader必须单独占用一个打开的数据库连接. 在使用 SqlDataRe ...

  7. 十六 StudentManagerSystem的一些业务实现

    1 删除学生的JSP实现: <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  8. mysql 子查询问题

    今天在做子查询的时候发现运行报错, 我的代码是select* from (....) device des ,我一直以为的是device是表名,然后dec是别名,后面问了同事才知道from(...)这 ...

  9. CentOS7 环境下 在Hadoop集群安装Hive

    1.下载Hive的tar.gz包:http://mirror.bit.edu.cn/apache/hive/ 2.放入CentOS 7 系统中并解压:tar -zxvf apache-hive-2.3 ...

  10. 查看 Secret【转】

    可以通过 kubectl get secret 查看存在的 secret. 显示有两个数据条目,kubectl describe secret 查看条目的 Key: 如果还想查看 Value,可以用  ...