zoukankan      html  css  js  c++  java
  • SHA算法:签名串SHA算法Java语言参考(SHAHelper.java)

    SHAHelper.java
    package com.util; 
    /** 
     * @author wangxiangyu
     * @date:2017年10月16日 上午9:00:47 
     * 类说明:SHA签名算法
     */
    public class SHAHelper {
    
        private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
        
        /**
         * 转换字节数组为16进制字串
         * 
         * @param b 字节数组
         * @return 16进制字串
         */
        public static String byteArrayToHexString(byte[] b) {
            StringBuffer resultSb = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                resultSb.append(byteToHexString(b[i]));
            }
            return resultSb.toString();
        }
    
        private static String byteToHexString(byte b) {
            int n = b;
            if (n < 0)
                n = 256 + n;
            int d1 = n / 16;
            int d2 = n % 16;
            return hexDigits[d1] + hexDigits[d2];
        }
    
        /**
         * SHA摘要计算(byte[]).
         * 
         * @param src byte[]
         * @throws Exception
         * @return byte[] 16 bit digest
         */
        public static byte[] SHADigest(byte[] src) throws Exception {
            java.security.MessageDigest alg = java.security.MessageDigest
                    .getInstance("SHA");
            return alg.digest(src);
        }
        
        /**
         * SHA摘要计算(String).
         * 
         * @param src String型的目标串
         * @throws Exception
         * @return String
         */
        public static String SHADigest(String src) throws Exception {
            return byteArrayToHexString(SHADigest(src.getBytes("UTF-8")));
        }
    
        /** Test crypt */
        public static void main(String[] args) {
            try {
                // 获得的明文数据
                String serialNumber = "0101102010031723101273807F00179E";
                String reqBizArgs = "<ReqBizArgs version='1'><test>test</test></ReqBizArgs>";
                String key = "8204CDBD76BB69DAE040007F010017A4";
                String desStr = serialNumber + reqBizArgs + key;
                System.out.println("原文串 desStr = '" + desStr + "'");
                // 生成Signature
                String signature = SHADigest(desStr);
                System.out.println("签名串Signature = '" + signature + "'");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    Leetcode 剑指 Offer 27(二叉树的镜像)
    Leetcode 1022从根到叶的二进制之和
    Leetcode 993二叉树的堂兄弟节点
    Leetcode 965单值二叉树
    Leetcode 938 二叉搜索树的范围和
    hdu 2082 找单词
    母函数模板
    hdu 1398 Square Coins
    hdu 1085 Holding Bin-Laden Captive!
    hdu 1028 Ignatius and the Princess III
  • 原文地址:https://www.cnblogs.com/xyhero/p/9344665.html
Copyright © 2011-2022 走看看