md5加密工具类

xiaoxiao2021-02-28  65

   对数据进行md5加密,使用的工具类记录下来

public class EncryptUtil { /** * Encrypt byte array. */ public static byte[] encrypt(byte[] source, String algorithm) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(algorithm); md.reset(); md.update(source); return md.digest(); } /** * Encrypt string */ public static String encrypt(String source, String algorithm) throws NoSuchAlgorithmException { byte[] resByteArray = encrypt(source.getBytes(), algorithm); return StringUtils.toHexString(resByteArray); } /** * Encrypt string using MD5 algorithm */ public static String encryptMD5(String source) { if (source == null) { source = ""; } String result = ""; try { result = encrypt(source, "MD5"); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } return result; } public static void main(String[] args) { System.out.println(encryptMD5("sloan")); } }

   

转载请注明原文地址: https://www.6miu.com/read-36270.html

最新回复(0)