• SHA加密


    package SSM.utils;
    
    import java.security.MessageDigest;
    import java.util.Calendar;
    import java.util.TimeZone;
    
    /**
     * SHA加密工具类
     * @author zhouhe
     * @date 2020/6/29 11:39
     */
    public class SecuritySHA1Utils {
        public static String shaEncode(String inStr) throws Exception {
            MessageDigest sha = null;
            try {
                sha = MessageDigest.getInstance("SHA");
            } catch (Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
                return "";
            }
    
            byte[] byteArray = inStr.getBytes("UTF-8");
            byte[] md5Bytes = sha.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        }
    
        public static void main(String args[]) throws Exception {
            Calendar cal = Calendar.getInstance();
            TimeZone tz = TimeZone.getTimeZone("GMT");
            cal.setTimeZone(tz);
            String timeStr = String.valueOf(cal.getTimeInMillis()/1000);    //返回的UTC时间戳(秒级) 这里要注意我们得到的UTC时间戳是毫秒,所以要除以1000
            System.out.println("t:"+timeStr);
    
            String str = new String("123456789");
            System.out.println("原始:" + str);
            System.out.println("SHA后:" + shaEncode(str));
        }
    
    }
  • 相关阅读:
    IO细述
    如何对HashMap按键值排序
    Java编程思想重点
    Java笔试题解答和部分面试题
    mysql优化
    MySQL 创建数据库并且指定编码
    JDBC 工具类
    JSP include标签和include指令
    SpingMVC ModelAttribute的用法
    Java 获取amr音频格式的音频长度
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/13208755.html
Copyright © 2020-2023  润新知