Android hamc 算法,知道进来帮帮忙 =======================

-船长- 2013-11-22 11:33:06
这是服务器 HMAC的算法,我不知道怎么在Android实现。

/**
* Calculate the HMAC for the http request.
* This function signs an api request using the information provided. The signature returned
* has been base64 encoded and then url encoded.
*
* @param string $algo The HMAC algorithm used
* @param string $time String representation of unix time
* @param string $nonce Nonce
* @param string $api_key Your api key
* @param string $secret_key Your private key
* @param string $get_variables URLEncoded string representation of the get variable parameters,
* eg "method=user&guid=2"
* @param string $post_hash Optional sha1 hash of the post data.
*
* @return string The HMAC signature
* @access private
*/
function calculate_hmac($algo, $time, $nonce, $api_key, $secret_key,
$get_variables, $post_hash = "") {

global $CONFIG;

elgg_log("HMAC Parts: $algo, $time, $api_key, $secret_key, $get_variables, $post_hash");

$ctx = hash_init(map_api_hash($algo), HASH_HMAC, $secret_key);

hash_update($ctx, trim($time));
hash_update($ctx, trim($nonce));
hash_update($ctx, trim($api_key));
hash_update($ctx, trim($get_variables));
if (trim($post_hash) != "") {
hash_update($ctx, trim($post_hash));
}

return urlencode(base64_encode(hash_final($ctx, true)));
}



这是我实现的方法,但是就是通过不了服务器的验证:
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
sha1.update(EncodingUtils.getAsciiBytes(PUBLIC_API_KEY));
sha1.update(EncodingUtils.getAsciiBytes(time));
sha1.update(EncodingUtils.getAsciiBytes(nonce));
sha1.update(EncodingUtils.getAsciiBytes(PRIVATE_API_KEY));
hmac = new String(Base64.encode(sha1.digest(), 0));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...全文
154 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
-船长- 2013-11-25
  • 打赏
  • 举报
回复
有人解释一下PHP服务端是怎么实现的吗 ?

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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