SHA加密
Posted zhouheblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)); } }
以上是关于SHA加密的主要内容,如果未能解决你的问题,请参考以下文章