New file |
| | |
| | | package com.example.jz.utils; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.Base64; |
| | | |
| | | /** |
| | | * @author 安瑾然 |
| | | * @data 2022/7/18 - 10:52 AM |
| | | * @description 加密工具类 |
| | | */ |
| | | public class Md5Utils { |
| | | |
| | | public static String md5(String str) { |
| | | try { |
| | | MessageDigest md = MessageDigest.getInstance("MD5"); |
| | | md.update(str.getBytes()); |
| | | byte b[] = md.digest(); |
| | | |
| | | str = byteToStr(b); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | public static String byteToStr(byte[] b){ |
| | | int i; |
| | | StringBuffer buf = new StringBuffer(""); |
| | | for (int offset = 0; offset < b.length; offset++) { |
| | | i = b[offset]; |
| | | //System.out.println(i); |
| | | if (i < 0) |
| | | i += 256; |
| | | if (i < 16) |
| | | buf.append("0"); |
| | | buf.append(Integer.toHexString(i)); |
| | | } |
| | | return buf.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 传入文本内容,返回 SHA-256 串 |
| | | * |
| | | * @param strText |
| | | * @return |
| | | */ |
| | | public static String SHA256(final String strText) |
| | | { |
| | | return SHA(strText, "SHA-256"); |
| | | } |
| | | |
| | | public static String SHA1(final String strText) |
| | | { |
| | | return SHA(strText, "SHA-1"); |
| | | } |
| | | |
| | | /** |
| | | * 传入文本内容,返回 SHA-512 串 |
| | | * |
| | | * @param strText |
| | | * @return |
| | | */ |
| | | public static String SHA512(final String strText) |
| | | { |
| | | return SHA(strText, "SHA-512"); |
| | | } |
| | | |
| | | /** |
| | | * 字符串 SHA 加密 |
| | | * |
| | | * @param strText |
| | | * @return |
| | | */ |
| | | private static String SHA(final String strText, final String strType) |
| | | { |
| | | // 返回值 |
| | | String strResult = null; |
| | | |
| | | // 是否是有效字符串 |
| | | if (strText != null && strText.length() > 0) |
| | | { |
| | | try |
| | | { |
| | | // SHA 加密开始 |
| | | MessageDigest messageDigest = MessageDigest.getInstance(strType); |
| | | // 传入要加密的字符串 |
| | | messageDigest.update(strText.getBytes("utf-8")); |
| | | // 得到 byte 类型的结果 |
| | | byte byteBuffer[] = messageDigest.digest(); |
| | | strResult = byteToStr(byteBuffer); |
| | | } |
| | | catch (NoSuchAlgorithmException e) |
| | | { |
| | | e.printStackTrace(); |
| | | }catch (UnsupportedEncodingException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | return strResult; |
| | | } |
| | | |
| | | public static String base64(String str){ |
| | | String baseStr = null; |
| | | Base64.Encoder encoder = Base64.getEncoder(); |
| | | byte[] textByte; |
| | | try { |
| | | textByte = str.getBytes("UTF-8"); |
| | | baseStr = encoder.encodeToString(textByte); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return baseStr; |
| | | |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String password = "bunana1"; |
| | | System.out.println(md5(password)); |
| | | //String base64 = base64(sha512); |
| | | //System.out.println(base64); |
| | | //String pwd1 = md5(base64); |
| | | //System.out.println(pwd1); |
| | | } |
| | | } |