Home > Back-end >  Ask how to use the HMAC - SHA1 algorithm in delpih
Ask how to use the HMAC - SHA1 algorithm in delpih

Time:10-10

Ask how to use the HMAC - SHA1 algorithm in delpih?

CodePudding user response:

Ha ha to YY

CodePudding user response:

USES IdHMACSHA1;

Var
Bs: TBytes;
The begin
With TIdHMACSHA1. Create the do
Try
Key:=ToBytes (HashKey);
Bs:=HashValue (ToBytes (BaseString));
The finally
Free;
end;
end;

CodePudding user response:

refer to the second floor ysai response:
USES IdHMACSHA1;

Var
Bs: TBytes;
The begin
With TIdHMACSHA1. Create the do
Try
Key:=ToBytes (HashKey);
Bs:=HashValue (ToBytes (BaseString));
The finally
Free;
end;
end;

This is in delphi2010, some, delphi7 under tip can't find IdHMACSHA1. Dcu

CodePudding user response:

Now a lot of API access platform, from the right to sign of MD5 algorithm, changed to HMac - Sha1 algorithm,
But does not support HMac - Sha1 algorithm under delphi7,
How to let delphi7, can call the Hmac - sha1 algorithm,
No can directly call DLL

CodePudding user response:

This is indy in the component unit, to install the indy, I am using the latest version

CodePudding user response:

Indy delphi7 version, also some,
Refers to the latest xe4 version?

CodePudding user response:

This is clap nets in the sign of hmac - sha1 algorithm demo of a function:
/*
* generate signature
* @ param method the HTTP request method "get"/" post "//parameter 1: requests the string value of
* @ param name url_path CGI,//parameter 2: API relative URL string value
* @ param params URL request parameters//3: composed of the request of the request names and values, a hashmap data type values
* @ param secret key//4 parameters: the user site appkey string value
* @ sign return value//the return value, value for the request of hmac - sha1 encryption algorithm of computing the return value,
* @ throws OpensnsException does not support the specified encoding, and does not support the specified encryption method throws an exception,
*/
Private String makeSign (String method, String url_path, HashMap Params, String secret) throws OpenApiException {
String sig="";
Try {
Mac Mac Mac.=getInstance (" HmacSHA1 ");//1 to get a MAC object is hmacsha1
SecretKeySpec secretKey=new SecretKeySpec (secret getBytes (charset), MAC. GetAlgorithm ());//2 create secretKey object
MAC. The init (secretKey);//3 secretkey object, the object initialization MAC,
String mk=makeSource (method, url_path, params);//4 input method, url_path, parms parameters, get the source code
System. The out. Println (mk);
Byte [] hash=MAC. DoFinal (mk. GetBytes (charset));//5 MAC to the source code encryption, get the byte array
Sig=new String (Base64Coder encode (hash));//6 encode encoding, and then converted to a string output values
//sig=encodeUrl (sig);
} the catch (Exception e) {
Throw new OpenApiException (OpenApiException MAKE_SIGNATURE_ERROR, e);
}
Return sig.
}

Input post/get string value, relative API url string value, a hashmap mode request value, appkey string value, through the hmac algorithm, get the password value calculation,
18:04:19 ZCK (25594165)
Source code=source plain code

CodePudding user response:

Byte [] hash=MAC. DoFinal (mk. GetBytes (charset));//5 plain code into a byte array, MAC computed byte array
This is the core, according to the byte [] value, get the byte []

CodePudding user response:

Below is Delphi2010 implementation source code for reference only,

Uses IdHashMessageDigest HTTPApp, IdHMACSHA1 IdCoderMIME, DateUtils;//these files,



Procedure TForm1. Button5Click (Sender: TObject);

Var

SHttpMethod: AnsiString;

SRequestURL: AnsiString;

SRequestParameter: AnsiString;

SAppSecret: AnsiString;

SSignatureBaseString: AnsiString;

SMD5: TIdHashMessageDigest5;

The begin

SMD5:=TIdHashMessageDigest5. Create;



SAppSecret:='4 b50186d12987f405b17bc38c9b99b0a & amp; ';//key



SHttpMethod:='GET';

SRequestURL:='http://open.t.qq.com/cgi-bin/request_token';

SRequestParameter:='oauth_callback=null& Oauth_consumer_key=8 * * * * * * * 9 & amp; Oauth_nonce=';

SRequestParameter:=sRequestParameter + sMD5 HashStringAsHex (IntToStr (Random (10000)), nil);//random 32 digits

SRequestParameter:=sRequestParameter + '& amp; Oauth_signature_method=HMAC - SHA1 & amp; Oauth_timestamp=';

SRequestParameter:=sRequestParameter + FloatToStr (DateTimeToUnix (Now () - 1/3));//timestamp

SRequestParameter:=sRequestParameter + '& amp; Oauth_version=1.0 ';



SSignatureBaseString:=HTTPEncode (sHttpMethod) + '& amp; '+ HTTPEncode (sRequestURL) +' & amp; '+ HTTPEncode (sRequestParameter);

Memo1. Text:=Base64Encode (EncryptHMACSha1 (sSignatureBaseString sAppSecret));

SMD5. Free;

end;



Two functions Base64Encode EncryptHMACSha1;

//HMACSha1 algorithm

The function TForm1. EncryptHMACSha1 (Input, AKey: AnsiString) : TBytes;

Var

Key: TBytes;

The begin

With TIdHMACSHA1. Create the do

Try

Key:=BytesOf (AKey);

Result:=HashValue (BytesOf (Input));

The finally

Free;

end;

end;



//Base64 encoding

The function TForm1. Base64Encode (const Input: TBytes) : string;

The begin

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related