I came across HMAC ( Hash-based message authentication code ) functions when developing a RESTful client application in Delphi. The RESTful Web Service API required me to send HMAC_SHA256 signatures (Base64 encoded) with each HTTP request.
HMAC functions take two parameters: a key and a message. The purpose of the HMAC function is to authenticate the message and guarantee the data integrity of the message.
The cryptographic strength of the HMAC function lies on the underlying hashing function that it uses: MD5, SHA1, SHA256, etc.
So, these functions are usually are termed HMAC_SHA256, HMAC_SHA1, HMAC_MD5 to connote the core hashing function being used.
The outcome of a HMAC function is basically an array of bytes, but it is usually represented as a hexadecimal string or encoded as a Base64 string. (The RESTful Web Service API needed the Base64 encoded output).
I Googled around for a bit, but I didn’t get a clean implementation of HMAC_SHA256 in Delphi (encoded as Base64). I glued together the pieces from some questions on StackOverflow and coded an Indy based implementation that uses generics to specify the core hashing function.
Brief description: I created a helper class called THMACUtils. Note that this class uses generics to indicate the hashing algorithm (TIdHMACSHA256, TIdHMACSHA1). Three functions are provided: the main thing happens in the HMAC(...) function; HMAC_HexStr(...) and HMAC_Base64(...) are simply decorations of the output.


unit HMAC;

interface

uses
System.SysUtils,
EncdDecd,
IdHMAC,
IdSSLOpenSSL,
IdHash;

type
THMACUtils<T:TIdHMAC, constructor>= 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<T>.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<T>.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<T>.HMAC_Base64(aKey, aMessage: RawByteString): RawByteString;
var
_HMAC: TBytes;
begin
_HMAC:= HMAC(aKey, aMessage);
Result:= EncodeBase64(_HMAC, Length(_HMAC));
end;


end.
原文地址: HMAC of Function

I came across HMAC (Hash-based message authentication code) functions when developing a RESTful client application in Delphi. The RESTful Web Service API required me to send HMAC_SHA256 signatures (Ba function Id HMAC SHA1 (BaseString,HashKey:String):TIdBytes; //OK begin with TId HMAC SHA1 .Create do Key := ToBytes(HashKey); Result := HashValu...
HMAC SHA1 是从 SHA1 哈希函数构造的一种键控哈希算法,被用作 HMAC (基于哈希的消息验证代码)。 此 HMAC 进程将密钥与消息数据混合,使用哈希函数对混合结果进行哈希计算,将所得哈希值与该密钥混合,然后再次应用哈希函数。 输出的哈希值长度为 160 位。 在发送方和接收方共享机密密钥的前提下, HMAC 可用于确定通过不安全信道发送的消息是否已被篡改。 发送方计算原始数
在pb下生成 sha1 加密字符串,很简单,查了很多资料,都很少介绍到,只好自己做了一个简单的,dll来自 delphi 自带的System.Hash.THash SHA1 .Get HMAC (s, key),希望能帮到需要的人。 为什么没有资源分数0分的选项?
系统需要试用阿里短信接口发送多可文档系统短信通知,但阿里没有提供 DELPHI 代码示例。在构造签名字符串时遇到了编码和签名几个坑,经过努力解决了签名问题,如下代码给大家参考: 相关代码需要包含的头如下: IdURI, IdGlobal, IdUriUtils, Id HMAC SHA1 , IdCoderMIME // URL编码函数,阿里提供的JAVA是 java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*