根据网上一个流传很久的基于Delph4的MD5单元修改的, 可以支持4G以上的文件, 可以支持UNICODE字符的Delphi

恩.......对于大文件速度稍微慢了一点点, 在我自己的电脑上测试, 5.2G文件计算速度86秒...凑合了吧

如果谁能优化一下速度, 请联系我哦~~~

  1. (*
  2. -----------------------------------------------------------------------------------------------
  3.  
  4. MD5 Message-Digest
  5.  
  6. Delphi Unit implementing the
  7. RSA Data Security, Inc. MD5 Message-Digest Algorithm
  8.  
  9. Implementation of Ronald L. Rivest's RFC 1321
  10.  
  11. Copyright ?1997-1999 Medienagentur Fichtner & Meyer
  12. Written by Matthias Fichtner
  13.  
  14. -----------------------------------------------------------------------------------------------
  15. 2013-11-11 堕落恶魔
  16. 增加对超过4G大小文件支持
  17. 增加对Delphi2009以及更高版本Delphi支持
  18.  
  19. 如果有修改, 希望能够将代码同步邮件给我, 谢谢
  20. hs_kill_god@hotmail.com
  21. -----------------------------------------------------------------------------------------------
  22.  
  23. *)
  24.  
  25. Unit MD5;
  26.  
  27. // -----------------------------------------------------------------------------------------------
  28. interface
  29. // -----------------------------------------------------------------------------------------------
  30.  
  31. uses
  32. Windows, Classes, SysUtils;
  33.  
  34. const
  35. MD5Length: Byte = ;
  36.  
  37. type
  38. MD5Count = array[..] of Int64;
  39. MD5State = array[..] of DWORD;
  40. MD5Block = array[..] of DWORD;
  41. MD5CBits = array[..] of byte;
  42. MD5Digest = array[..] of byte;
  43. MD5Buffer = array[..] of byte;
  44.  
  45. function MD5String(M: AnsiString): MD5Digest;
  46. function MD5File(N: AnsiString): MD5Digest;
  47. function MD5Print(D: MD5Digest): AnsiString;
  48.  
  49. function MD5Match(D1, D2: MD5Digest): boolean;
  50.  
  51. // -----------------------------------------------------------------------------------------------
  52. IMPLEMENTATION
  53. // -----------------------------------------------------------------------------------------------
  54.  
  55. var
  56. PADDING: MD5Buffer = (
  57. $, $, $, $, $, $, $, $,
  58. $, $, $, $, $, $, $, $,
  59. $, $, $, $, $, $, $, $,
  60. $, $, $, $, $, $, $, $,
  61. $, $, $, $, $, $, $, $,
  62. $, $, $, $, $, $, $, $,
  63. $, $, $, $, $, $, $, $,
  64. $, $, $, $, $, $, $, $
  65. );
  66.  
  67. type
  68. MD5Context = record
  69. State: MD5State;
  70. Count: MD5Count;
  71. Buffer: MD5Buffer;
  72. end;
  73.  
  74. function F(x, y, z: DWORD): DWORD; inline;
  75. begin
  76. Result := (x and y) or ((not x) and z);
  77. end;
  78.  
  79. function G(x, y, z: DWORD): DWORD; inline;
  80. begin
  81. Result := (x and z) or (y and (not z));
  82. end;
  83.  
  84. function H(x, y, z: DWORD): DWORD; inline;
  85. begin
  86. Result := x xor y xor z;
  87. end;
  88.  
  89. function I(x, y, z: DWORD): DWORD; inline;
  90. begin
  91. Result := y xor (x or (not z));
  92. end;
  93.  
  94. procedure rot(var x: DWORD; n: BYTE); inline;
  95. begin
  96. x := (x shl n) or (x shr ( - n));
  97. end;
  98.  
  99. procedure FF(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD); inline;
  100. begin
  101. inc(a, F(b, c, d) + x + ac);
  102. rot(a, s);
  103. inc(a, b);
  104. end;
  105.  
  106. procedure GG(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD); inline;
  107. begin
  108. inc(a, G(b, c, d) + x + ac);
  109. rot(a, s);
  110. inc(a, b);
  111. end;
  112.  
  113. procedure HH(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD); inline;
  114. begin
  115. inc(a, H(b, c, d) + x + ac);
  116. rot(a, s);
  117. inc(a, b);
  118. end;
  119.  
  120. procedure II(var a: DWORD; b, c, d, x: DWORD; s: BYTE; ac: DWORD); inline;
  121. begin
  122. inc(a, I(b, c, d) + x + ac);
  123. rot(a, s);
  124. inc(a, b);
  125. end;
  126.  
  127. // -----------------------------------------------------------------------------------------------
  128.  
  129. // Encode Count bytes at Source into (Count / ) DWORDs at Target
  130. procedure Encode(Source, Target: pointer; Count: longword);
  131. var
  132. S: PByte;
  133. T: PDWORD;
  134. I: longword;
  135. begin
  136. S := Source;
  137. T := Target;
  138. for I := to Count div do
  139. begin
  140. T^ := S^;
  141. inc(S);
  142. T^ := T^ or (S^ shl );
  143. inc(S);
  144. T^ := T^ or (S^ shl );
  145. inc(S);
  146. T^ := T^ or (S^ shl );
  147. inc(S);
  148. inc(T);
  149. end;
  150. end;
  151.  
  152. // Decode Count DWORDs at Source into (Count * ) Bytes at Target
  153. procedure Decode(Source, Target: pointer; Count: longword);
  154. var
  155. S: PDWORD;
  156. T: PByte;
  157. I: longword;
  158. begin
  159. S := Source;
  160. T := Target;
  161. for I := to Count do
  162. begin
  163. T^ := S^ and $FF;
  164. inc(T);
  165. T^ := (S^ shr ) and $FF;
  166. inc(T);
  167. T^ := (S^ shr ) and $FF;
  168. inc(T);
  169. T^ := (S^ shr ) and $FF;
  170. inc(T);
  171. inc(S);
  172. end;
  173. end;
  174.  
  175. function MP(P: Pointer; M: Integer): Pointer;
  176. begin
  177. Result := Pointer(Integer(P) + M);
  178. end;
  179.  
  180. // Transform State according to first bytes at Buffer
  181. procedure Transform(Buffer: pointer; var State: MD5State);
  182. var
  183. a, b, c, d: DWORD;
  184. Block: MD5Block;
  185. begin
  186. Encode(Buffer, @Block, );
  187. a := State[];
  188. b := State[];
  189. c := State[];
  190. d := State[];
  191. FF (a, b, c, d, Block[ ], , $d76aa478);
  192. FF (d, a, b, c, Block[ ], , $e8c7b756);
  193. FF (c, d, a, b, Block[ ], , $242070db);
  194. FF (b, c, d, a, Block[ ], , $c1bdceee);
  195. FF (a, b, c, d, Block[ ], , $f57c0faf);
  196. FF (d, a, b, c, Block[ ], , $4787c62a);
  197. FF (c, d, a, b, Block[ ], , $a8304613);
  198. FF (b, c, d, a, Block[ ], , $fd469501);
  199. FF (a, b, c, d, Block[ ], , $698098d8);
  200. FF (d, a, b, c, Block[ ], , $8b44f7af);
  201. FF (c, d, a, b, Block[], , $ffff5bb1);
  202. FF (b, c, d, a, Block[], , $895cd7be);
  203. FF (a, b, c, d, Block[], , $6b901122);
  204. FF (d, a, b, c, Block[], , $fd987193);
  205. FF (c, d, a, b, Block[], , $a679438e);
  206. FF (b, c, d, a, Block[], , $49b40821);
  207. GG (a, b, c, d, Block[ ], , $f61e2562);
  208. GG (d, a, b, c, Block[ ], , $c040b340);
  209. GG (c, d, a, b, Block[], , $265e5a51);
  210. GG (b, c, d, a, Block[ ], , $e9b6c7aa);
  211. GG (a, b, c, d, Block[ ], , $d62f105d);
  212. GG (d, a, b, c, Block[], , $);
  213. GG (c, d, a, b, Block[], , $d8a1e681);
  214. GG (b, c, d, a, Block[ ], , $e7d3fbc8);
  215. GG (a, b, c, d, Block[ ], , $21e1cde6);
  216. GG (d, a, b, c, Block[], , $c33707d6);
  217. GG (c, d, a, b, Block[ ], , $f4d50d87);
  218. GG (b, c, d, a, Block[ ], , $455a14ed);
  219. GG (a, b, c, d, Block[], , $a9e3e905);
  220. GG (d, a, b, c, Block[ ], , $fcefa3f8);
  221. GG (c, d, a, b, Block[ ], , $676f02d9);
  222. GG (b, c, d, a, Block[], , $8d2a4c8a);
  223. HH (a, b, c, d, Block[ ], , $fffa3942);
  224. HH (d, a, b, c, Block[ ], , $8771f681);
  225. HH (c, d, a, b, Block[], , $6d9d6122);
  226. HH (b, c, d, a, Block[], , $fde5380c);
  227. HH (a, b, c, d, Block[ ], , $a4beea44);
  228. HH (d, a, b, c, Block[ ], , $4bdecfa9);
  229. HH (c, d, a, b, Block[ ], , $f6bb4b60);
  230. HH (b, c, d, a, Block[], , $bebfbc70);
  231. HH (a, b, c, d, Block[], , $289b7ec6);
  232. HH (d, a, b, c, Block[ ], , $eaa127fa);
  233. HH (c, d, a, b, Block[ ], , $d4ef3085);
  234. HH (b, c, d, a, Block[ ], , $4881d05);
  235. HH (a, b, c, d, Block[ ], , $d9d4d039);
  236. HH (d, a, b, c, Block[], , $e6db99e5);
  237. HH (c, d, a, b, Block[], , $1fa27cf8);
  238. HH (b, c, d, a, Block[ ], , $c4ac5665);
  239. II (a, b, c, d, Block[ ], , $f4292244);
  240. II (d, a, b, c, Block[ ], , $432aff97);
  241. II (c, d, a, b, Block[], , $ab9423a7);
  242. II (b, c, d, a, Block[ ], , $fc93a039);
  243. II (a, b, c, d, Block[], , $655b59c3);
  244. II (d, a, b, c, Block[ ], , $8f0ccc92);
  245. II (c, d, a, b, Block[], , $ffeff47d);
  246. II (b, c, d, a, Block[ ], , $85845dd1);
  247. II (a, b, c, d, Block[ ], , $6fa87e4f);
  248. II (d, a, b, c, Block[], , $fe2ce6e0);
  249. II (c, d, a, b, Block[ ], , $a3014314);
  250. II (b, c, d, a, Block[], , $4e0811a1);
  251. II (a, b, c, d, Block[ ], , $f7537e82);
  252. II (d, a, b, c, Block[], , $bd3af235);
  253. II (c, d, a, b, Block[ ], , $2ad7d2bb);
  254. II (b, c, d, a, Block[ ], , $eb86d391);
  255. inc(State[], a);
  256. inc(State[], b);
  257. inc(State[], c);
  258. inc(State[], d);
  259. end;
  260.  
  261. // -----------------------------------------------------------------------------------------------
  262.  
  263. // Initialize given Context
  264. procedure MD5Init(var AContext: MD5Context);
  265. begin
  266. with AContext do
  267. begin
  268. State[] := $;
  269. State[] := $EFCDAB89;
  270. State[] := $98BADCFE;
  271. State[] := $;
  272. Count[] := ;
  273. Count[] := ;
  274. FillChar(Buffer, SizeOf(MD5Buffer), );
  275. end;
  276. end;
  277.  
  278. // Update given Context to include Length bytes of Input
  279. procedure MD5Update(var AContext: MD5Context; AInput: Pointer; ALength: Int64); overload;
  280. var
  281. nIndex: Int64;
  282. nPartLen: Int64;
  283. I: Int64;
  284. begin
  285. with AContext do
  286. begin
  287. nIndex := (Count[] shr ) and $3F;
  288. inc(Count[], ALength shl );
  289. if Count[] < (ALength shl ) then
  290. inc(Count[]);
  291. inc(Count[], ALength shr );
  292. end;
  293. nPartLen := - nIndex;
  294. if ALength >= nPartLen then
  295. begin
  296. Move(AInput^, AContext.Buffer[nIndex], nPartLen);
  297. Transform(@AContext.Buffer, AContext.State);
  298. I := nPartLen;
  299. while I + < ALength do
  300. begin
  301. Transform(MP(AInput, I), AContext.State);
  302. inc(I, );
  303. end;
  304. nIndex := ;
  305. end
  306. else
  307. I := ;
  308. Move(MP(AInput, I)^, AContext.Buffer[nIndex], ALength - i);
  309. end;
  310.  
  311. procedure MD5Update(var AContext: MD5Context; AInput: TStream; ALength: Int64); overload;
  312. var
  313. nIndex: Int64;
  314. nPartLen: Int64;
  315. i: Int64;
  316. x, m: Integer;
  317. nBuff: array[..] of Byte;
  318. begin
  319. with AContext do
  320. begin
  321. nIndex := (Count[] shr ) and $3F;
  322. inc(Count[], ALength shl );
  323. if Count[] < (ALength shl ) then
  324. inc(Count[]);
  325. inc(Count[], ALength shr );
  326. end;
  327. nPartLen := - nIndex;
  328. if ALength >= nPartLen then
  329. begin
  330. AInput.Read(AContext.Buffer[nIndex], nPartLen);
  331. Transform(@AContext.Buffer, AContext.State);
  332. i := nPartLen;
  333. while i < ALength - do
  334. begin
  335. AInput.Read(nBuff, );
  336. for x := to do
  337. Transform(@(nBuff[x * ]), AContext.State);
  338. Inc(i, );
  339. end;
  340. m := (ALength - i) div ;
  341. x := m * ;
  342. AInput.Read(nBuff, x);
  343. Inc(I, x);
  344. for x := to m - do
  345. Transform(@(nBuff[x * ]), AContext.State);
  346. nIndex := ;
  347. end
  348. else
  349. i := ;
  350. AInput.Read(AContext.Buffer[nIndex], ALength - i);
  351. end;
  352.  
  353. // Finalize given Context, create Digest and zeroize Context
  354. procedure MD5Final(var AContext: MD5Context; var Digest: MD5Digest);
  355. var
  356. nBits: MD5CBits;
  357. nIndex: longword;
  358. nPadLen: longword;
  359. begin
  360. Decode(@AContext.Count, @nBits, );
  361. nIndex := (AContext.Count[] shr ) and $3f;
  362. if nIndex < then
  363. nPadLen := - nIndex
  364. else
  365. nPadLen := - nIndex;
  366. MD5Update(AContext, @PADDING, nPadLen);
  367. MD5Update(AContext, @nBits, );
  368. Decode(@AContext.State, @Digest, );
  369. FillChar(AContext, SizeOf(MD5Context), );
  370. end;
  371.  
  372. // -----------------------------------------------------------------------------------------------
  373.  
  374. // Create digest of given Message
  375. function MD5String(M: AnsiString): MD5Digest;
  376. var
  377. nContext: MD5Context;
  378. begin
  379. MD5Init(nContext);
  380. MD5Update(nContext, pAnsiChar(M), length(M));
  381. MD5Final(nContext, Result);
  382. end;
  383.  
  384. // Create digest of file with given Name
  385. function MD5File(N: AnsiString): MD5Digest;
  386. var
  387. nContext: MD5Context;
  388. nFS: TFileStream;
  389. begin
  390. MD5Init(nContext);
  391. if FileExists(N) then
  392. begin
  393. nFS := TFileStream.Create(N, fmOpenRead or fmShareDenyWrite);
  394. try
  395. nFS.Position := ;
  396. MD5Update(nContext, nFS, nFS.Size);
  397. finally
  398. nFS.Free;
  399. end;
  400. end;
  401. MD5Final(nContext, Result);
  402. end;
  403.  
  404. // Create hex representation of given Digest
  405. function MD5Print(D: MD5Digest): AnsiString;
  406. var
  407. I, x: byte;
  408. const
  409. Digits: array[..] of AnsiChar =
  410. ('', '', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f');
  411. begin
  412. SetLength(Result, );
  413. for I := to do
  414. begin
  415. x := i * + ;
  416. Result[x] := Digits[(D[I] shr ) and $0f];
  417. Result[x + ] := Digits[D[I] and $0f];
  418. end;
  419. end;
  420.  
  421. // -----------------------------------------------------------------------------------------------
  422.  
  423. // Compare two Digests
  424. function MD5Match(D1, D2: MD5Digest): boolean;
  425. var
  426. I: byte;
  427. begin
  428. I := ;
  429. Result := TRUE;
  430. while Result and (I < ) do
  431. begin
  432. Result := D1[I] = D2[I];
  433. inc(I);
  434. end;
  435. end;
  436.  
  437. end.

支持4G以上文件的MD5单元的更多相关文章

  1. 谁说NTFS不支持UEFI启动的?启动U盘放不了超过4G的文件怎么办?Server2016 Win10 U盘UEFI启动制作方法

    大家都知道,我们平时做启动盘,用得最多的就是UltraISO(软碟通)这个工具了.用它我们可以很简单快速的把一个空白的普通U盘制作成一个PE启动U盘或系统U盘,然后用它来安装系统非常的方便,受到了广大 ...

  2. 如何让U盘支持大于4G的文件

    U盘通常是FAT(*)格式,不能支持大于4G的文件.为了实现这个目的,通常可以把U盘格式化成NTFS或者exFAT,这两种文件系统都支持大于4G的文件. 一.格式化成NTFS第一步首先我们把优盘插入电 ...

  3. U盘存放大于4G数据文件且无须格式化U盘的解决方法

    现在优盘的容量越来越大了,价格越来越便宜,可是它也有个缺点,因为它默认的文件系统是"FAT32",这种文件系统最大只能保存4G的文件,超过4G的文件就不能保存在优盘上了,这样就不能 ...

  4. 计算文件的MD5值(Java & Rust)

    Java public class TestFileMD5 { public final static String[] hexDigits = { "0", "1&qu ...

  5. 基础 - 32位操作系统最多只支持4G内存。

    32位操作系统最多只支持4G内存. CPU能不能直接访问硬盘的数据呢, 不能. 只能通过把硬盘的数据先放到内存里, 然后再从内存里访问硬盘的数据.我们平时玩游戏碰上读图loading 进度条的这个过程 ...

  6. MD5介绍及Windows下对文件做md5校验。

    MD5介绍参考百度百科: 摘要如下: MD5 校验和(checksum)通过对接收的传输数据执行散列运算来检查数据的正确性. 一个散列函数,比如 MD5,是一个将任意长度的数据字符串转化成短的固定长度 ...

  7. Vue.js实现大文件分片md5断点续传

    背景 根据部门的业务需求,需要在网络状态不良的情况下上传很大的文件(1G+).其中会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长,请求超时:3,传输中断,必须重新上传导致前功尽 ...

  8. Jav获取文件的MD5码,比较两个文件内容是否相同

    Jav获取文件的MD5码,比较两个文件内容是否相同 代码: System.out.println(DigestUtils.md5Hex(new FileInputStream(new File(&qu ...

  9. python计算文件的md5值

    前言 最近要开发一个基于python的合并文件夹/目录的程序,本来的想法是基于修改时间的比较,即判断文件有没有改变,比较两个文件的修改时间即可.这个想法在windows的pc端下测试没有问题. 但是当 ...

随机推荐

  1. 【转】cloudera新增用户权限配置

    转自 http://lookqlp.iteye.com/blog/2189119  .   配置起来较复杂,需要在有测试环境之后再进行配置测试.  之后是有上HUE的计划的,所以这个也是一定要做的. ...

  2. ember.js:使用笔记3 活用{{bind-attr}}

    说明:属性值绑定(属性值有无引号都可以) 如果是非布尔值: 一般使用,绑定其值; 使用冒号时,绑定名称,如 :high -> high; 如果是布尔值: 如果值是true,绑定其名,这里要注意驼 ...

  3. 使用OUYA第一次启动OUYA

    使用OUYA第一次启动OUYA 1.4  使用OUYA 初次使用OUYA时,其启动以后的设置过程耗时较长,也比较繁琐,因此本节将会对其做个详细介绍,让读者的使用过程更加顺利些!好的开端总归是一个不错的 ...

  4. git将本地仓库上传到远程仓库

    在已有的Git库中搭建新库,并且将本地的git仓库,上传到远程服务器的git库中,从而开始一个新的项目 首先,在本地新建文件夹abc,进入到abc里面,然后git init.这样就在本地初始化了一个g ...

  5. BZOJ3658 : Jabberwocky

    考虑将某线段下方的点取走: 将所有点从低到高排序 每扫描到一条水平线,对于上面每个点,找到它下面同色的前驱后继,统计中间点的个数 然后再把线上所有点插入数据结构中 最后再统计相邻的同色的点之间的点个数 ...

  6. ZOJ 3626(树形DP+背包+边cost)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...

  7. JAVA7遍历文件夹

    在JAVA7中提供了新的遍历文件的方法,比原有File类的递归遍历效率要好大约30%左右. 测试结果: 测试用的File类的递归,是经过对比测试几种方法,找出相对效率较好的来和JAVA7进行测试. 1 ...

  8. 【wikioi】1022 覆盖(匈牙利)

    http://www.wikioi.com/problem/1022/ 好不容易来一次1A,,水题啊.. 染色后裸匈牙利orz #include <cstdio> #include < ...

  9. 上传文件及$_FILES的用法实例

    Session变量($_SESSION):�php的SESSION函数产生的数据,都以超全局变量的方式,存放在$_SESSION变量中.1.Session简介SESSION也称为会话期,其是存储在服务 ...

  10. 从eclipse到Android studio/迁移eclipse的Android项目到Android studio平台的注意事项

    整体要注意的地方 先说明一下整体需要注意的地方 1在Android studio建立项目的时候,要注意包名和原来的完全一致,不然会有很多需要改动. 2依赖的jar一定一定要找齐,不然新建项目引用不到, ...