Java对PHP服务器hmac_sha1签名认证方法的匹配实现

来源:互联网 发布:java jlabel 鼠标事件 编辑:程序博客网 时间:2024/04/30 07:53

如果你的API服务安全认证协议中要求使用hmac_sha1方法对信息进行编码, 

而你的服务是由PHP实现的,客户端是由JAVA实现的,那么为了对签名正确比对,就需要在两者之间建立能匹配的编码方式.


PHP侧如下:

[php] view plaincopy
  1. define('ID','123456');  
  2. define('KEY','k123456');  
  3.   
  4. $strToSign = "test_string";  
  5.   
  6. $utf8Str = mb_convert_encoding($strToSign"UTF-8");  
  7. $hmac_sha1_str = base64_encode(hash_hmac("sha1"$utf8Str, KEY));  
  8. $signature = urlencode($hmac_sha1_str);  
  9. print_r($signature);  

JAVA侧需要注意如下几点:

1. hmac_sha1编码结果需要转换成hex格式

2. java中base64的实现和php不一致,其中java并不会在字符串末尾填补=号以把字节数补充为8的整数

3. hmac_sha1并非sha1, hmac_sha1是需要共享密钥的


参考实现如下:

[java] view plaincopy
  1. import java.io.UnsupportedEncodingException;  
  2. import javax.crypto.Mac;  
  3. import javax.crypto.spec.SecretKeySpec;  
  4. import org.apache.wicket.util.crypt.Base64UrlSafe;  
  5.   
  6. public class test {  
  7.      public static void main(String[] args) {  
  8.         String key = "f85b8b30f73eb2bf5d8063a9224b5e90";  
  9.       String toHash =  "GET"+"\n"+"Thu, 09 Aug 2012 13:33:46 +0000"+"\n"+"/ApiChannel/Report.m";  
  10.       //String toHashUtf8 = URLEncoder.encode(toHash, "UTF-8");  
  11.         String res = hmac_sha1(toHash, key);  
  12.         //System.out.print(res+"\n");  
  13.           
  14.         String signature;  
  15.         try {  
  16.             signature = new String(Base64UrlSafe.encodeBase64(res.getBytes()),"UTF-8");  
  17.             signature = appendEqualSign(signature);  
  18.             System.out.print(signature);  
  19.         } catch (UnsupportedEncodingException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.       
  24.     public static String hmac_sha1(String value, String key) {  
  25.         try {  
  26.             // Get an hmac_sha1 key from the raw key bytes  
  27.             byte[] keyBytes = key.getBytes();             
  28.             SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");  
  29.   
  30.             // Get an hmac_sha1 Mac instance and initialize with the signing key  
  31.             Mac mac = Mac.getInstance("HmacSHA1");  
  32.             mac.init(signingKey);  
  33.   
  34.             // Compute the hmac on input data bytes  
  35.             byte[] rawHmac = mac.doFinal(value.getBytes());  
  36.   
  37.             // Convert raw bytes to Hex  
  38.             String hexBytes = byte2hex(rawHmac);  
  39.             return hexBytes;  
  40.         } catch (Exception e) {  
  41.             throw new RuntimeException(e);  
  42.         }  
  43.     }  
  44.   
  45.     private static String byte2hex(final byte[] b){  
  46.         String hs="";  
  47.         String stmp="";  
  48.         for (int n=0; n<b.length; n++){  
  49.             stmp=(java.lang.Integer.toHexString(b[n] & 0xFF));  
  50.             if (stmp.length()==1) hs=hs+"0"+stmp;  
  51.                 else hs=hs+stmp;  
  52.         }  
  53.         return hs;  
  54.     }     
  55.       
  56.     private static String appendEqualSign(String s){  
  57.         int len = s.length();  
  58.         int appendNum = 8 - (int)(len/8);  
  59.         for (int n=0; n<appendNum; n++){  
  60.             s += "%3D";  
  61.         }  
  62.         return s;  
  63.     }  
  64. }  

iefreer
0 0