• 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'};
        }
    }
  • 相关阅读:
    帆软报表实现全选全不选的功能
    knowledge_others
    skills_kafka
    skills_operation
    problems_others
    skills_windows
    c语言标识符
    快速排序法
    字符串处理scanf("%d%*c",&n);
    Byte.parseByte(String s,int radix)的解释
  • 原文地址:https://www.cnblogs.com/chong-zuo3322/p/12855036.html
Copyright © 2020-2023  润新知