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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi是一种编程语言,可以用来实现HMAC-SHA256算法。HMAC-SHA256是一种基于哈希函数和密钥的消息认证码算法,可以用来保护数据的完整性和认证性。在Delphi中实现HMAC-SHA256算法需要使用相关的库或者组件,例如可以使用Delphi中的Indy组件来实现HMAC-SHA256算法。 首先,需要在Delphi中导入Indy组件库,并创建一个TIdHMACSHA256对象来实现HMAC-SHA256算法。接着,需要设置该对象的密钥和要计算的消息,并调用Compute方法来计算HMAC-SHA256值。最后,可以将计算得到的HMAC-SHA256值用于数据的认证和完整性保护。 下面是一个简单的Delphi代码示例来实现HMAC-SHA256算法: ```Delphi uses IdHMACSHA256, IdGlobal; function CalculateHMACSHA256(key, data: string): string; var hmac: TIdHMACSHA256; begin hmac := TIdHMACSHA256.Create; try hmac.Key := ToBytes(key, IndyTextEncoding_UTF8); Result := BytesToHex(hmac.HashValue(ToBytes(data, IndyTextEncoding_UTF8))); finally hmac.Free; end; end; ``` 在上面的示例中,我们定义了一个CalculateHMACSHA256函数,该函数接受两个字符串参数:key和data,分别表示密钥和要计算的消息。函数内部创建了一个TIdHMACSHA256对象,并设置了密钥和消息,然后调用HashValue方法来计算HMAC-SHA256值,并使用BytesToHex函数将结果转换成十六进制字符串返回。 这样,我们就可以在Delphi中实现HMAC-SHA256算法并用于数据的认证和完整性保护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值