Delphi XE 中的字符串生成哈希值(MD5 / SHA-1 / Jenkins)


本文整理自网络,侵删。

 
Delphi XE8和更高版本具有在System.Hash单元中生成哈希值的功能。

System.Hash.THashMD5 
实现MD5哈希的记录。
System.Hash.THashSHA1 
实现SHA-1哈希的记录。

System.Hash.THashBobJenkins 
实现Jenkins哈希的记录。

以下代码是使用THashMD5的示例。

md5

uses System.Hash;

procedure TForm1.Button1Click(Sender: TObject);
const
  MSG = 'Hello, world';
var
  MD5: THashMD5;
  Hash: string;
begin
  MD5 := THashMD5.Create;
  MD5.Update(MSG);
  Hash := MD5.HashAsString;

  Edit1.Text := Hash;
end;
您还可以从字节字符串生成哈希值。

procedure TForm1.Button2Click(Sender: TObject);
const
  MSG = 'Hello, world';
var
  MD5: THashMD5;
  Bytes: TBytes;
  Hash: string;
begin
  MD5 := THashMD5.Create;
  Bytes := TEncoding.UTF8.GetBytes(MSG);
  MD5.Update(Bytes, Length(Bytes));
  Hash := MD5.HashAsString;

  Edit2.Text := Hash;
end;

相关阅读 >>

Delphi tfilestream流操作1

Delphi根据网络链接截取域名

Delphi把域名转换成ip

Delphi dll 字符串传递例子

改写个Delphi 版 lz前辈的openprocess

Delphi 实现文件分割合并

Delphi 改造shellexecute 精简函数

Delphi 通过机器名读取ip地址的代码

Delphi scrollbox1滚动框鼠标滚轮

Delphi [函数] unicode 检查字符串是否含中文字

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...