DELPHI HMAC256
DELPHI HMAC256
unit HMAC;
interface
uses
System.SysUtils,
EncdDecd,
IdHMAC,
IdSSLOpenSSL,
IdHash;
type
THMACUtils = class
public
class function HMAC(aKey, aMessage: RawByteString): TBytes;
class function HMAC_HexStr(aKey, aMessage: RawByteString): RawByteString;
class function HMAC_Base64(aKey, aMessage: RawByteString): RawByteString;
end;
implementation
class function THMACUtils.HMAC(aKey, aMessage: RawByteString): TBytes;
var
_HMAC: T;
begin
if not IdSSLOpenSSL.LoadOpenSSLLibrary then Exit;
_HMAC:= T.Create;
try
_HMAC.Key := BytesOf(aKey);
Result:= _HMAC.HashValue(BytesOf(aMessage));
finally
_HMAC.Free;
end;
end;
class function THMACUtils.HMAC_HexStr(aKey, aMessage: RawByteString): RawByteString;
var
I: Byte;
begin
Result:= '0x';
for I in HMAC(aKey, aMessage) do
Result:= Result + IntToHex(I, 2);
end;
class function THMACUtils.HMAC_Base64(aKey, aMessage: RawByteString): RawByteString;
var
_HMAC: TBytes;
begin
_HMAC:= HMAC(aKey, aMessage);
Result:= EncodeBase64(_HMAC, Length(_HMAC));
end;
end.
下面是调用的例子:
program HMACSample;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
HMAC,
IdHMACSHA1,
IdHashMessageDigest;
begin
try
Write('HMAC_SHA1("key", "message")'#9#9'= ');
Writeln(THMACUtils.HMAC_HexStr('key', 'message' ));
Writeln;
Write('HMAC_SHA256("key", "message")'#9#9'= ');
Writeln(THMACUtils.HMAC_HexStr('key', 'message' ));
Writeln;
Write('HMAC_SHA1_Base64("key", "message")'#9'= ');
Writeln(THMACUtils.HMAC_Base64('key', 'message' ));
Writeln;
Write('HMAC_SHA256_Base64("key", "message")'#9'= ');
Writeln(THMACUtils.HMAC_Base64('key', 'message' ));
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/7735031.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?