[转]HMAC functions in Delphi (HMAC_SHA256, HMAC_SHA1)
来源:互联网 发布:邮箱如何注册淘宝账号 编辑:程序博客网 时间:2024/06/14 09:14
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:
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
- [转]HMAC functions in Delphi (HMAC_SHA256, HMAC_SHA1)
- MD5 SHA1 HMAC HMAC_SHA1区别
- MD5 SHA1 HMAC HMAC_SHA1区别
- MD5 SHA1 HMAC HMAC_SHA1区别
- hmac_SHA256加密
- JAVA Functions in XI(转)
- HMAC
- HMAC
- hmac
- HMAC authentication in ASP.NET Web API
- some functions in assembly
- Useful functions in PHP
- python build-in functions
- Inline Functions in C++
- Callback Functions in JavaScript
- Functions in R
- Overloading Functions in C
- Built-in Functions
- 使用shell脚本显示出当前路径或者去掉目录路径
- mysql存储过程详解
- windows 启动选项修改
- delphi 动态结构数组例程
- 如何使DELPHI程序在Win7下自动请求以管理员身份运行
- [转]HMAC functions in Delphi (HMAC_SHA256, HMAC_SHA1)
- 致自己
- delphi 中判断windows系统是否是64位系统
- (转)delphi chrome cef3 控件学习笔记 (一)
- (转)delphi chrome cef3 控件学习笔记 (二)
- (转)delphi chrome cef3 控件学习笔记 (三)
- (转)delphi chrome cef3 控件学习笔记 (四)
- (转)delphi chrome cef3 控件学习笔记 (五)
- 关于 cxGrid 的 filter 中,like 包含条件的改造