求类似java的hmac-sha1算法的delphi代码例子。

披着虎皮 的石头 2013-06-13 07:31:25
/*
* 生成签名
* @param method HTTP请求方法 "get" / "post" //参数1:请求方式的字符串值
* @param url_path CGI名字, //参数2:api的相对URL的字符串值
* @param params URL请求参数 //参数3:由request的请求名和值,构成的hashmap数据类型值
* @param secret 密钥 //参数4:用户网站的appkey的字符串值
* @return 签名值 //返回值,对请求值进行hmac-sha1加密算法的计算返回值。
* @throws OpensnsException 不支持指定编码以及不支持指定的加密方法时抛出异常。
*/
private String makeSign(String method, String url_path, HashMap<String, String> params, String secret) throws OpenApiException {
String sig = "";
try {
Mac mac = Mac.getInstance("HmacSHA1"); //1 得到一个是hmacsha1的mac对象
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(charset), mac.getAlgorithm()); //2 创建secretKey对象
mac.init(secretKey); //3 用secretkey对象,初始化mac对象。
String mk = makeSource(method, url_path, params); //4 输入method、url_path,parms参数,得到源名码
System.out.println(mk);
byte[] hash = mac.doFinal(mk.getBytes(charset)); //5 用mac对源名码加密,得到byte数组
sig = new String(Base64Coder.encode(hash)); //6 encode编码,然后转化为string输出值
// sig = encodeUrl(sig);
} catch (Exception e) {
throw new OpenApiException(OpenApiException.MAKE_SIGNATURE_ERROR, e);
}
return sig;
}
输入post/get 的string值、相对api的url string值、hashmap方式的request值、appkey的string值,经过hmac算法,得到计算的密码值。
源名码=源明码
...全文
404 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
x_kun 2013-09-27
  • 打赏
  • 举报
回复
delphi7如何
  • 打赏
  • 举报
回复 1
下面是Delphi2010实现的源码仅供参考。 Uses IdHashMessageDigest, HTTPApp, IdHMACSHA1, IdCoderMIME, DateUtils; //这些文件。 procedure TForm1.Button5Click(Sender: TObject); var sHttpMethod: AnsiString; sRequestURL: AnsiString; sRequestParameter: AnsiString; sAppSecret: AnsiString; sSignatureBaseString: AnsiString; sMD5: TIdHashMessageDigest5; begin sMD5 := TIdHashMessageDigest5.Create; sAppSecret := '4b50186d12987f405b17bc38c9b99b0a&'; //密钥 sHttpMethod := 'GET'; sRequestURL := 'http://open.t.qq.com/cgi-bin/request_token'; sRequestParameter := 'oauth_callback=null&oauth_consumer_key=8*******9&oauth_nonce='; sRequestParameter := sRequestParameter + sMD5.HashStringAsHex(IntToStr(Random(10000)), nil); //随机32位数 sRequestParameter := sRequestParameter + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp='; sRequestParameter := sRequestParameter + FloatToStr(DateTimeToUnix(Now()-1/3)); //时间戳 sRequestParameter := sRequestParameter + '&oauth_version=1.0'; sSignatureBaseString := HTTPEncode(sHttpMethod) + '&' + HTTPEncode(sRequestURL) + '&' + HTTPEncode(sRequestParameter); Memo1.Text := Base64Encode(EncryptHMACSha1(sSignatureBaseString, sAppSecret)); sMD5.Free; end; 两个函数 Base64Encode,EncryptHMACSha1; //HMACSha1算法 function TForm1.EncryptHMACSha1(Input,AKey:AnsiString): TBytes; var Key: TBytes; begin with TIdHMACSHA1.Create do try Key := BytesOf(AKey); Result := HashValue(BytesOf(Input)); finally Free; end; end; //Base64编码 function TForm1.Base64Encode(const Input: TBytes): string; begin Result := TIdEncoderMIME.EncodeBytes(Input); end; 希望能给后面新入手的朋友一点帮助。
  • 打赏
  • 举报
回复
OAuth的精髓 DM5是无密钥的加密算法,输入明文,得到密码 如果黑客截获URL值,里面包含网店的id、密码、spid等值, 就可以利用这些值构造另外一个URL, 再用DM5算法得到sign值,这样网店的接口也对黑客开放了。 所以MD5加密签权方法,让黑客有机会进入网店后台乱搞,比如修改价格、修改商品等 黑客可以用木马等程序,截获API的URL 而OAuth方法,是采用hmac-sha1算法,加密时必须OAuthKey(密钥值), 这个值是商家系统在平台注册商家系统时留在平台里的,把明文加密成sign值时, 必需要提供OAuthKey值才可以加密成密码。 而在URL只是包含网店的key、密码、商家ID,没有AuthKey, 那么黑客截获了URL,因为没有包含AuthKey值(在商家系统和平台才有), 那么就不能生成Sign值,所以就无法随意访问网店信息,这样就安全了。

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧