package com.jj.springshop.pojo.utils;
import org.apache.commons.codec.digest.DigestUtils;
public class MD5Utils {
public static String md5(String src) {
return DigestUtils.md5Hex(src);
}
private static final String salt = "1a2b3c4d";
public static String inputPassToFormPass(String inputPass) {
String str = ""+salt.charAt(0)+salt.charAt(2) + inputPass +salt.charAt(5) + salt.charAt(4);
System.out.println(str);
return md5(str);
}
public static String formPassToDBPass(String formPass, String salt) {
String str = ""+salt.charAt(0)+salt.charAt(2) + formPass +salt.charAt(5) + salt.charAt(4);
return md5(str);
}
public static String inputPassToDbPass(String inputPass, String saltDB) {
String formPass = inputPassToFormPass(inputPass);
String dbPass = formPassToDBPass(formPass, saltDB);
return dbPass;
}
//编写主类进行测试
public static void main(String args[]) {
String s = "Kodak";
System.out.println("原始:" + s);
System.out.println("加密后:"+MD5Utils.inputPassToFormPass(s));
}
}
或者:
public class Encryption {
//MD5加密
public static String Md5Util(String password) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
String str=new BigInteger(1,md.digest()).toString(32);
return str;
}
}
用户注册测试:(密码加密)
public RespResult<String> insert(@RequestBody Member member,HttpServletRequest request) throws NoSuchAlgorithmException {
HttpSession session = request.getSession();
//密码加密
String newPassword = Encryption.Md5Util(member.getPassword());
member.setPassword(newPassword);
//手机号唯一判断
int i = memberMapper.selectExist(member.getPhone());
if (i>0){
return returnFailed("该手机号已被注册");
}
int j = memberMapper.insert(member);
if (j>0){
session.setAttribute("phone",member.getPhone());
session.setMaxInactiveInterval(60*60);
return returnSuccess("注册成功");
}else {
return returnFailed("注册失败");
}
}