• java工具类3-签名加密


    import java.security.MessageDigest;
    
    import javax.crypto.Mac;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.dms.common.constant.CommonConstants;
    
    /**
     * 签名加密工具类<br/>
     * Date: 2018年<br/>
     * 
     * @version
     * @see
     */
    public class SignUtils {
        private static final Logger LOG = LoggerFactory.getLogger(SignUtils.class);
        //private static final String DEFAULT_CHARSET = "UTF-8";
        private static final char[] DIGITS;
    
        public static String hmacSha256(String key, String data) {
            try {
                Mac mac = Mac.getInstance("HmacSHA256");
                SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
                mac.init(signingKey);
                return encodeHex(mac.doFinal(data.getBytes()));
            } catch (Exception e) {
                LOG.error("execute hmacSHA256 error",e);
            }
    
            return null;
        }
    
        public static String md5(String data, String charset) {
            try {
                byte[] msg = data.getBytes(charset);
                MessageDigest md = MessageDigest.getInstance("MD5");
                return encodeHex(md.digest(msg));
            } catch (Exception e) {
                LOG.error("execute MD5 error",e);
            } 
            return null;
        }
    
        public static String sha256(String data, String charset) {
            try {
                byte[] msg = data.getBytes(charset);
                MessageDigest md = MessageDigest.getInstance("SHA-256");
                return encodeHex(md.digest(msg));
            } catch (Exception e) {
                LOG.error("execute SHA-256 error",e);
            } 
            return null;
        }
    
        public static String sha1(String data, String charset) {
            try {
                byte[] msg = data.getBytes(charset);
                MessageDigest md = MessageDigest.getInstance("SHA1");
                return encodeHex(md.digest(msg));
            } catch (Exception e) {
                LOG.error("execute SHA1 error",e);
            } 
            return null;
        }
    
        public static String sha1(String data) {
            return sha1(data, CommonConstants.DEFAULT_CHARSET);
        }
    
        private static String encodeHex(byte[] data) {
            int l = data.length;
            char[] out = new char[l << 1];
            int i = 0;
    
            for (int j = 0; i < l; ++i) {
                out[j++] = DIGITS[(240 & data[i]) >>> 4];
                out[j++] = DIGITS[15 & data[i]];
            }
    
            return new String(out);
        }
    
        static {
            DIGITS =
                    new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
                            'e', 'f'};
        }
    }
  • 相关阅读:
    POJ 2418 Hardwood Species
    用Excel打开csv格式文件并生成相应图形
    虚拟内存(Virtual Memory)
    POJ 3984 迷宫问题 (Dijkstra)
    算法导论16.22 01背包问题
    POJ 1019 Number Sequence
    POJ 1458 Common Subsequence (最长公共子序列)
    Java处理文件BOM头的方式推荐
    web开发中的中文编码问题
    struts2学习笔记之spring整合
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/12855036.html
Copyright © 2020-2023  润新知