SHA加密

1.SHA加密

package com.yxkj.yxbgk.utils;


import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;

/**
 * @USER: 文 俊
 * @DATE: 2021-05-17
 * @description:功能描述
 */
public class SHA1Utils {

    public static final String TOKEN = "59750e50-f2de-43bc-8322-f10d1ec52eed";//用于生成数字签名

    public static String getSha1(String str) {

        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 (Exception e) {
            return null;
        }
    }
	/*
	timestamp:时间戳
	nonce:随机数
	token:token
	*/
    public static String encryptSHA1(String timestamp,String nonce,String token) throws Exception {
        System.out.println("datestamp="+timestamp);
        System.out.println("nonce="+nonce);
        String[] arr = new String[]{timestamp,nonce,token};
        Arrays.sort(arr);
        StringBuffer content = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        System.out.println(content.toString());
        String signature = org.apache.commons.codec.digest.DigestUtils.shaHex(content.toString().getBytes("UTF-8"));

        return signature;
    }
    /**
     * SHA加密
     * @param timestamp 时间戳
     * @param nonce 随机码
     * @param token 授权码
     * @return
     * @throws Exception
     */
    public static byte[]  encryptSHA(String timestamp,String nonce,String token) throws Exception {
        //
        String[] arr = new String[]{timestamp,nonce,token};
        Arrays.sort(arr);
        StringBuffer content = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        return content.toString().getBytes("UTF-8");

    }



    /**
     * 验证是否合法 验方式:
     *   1、通过时间戳、随机码、授权码加密
     *   2、认证密钥与加密后的密文比较

     * @param signature 认证密钥
     * @param timestamp 时间戳
     * @param nonce 随机码
     * @param token 授权码
     * @return true,认证成功;false认证失败
     *
     * 备注:
     *  因前期已对接部分单位,不能修改加解密方式;
     *  考虑各个单位开发语言不同,所以新增验证方法
     *  20180329 yuanl
     *
     * @author yuanliu
     */
    public static boolean validToken(String signature, String timestamp, String nonce,String token) {
        try {
            String v_signature = new BigInteger(encryptSHA(timestamp, nonce, token)).toString(64);//java
            String c_signature = encryptSHA1(timestamp, nonce, token) ;//C#
            if (signature.equals(v_signature)) {
                return true;
            } else if (signature.equals(c_signature)){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public static void main(String[] args) throws Exception {
        String signaure = encryptSHA1(DateUtils.dateToStamp(),SignatureUtils.getUUID(),TOKEN);
        System.out.println("signaure="+signaure);

    }
}

posted @ 2021-06-25 09:18  风飘落叶  阅读(230)  评论(0编辑  收藏  举报