Delphi MD5
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的更多相关文章
- Delphi MD5加密
Delphi MD5加密 1. 引用两个单元 uses IdHash,IdHashMessageDigest; 2.编写加密函数 function TEncrypti ...
- Delphi 中的MD5实现方法(转)
在Delphi自带的Indy控件中其实是提供了MD2,MD4,MD5对象的,我们可以直接使用它们来完成MD5的签名算法.而不需要再去找其它的DLL或是Pas了. 在Uses单元中引用 IdHashMe ...
- delphi 10.1 Berlin 中使用自带的 MD5 校验
uses System.Hash;//要引用这个单元哈 var Digest: TBytes; MD5: THashMD5; MD5Buf: TBytes; params: string; begin ...
- MD5 与 SHA 在 Delphi 中函数实现,加密密码
MD5 与 SHA 在 Delphi 中函数实现. 为了加密密码,必须使用一种算法,查询资料,比较好的方法是使用:MD5等算法,参考:Delphi XE8 支持MD5 第一种方式是:引用 System ...
- Delphi中MD5实现方法(转)
原来写过一个计算MD5的程序,是用了一个叫MD5.pas的单元,使用起来还算简单,但还有更简单的办法,安装了indy就会有IdHashMessageDigest单元(delphi 7默认安装indy) ...
- Delphi微信支付【支持MD5和HMAC-SHA256签名与验签】
作者QQ:(648437169) 点击下载➨微信支付 微信支付api文档 [Delphi 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...
- 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 ...
- Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】
作者QQ:(648437169) 点击下载➨delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA ...
- Delphi 使用MD5 比对文件
使用MD5的方法比对CXimage里图片是否改变: Delphi7实现方法: uses IdHashMessageDigest function TForm1.GetImageMD5(cxImage: ...
随机推荐
- keep-alive 用法 及activated,deactivated这两个生命周期函数
keep-aliveProps: include - 字符串或正则表达式.只有名称匹配的组件会被缓存.exclude - 字符串或正则表达式.任何名称匹配的组件都不会被缓存.max - 数字.最多可以 ...
- c# 事件3
1.什么是事件,使对象或者类具有通知功能的成员.//为了解决字段在外部被滥用,推出了事件 事件的功能能=通知+可选的事件参数(具体的详细信息,包括谁发送了消息,发送的什么消息) 使用:用于对象或者类件 ...
- python join 和setDaemon 简介
Python多线程编程时,经常会用到join()和setDaemon()方法 1.join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等 ...
- jQuery Validation Engine(三) 基本常识
1:response.validateFail(fieldId, "机构英文名已被其他人使用"); //field为这个字段的id,”“ 双引号的内容,是提示语 <!DOCT ...
- CH11 关联容器
关联容器与顺序容器有着根本的不同:关联容器中的元素是按关键字来保存和访问的,而顺序容器是按它们在容器中的位置来顺序保存和访问的.两个主要的关联容器:map和set map 中的元素的是一个key-va ...
- P1435 回文字串(LCS问题)
题目背景 IOI2000第一题 题目描述(题目链接:https://www.luogu.org/problem/P1435) 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成 ...
- 5.3 Nginx 动静分离
Server 脚本片段 server { listen ; server_name ccserver1; #charset koi8-r; access_log logs/host.access.lo ...
- QEMU 运行uboot,动态加载内核与文件系统
背景 上一讲我们完成了 编译 QEMU 以及简单地做了仿真.这一讲在 启动uboot 的基础上进行,以加强对于 运行地址,加载地址等理解. 有关资料: uboot 与 代码重定位 有这样的约定,ubo ...
- POJ - 1061 青蛙的约会 (扩展欧几里得求同余式)
题意:两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对 ...
- mac flutter 创建过程及遇到的问题
参考: 1.入门: 在macOS上搭建Flutter开发环境 系统要求 2.mac配置环境变量 1.打开终端 2.clone flutter 命令: git clone -b beta https:/ ...