1.建立MD5的接口
public interface Md5Pwd {
public String encode(String password);
}
2.建立MD5的实现类
/**
* Md5加密
*
*
*/
public class Md5PwdImpl implements Md5Pwd{
//加密
public String encode(String password){
String algorithm = “MD5”;
//加盐所谓加盐就是在用户密码的基础上按照某一规则添加不规律的字符
MessageDigest instance = null;
try {
instance = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//加密
byte[] digest = instance.digest(password.getBytes());
//十六进制加密
char[] encodeHex = Hex.encodeHex(digest);
return new String(encodeHex);
}
}
3.测试一下
//测试一下Md5
public static void main(String[] args) {
Md5PwdEncoder m = new Md5PwdEncoder();
String encoder = m.encoder(“123456”);
System.out.println(encoder);
}
4.加入或创建utils.xml
5.Controller中使用
@Autowired
private Md5Pwd md5Pwd;