shaindysha1怎么读读

小焱的网络随笔
小焱的网络随笔
发表时间:日16:44:51
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。【转】HMAC&functions&in&Delphi&(HMAC_SHA256,&HMAC_SHA1)
HMAC functions in Delphi
(HMAC_SHA256, HMAC_SHA1)
I came across HMAC
() 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
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(...) HMAC_HexStr(...)
and HMAC_Base64(...) are simply decorations of the
unit HMAC;
& System.SysUtils,
& EncdDecd,
& IdSSLOpenSSL,
& THMACUtils = class
function HMAC(aKey, aMessage: RawByteString): TB
function HMAC_HexStr(aKey, aMessage: RawByteString):
function HMAC_Base64(aKey, aMessage: RawByteString):
implementation
class function THMACUtils.HMAC(aKey,
aMessage: RawByteString): TB
& _HMAC: T;
& if not IdSSLOpenSSL.LoadOpenSSLLibrary
& _HMAC:= T.C
_HMAC.Key := BytesOf(aKey);
Result:= _HMAC.HashValue(BytesOf(aMessage));
class function
THMACUtils.HMAC_HexStr(aKey, aMessage: RawByteString):
& Result:= '0x';
& for I in HMAC(aKey, aMessage)
Result:= Result + IntToHex(I, 2);
class function
THMACUtils.HMAC_Base64(aKey, aMessage: RawByteString):
& _HMAC: TB
& _HMAC:= HMAC(aKey, aMessage);
& Result:= EncodeBase64(_HMAC,
Length(_HMAC));
Below there’s an example of how to use the THMACUtils
program HMACS
{$APPTYPE CONSOLE}
{$R *.res}
& System.SysUtils,
& IdHMACSHA1,
& IdHashMessageD
Write('HMAC_SHA1("key", "message")'#9#9'= ');
Writeln(THMACUtils.HMAC_HexStr('key', 'message' ));
Write('HMAC_SHA256("key", "message")'#9#9'= ');
Writeln(THMACUtils.HMAC_HexStr('key', 'message' ));
Write('HMAC_SHA1_Base64("key", "message")'#9'= ');
Writeln(THMACUtils.HMAC_Base64('key', 'message' ));
Write('HMAC_SHA256_Base64("key", "message")'#9'= ');
Writeln(THMACUtils.HMAC_Base64('key', 'message' ));
Exception do
Writeln(E.ClassName, ': ', E.Message);
原文地址:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。chloe indy包怎么鉴定真假_百度知道

参考资料

 

随机推荐