众筹网_工具类_Md5加密
管理员密码需要经过加密再存入数据库
Md5加密工具方法:
位置:/common-05util/src/main/java/com/crowd/util/CrowdUtil.java
/*** * 对明文字符串进行 MD5 加密 * * @param source 传入的明文字符串 * @return 加密结果 */ public static String md5(String source) { // 1.判断 source 是否有效 if (source == null || source.length() == 0) { // 2.如果不是有效的字符串抛出异常 throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE); } //这里对异常选择try catch而不是直接throw抛出去,抛到方法外面为什么呢? //如果抛出去了,最终还是要我们处理,增加麻烦,现在处理掉更加方便 try { // 3.获取 MessageDigest 对象,MessageDigest类是jdk提供的加密工具 String algorithm = "md5"; MessageDigest messageDigest = MessageDigest.getInstance(algorithm); // 4.获取明文字符串对应的字节数组 byte[] input = source.getBytes(); // 5.执行加密 byte[] output = messageDigest.digest(input); // 第6、7步为将字节数组转为字符串 // 6.创建 BigInteger 对象,1表示正数 int signum = 1; BigInteger bigInteger = new BigInteger(signum, output); // 7.按照 16 进制将 bigInteger 的值转换为字符串 //.toUpperCase()统一大写 int radix = 16; String encoded = bigInteger.toString(radix).toUpperCase(); return encoded; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }
浙公网安备 33010602011771号