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));
    }

}
原文地址:https://www.cnblogs.com/zhouheblog/p/13208755.html