HMAC-SHA1算法签名及Authorization头认证
使用PHP进行HMAC-SHA1签名,并通过Authorization头认证Deom
$app_id = 'id';
$host = "test.abc.com";
$port = "80";
$app_Key = "key";
$app_timestamp = time();
$app_nonce = "8FINtYTUsKbSZGfl".time().rand(10,1000);
$uri = "/account/ass/verify.do"; //build string
$arr = array($app_timestamp, $app_nonce, "POST", $uri, $host, $port);
$text = join("\n", $arr) . "\n\n";
var_dump($text);
$sig = get_signature($text, $app_Key);
var_dump($sig); $headers=array();
$headers[] = "Authorization: MAC id=\"$app_id\",ts=\"$app_timestamp\",nonce=\"$app_nonce\",mac=\"$sig\"";
$headers[]="Content-Type: application/json"; $data='{"h":"2D4D9BE245FC4172989BC5FAD7EC8784","n":"97C5237B","t":"1428462620","v":"8F2ACF569FCBFDA9081248486240170B325AFF6D"}'; $result = curlPost('http://t-id.gionee.com'.$uri, $headers, $data); /**
* @使用HMAC-SHA1算法生成oauth_signature签名值
*
* @param $key 密钥
* @param $str 源串
*
* @return 签名值
*/ function get_signature($str, $key) {
$signature = "";
if (function_exists('hash_hmac')) {
$signature = base64_encode(hash_hmac("sha1", $str, $key, true));
} else {
$blocksize = 64;
$hashfunc = 'sha1';
if (strlen($key) > $blocksize) {
$key = pack('H*', $hashfunc($key));
}
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5c), $blocksize);
$hmac = pack(
'H*', $hashfunc(
($key ^ $opad) . pack(
'H*', $hashfunc(
($key ^ $ipad) . $str
)
)
)
);
$signature = base64_encode($hmac);
}
return $signature;
} function curlPost($url, $headers = array(), $data = array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //设置不验证ssl, 发送https接口请求时需要加此行
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //设置不验证ssl, 发送https接口请求时需要加此行
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
var_dump('set header');
var_dump($headers);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
附:java算法示例:
package net.svr.cas.test.demo;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import net.iharder.Base64; public class sig_demo_app {
private static final String MAC_NAME = "HmacSHA1";
private static final String UTF8 = "UTF-8";
private static final byte[] BYTEARRAY = new byte[0]; public static void main(String[] args) {
String host = "id.test.cn";
String port = "443";
String app_Key = "请填写你的AppKey";
String app_timestamp = "1369732102";
String app_nonce = "8FINtYTUsKbSZGfl";
String uri = "/account/verify.do"; String sig = macSig(host, port, app_Key, app_timestamp, app_nonce, "POST", uri);
System.out.println(sig);
} public static String macSig(String host, String port, String macKey, String timestamp, String nonce, String method, String uri) {
// 1. build mac string
// 2. hmac-sha1
// 3. base64-encoded StringBuffer buffer = new StringBuffer();
buffer.append(timestamp).append("\n");
buffer.append(nonce).append("\n");
buffer.append(method.toUpperCase()).append("\n");
buffer.append(uri).append("\n");
buffer.append(host.toLowerCase()).append("\n");
buffer.append(port).append("\n");
buffer.append("\n");
String text = buffer.toString(); System.out.println(text); byte[] ciphertext = null;
try {
ciphertext = hmacSHA1Encrypt(macKey, text);
} catch (Throwable e) {
return null;
} String sigString = Base64.encodeBytes(ciphertext);
return sigString;
} public static byte[] hmacSHA1Encrypt(String encryptKey, String encryptText) throws InvalidKeyException, NoSuchAlgorithmException {
Mac mac = Mac.getInstance(MAC_NAME);
mac.init(new SecretKeySpec(getBytes(encryptKey), MAC_NAME));
return mac.doFinal(getBytes(encryptText));
} public static byte[] getBytes(String value) {
return getBytes(value, UTF8);
} public static byte[] getBytes(String value, String charset) {
if (isNullOrEmpty(value))
return BYTEARRAY;
if (isNullOrEmpty(charset))
charset = UTF8;
try {
return value.getBytes(charset);
} catch (UnsupportedEncodingException e) {
return BYTEARRAY;
}
} public static boolean isNullOrEmpty(String s) {
if (s == null || s.isEmpty() || s.trim().isEmpty())
return true;
return false;
}
}
HMAC-SHA1算法签名及Authorization头认证的更多相关文章
- 可以进行SHA-1,SHA-224,SHA-256,SHA-384,SHA-512五种算法签名的工具类,以及简单说明
import java.security.MessageDigest; public class SignatureSHA { public static String signSHA(String ...
- 安全体系(三)——SHA1算法详解
本文主要讲述使用SHA1算法计算信息摘要的过程. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(一)—— DES算法详解 安全体系(二)——RSA算法详解 为保 ...
- LoadRunner中调用SHA1算法加密字符串
参考<SHA-1 hash for LoadRunner>: http://ptfrontline.wordpress.com/2010/03/02/sha-1-hash-for-load ...
- asp.net core 自定义认证方式--请求头认证
asp.net core 自定义认证方式--请求头认证 Intro 最近开始真正的实践了一些网关的东西,最近写几篇文章分享一下我的实践以及遇到的问题. 本文主要介绍网关后面的服务如何进行认证. 解决思 ...
- SHA1算法原理【转】
转自:https://www.cnblogs.com/scu-cjx/p/6878853.html 一.SHA1与MD5差异 SHA1对任意长度明文的预处理和MD5的过程是一样的,即预处理完后的明文长 ...
- SHA1算法原理
一.SHA1与MD5差异 SHA1对任意长度明文的预处理和MD5的过程是一样的,即预处理完后的明文长度是512位的整数倍,但是有一点不同,那就是SHA1的原始报文长度不能超过2的64次方,然后SHA1 ...
- delphi自带的SHA1算法
delphi自带的SHA1算法 uses IdHashSHA, IdGlobal; function SHA1(Input: String): String; begin with TIdHashSH ...
- SHA1算法实现及详解
1 SHA1算法简介 安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digit ...
- javascript实现SHA1算法
web里面密码直接传到后台是不安全的,有时候需要进行加密,找到一个不错的javascript SHA1算法: <!DOCTYPE html> <html lang="en& ...
随机推荐
- Windows IOT
Windows IOT 开发入门(准备工作) 终于抽出空来了,将最近研究的东西记录下来,物联网,万物皆可联网.然后可以做到智能家居,智能生活,智能城市....一大堆.吹牛的就不说了. 在实际应用中 ...
- Mahout推荐算法ItemBased
Mahout推荐的ItemBased 一. 算法原理 (一) 基本的 下面的例子,参见图评分矩阵:表现user,归类为item. 图(1) 该算法的原理: 1. 计算Item之间的相似度. ...
- Oracle使用并行建索引须要注意的问题
建索引时.我们为了建索引快.会加上并行,加上并行之后.此列索引就会是并行了. 訪问有并行度的索引时,CBO可能可能会考虑并行运行.这可能会引发一些问题,如在server资源紧张的时候用并行会引起更加严 ...
- 使用 WPF 实现所见即所得HTML编辑器
Introduction In this tip, you will learn the use of WPF webbrowser control and the use of the librar ...
- Java Swing 树状组件JTree的使用方法(转)
树中特定的节点可以由 TreePath(封装节点及其所有祖先的对象)标识,或由其显示行(其中显示区域中的每一行都显示一个节点)标识.展开 节点是一个非叶节点(由返回 false 的 TreeModel ...
- TypeScript 5 Angular 2
TypeScript 5 分钟快速入门 翻译:Angular 2 - TypeScript 5 分钟快速入门 原文地址:https://angular.io/docs/ts/latest/quicks ...
- memset功能的具体说明
1.void *memset(void *s,int c,size_t n)总的效果:内存空间开辟了 s 第一 n 字节的值设置为一个值 c. 2.样本#include void main(){cha ...
- Metatable和Metamethod(转)
Metatable和Metamethod是用来干啥的?它们可以使得表a和b的表达式“a + b”变得有意义,其中metatable使两个不相关的表a和b之间可以进行操作,而操作的具体行为比如说&quo ...
- uva 11324 The Largest Clique(图论-tarjan,动态规划)
Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...
- webBrower控件实现winform和webpage交互
添加WebBrowser控件 private WebBrowser webBrowser1; 引用页面的document对象 HtmlDocument doc = webBrowser1.Docume ...