用于用户注册时密码的加密以及登录时密码的匹配
import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.util.DigestUtils; @Slf4j //@Component public class Md5Util { public final static String md5key = "allen"; /** * MD5方法 * * @param text 明文 * @param key 密钥 * @return 密文 * @throws Exception */ public static String md5(String text, String key) throws Exception { //加密后的字符串 // String encodeStr = DigestUtils.md5Hex(text + key); String encodeStr = DigestUtils.md5DigestAsHex((text+key).getBytes()); log.error("MD5加密后的字符串为:encodeStr=" +encodeStr); return encodeStr; } /** * MD5验证方法 * * @param text 明文 * @param key 密钥 * @param md5 密文 * @return true/false * @throws Exception */ public static boolean verify(String text, String key, String md5) throws Exception { //根据传入的密钥进行验证 String md5Text = md5(text, key); if (md5Text.equalsIgnoreCase(md5)) { log.error("MD5验证通过"); return true; } return false; } }