+发表新主题
查看: 1635|回复: 0

[代码片段] 微信公共平台验证接口JAVA实现

[复制链接]

[代码片段] 微信公共平台验证接口JAVA实现

[复制链接]
水中花 发表于 2013-12-24 19:55:42 浏览:  1635 回复:  0 [显示全部楼层] 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
看到微信的公共平台接入文档接口验证的例子是PHP写的,对于很多不是做php的人来说有点麻烦,虽然编程思想是相同的,逻辑也很简单,但是一种语言有一种语言的语法规范,其实代码的编写还是差距挺大的。这里写一下JAVA版接口验证的实现。
  1. response.setContentType("text/html");
  2.         PrintWriter out = response.getWriter();
  3.         String signature = request.getParameter("signature");
  4.         String timestamp = request.getParameter("timestamp");
  5.         String nonce = request.getParameter("nonce");
  6.         String echostr = request.getParameter("echostr");
  7.         String reSignature = null;
  8.         try {
  9.             String[] str = { TOKEN, timestamp, nonce };
  10.             Arrays.sort(str);
  11.             String bigStr = str[0] + str[1] + str[2];
  12.             reSignature = new SHA1().getDigestOfString(bigStr.getBytes())
  13.                     .toLowerCase();
  14.         } catch (Exception e) {
  15.             e.printStackTrace();
  16.         }
  17.         if (null != reSignature && reSignature.equals(signature)) {
  18.             //请求来自微信
  19.             out.print(echostr);
  20.         } else {
  21.             out.print("error request! the request is not from weixin server");
  22.         }
  23.         out.flush();          out.close();
复制代码



逻辑代码就这些,就是按照文档上说的来:1. 将token、timestamp、nonce三个参数进行字典序排序2. 将三个参数字符串拼接成一个字符串进行sha1加密3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信上面代码中有一个工具类SHA1,就是实现了一个加密解密算法,网上也有,这里贴一下代码。
  1. package duanqing.test.servlet.tools;  
  2.   
  3. public class SHA1 {  
  4.     private final int[] abcde = { 0x67452301, 0xefcdab89, 0x98badcfe,  
  5.             0x10325476, 0xc3d2e1f0 };  
  6.     // 摘要数据存储数组  
  7.     private int[] digestInt = new int[5];  
  8.     // 计算过程中的临时数据存储数组  
  9.     private int[] tmpData = new int[80];  
  10.   
  11.     // 计算sha-1摘要  
  12.     private int process_input_bytes(byte[] bytedata) {  
  13.         // 初试化常量  
  14.         System.arraycopy(abcde, 0, digestInt, 0, abcde.length);  
  15.         // 格式化输入字节数组,补10及长度数据  
  16.         byte[] newbyte = byteArrayFormatData(bytedata);  
  17.         // 获取数据摘要计算的数据单元个数  
  18.         int MCount = newbyte.length / 64;  
  19.         // 循环对每个数据单元进行摘要计算  
  20.         for (int pos = 0; pos < MCount; pos++) {  
  21.             // 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中  
  22.             for (int j = 0; j < 16; j++) {  
  23.                 tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));  
  24.             }  
  25.             // 摘要计算函数  
  26.             encrypt();  
  27.         }  
  28.         return 20;  
  29.     }  
  30.   
  31.     // 格式化输入字节数组格式  
  32.     private byte[] byteArrayFormatData(byte[] bytedata) {  
  33.         // 补0数量  
  34.         int zeros = 0;  
  35.         // 补位后总位数  
  36.         int size = 0;  
  37.         // 原始数据长度  
  38.         int n = bytedata.length;  
  39.         // 模64后的剩余位数  
  40.         int m = n % 64;  
  41.         // 计算添加0的个数以及添加10后的总长度  
  42.         if (m < 56) {  
  43.             zeros = 55 - m;  
  44.             size = n - m + 64;  
  45.         } else if (m == 56) {  
  46.             zeros = 63;  
  47.             size = n + 8 + 64;  
  48.         } else {  
  49.             zeros = 63 - m + 56;  
  50.             size = (n + 64) - m + 64;  
  51.         }  
  52.         // 补位后生成的新数组内容  
  53.         byte[] newbyte = new byte[size];  
  54.         // 复制数组的前面部分  
  55.         System.arraycopy(bytedata, 0, newbyte, 0, n);  
  56.         // 获得数组Append数据元素的位置  
  57.         int l = n;  
  58.         // 补1操作  
  59.         newbyte[l++] = (byte) 0x80;  
  60.         // 补0操作  
  61.         for (int i = 0; i < zeros; i++) {  
  62.             newbyte[l++] = (byte) 0x00;  
  63.         }  
  64.         // 计算数据长度,补数据长度位共8字节,长整型  
  65.         long N = (long) n * 8;  
  66.         byte h8 = (byte) (N & 0xFF);  
  67.         byte h7 = (byte) ((N >> 8) & 0xFF);  
  68.         byte h6 = (byte) ((N >> 16) & 0xFF);  
  69.         byte h5 = (byte) ((N >> 24) & 0xFF);  
  70.         byte h4 = (byte) ((N >> 32) & 0xFF);  
  71.         byte h3 = (byte) ((N >> 40) & 0xFF);  
  72.         byte h2 = (byte) ((N >> 48) & 0xFF);  
  73.         byte h1 = (byte) (N >> 56);  
  74.         newbyte[l++] = h1;  
  75.         newbyte[l++] = h2;  
  76.         newbyte[l++] = h3;  
  77.         newbyte[l++] = h4;  
  78.         newbyte[l++] = h5;  
  79.         newbyte[l++] = h6;  
  80.         newbyte[l++] = h7;  
  81.         newbyte[l++] = h8;  
  82.         return newbyte;  
  83.     }  
  84.   
  85.     private int f1(int x, int y, int z) {  
  86.         return (x & y) | (~x & z);  
  87.     }  
  88.   
  89.     private int f2(int x, int y, int z) {  
  90.         return x ^ y ^ z;  
  91.     }  
  92.   
  93.     private int f3(int x, int y, int z) {  
  94.         return (x & y) | (x & z) | (y & z);  
  95.     }  
  96.   
  97.     private int f4(int x, int y) {  
  98.         return (x << y) | x >>> (32 - y);  
  99.     }  
  100.   
  101.     // 单元摘要计算函数  
  102.     private void encrypt() {  
  103.         for (int i = 16; i <= 79; i++) {  
  104.             tmpData = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14]  
  105.                     ^ tmpData[i - 16], 1);  
  106.         }  
  107.         int[] tmpabcde = new int[5];  
  108.         for (int i1 = 0; i1 < tmpabcde.length; i1++) {  
  109.             tmpabcde[i1] = digestInt[i1];  
  110.         }  
  111.         for (int j = 0; j <= 19; j++) {  
  112.             int tmp = f4(tmpabcde[0], 5)  
  113.                     + f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  114.                     + tmpData[j] + 0x5a827999;  
  115.             tmpabcde[4] = tmpabcde[3];  
  116.             tmpabcde[3] = tmpabcde[2];  
  117.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  118.             tmpabcde[1] = tmpabcde[0];  
  119.             tmpabcde[0] = tmp;  
  120.         }  
  121.         for (int k = 20; k <= 39; k++) {  
  122.             int tmp = f4(tmpabcde[0], 5)  
  123.                     + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  124.                     + tmpData[k] + 0x6ed9eba1;  
  125.             tmpabcde[4] = tmpabcde[3];  
  126.             tmpabcde[3] = tmpabcde[2];  
  127.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  128.             tmpabcde[1] = tmpabcde[0];  
  129.             tmpabcde[0] = tmp;  
  130.         }  
  131.         for (int l = 40; l <= 59; l++) {  
  132.             int tmp = f4(tmpabcde[0], 5)  
  133.                     + f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  134.                     + tmpData[l] + 0x8f1bbcdc;  
  135.             tmpabcde[4] = tmpabcde[3];  
  136.             tmpabcde[3] = tmpabcde[2];  
  137.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  138.             tmpabcde[1] = tmpabcde[0];  
  139.             tmpabcde[0] = tmp;  
  140.         }  
  141.         for (int m = 60; m <= 79; m++) {  
  142.             int tmp = f4(tmpabcde[0], 5)  
  143.                     + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]  
  144.                     + tmpData[m] + 0xca62c1d6;  
  145.             tmpabcde[4] = tmpabcde[3];  
  146.             tmpabcde[3] = tmpabcde[2];  
  147.             tmpabcde[2] = f4(tmpabcde[1], 30);  
  148.             tmpabcde[1] = tmpabcde[0];  
  149.             tmpabcde[0] = tmp;  
  150.         }  
  151.         for (int i2 = 0; i2 < tmpabcde.length; i2++) {  
  152.             digestInt[i2] = digestInt[i2] + tmpabcde[i2];  
  153.         }  
  154.         for (int n = 0; n < tmpData.length; n++) {  
  155.             tmpData[n] = 0;  
  156.         }  
  157.     }  
  158.   
  159.     // 4字节数组转换为整数  
  160.     private int byteArrayToInt(byte[] bytedata, int i) {  
  161.         return ((bytedata& 0xff) << 24) | ((bytedata[i + 1] & 0xff) << 16)  
  162.                | ((bytedata[i + 2] & 0xff) << 8) | (bytedata[i + 3] & 0xff);  
  163.    }  

  164.    // 整数转换为4字节数组  
  165.    private void intToByteArray(int intValue, byte[] byteData, int i) {  
  166.        byteData= (byte) (intValue >>> 24);  
  167.       byteData[i + 1] = (byte) (intValue >>> 16);  
  168.       byteData[i + 2] = (byte) (intValue >>> 8);  
  169.       byteData[i + 3] = (byte) intValue;  
  170.   }  

  171.   // 将字节转换为十六进制字符串  
  172.   private static String byteToHexString(byte ib) {  
  173.       char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',  
  174.               'B', 'C', 'D', 'E', 'F' };  
  175.       char[] ob = new char[2];  
  176.       ob[0] = Digit[(ib >>> 4) & 0X0F];  
  177.       ob[1] = Digit[ib & 0X0F];  
  178.       String s = new String(ob);  
  179.       return s;  
  180.   }  

  181.   // 将字节数组转换为十六进制字符串  
  182.   private static String byteArrayToHexString(byte[] bytearray) {  
  183.       String strDigest = "";  
  184.       for (int i = 0; i < bytearray.length; i++) {  
  185.           strDigest += byteToHexString(bytearray<i>);  
  186.       }  
  187.       return strDigest;  
  188.   }  

  189.   // 计算sha-1摘要,返回相应的字节数组  
  190.   public byte[] getDigestOfBytes(byte[] byteData) {  
  191.       process_input_bytes(byteData);  
  192.       byte[] digest = new byte[20];  
  193.       for (int i = 0; i < digestInt.length; i++) {  
  194.           intToByteArray(digestInt<i>, digest, i * 4);  
  195.       }  
  196.       return digest;  
  197.   }  

  198.   // 计算sha-1摘要,返回相应的十六进制字符串  
  199.   public String getDigestOfString(byte[] byteData) {  
  200.       return byteArrayToHexString(getDigestOfBytes(byteData));  
  201.   }  

  202.   public static void main(String[] args) {  
  203.       String data = "123456";  
  204.       System.out.println(data);  
  205.       String digest = new SHA1().getDigestOfString(data.getBytes()).toLowerCase();  
  206.       System.out.println(digest);  

  207.       // System.out.println( ToMD5.convertSHA1(data).toUpperCase());  
  208.   }  }  
复制代码



回复

使用道具 举报


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 赣南网

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4

用微信扫一扫

赣南网