Sign验证代码

Notice:由药联提供双方校验的token值,对方根据sign生成机制生成sign,和请求方中的sign进行对比。

PHP

$token = 'uniondrug';
$nonce =  '94092762BDA12EF580DA1B773738DE5E1';
$timestamp = '1507602046';
$data = array($token, $nonce, $timestamp);
sort($data, SORT_STRING);
$sign_orign = implode('',$data);
$sign = sha1($sign_orign);

JAVA

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/*
 * @author luzhouyu
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String nonce = "7896B8FDBE9527DFE88A6DF3B088569D";
        String timestamp = "1507602046";
        String token = "uniondrug";
        String[] data = {nonce,timestamp,token};
        Arrays.sort(data);
        String sign_origin = "";
        for(int i=0;i<data.length;i++) {
           sign_origin += data[i];
        }
        String sign = getSha1(sign_origin);
        System.out.println(sign);
    }

    public static String getSha1(String str){
        if (null == str || 0 == str.length()){
            return null;
        }
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

}