/**
* base64编码和解码
*/
@Test
public void testBase64() {
String str = "hello";
//编码
//String encodeToString = Base64.encodeToString(str.getBytes());
String encodeToString = Base64.encodeToString(CodecSupport.toBytes(str));
System.out.println(encodeToString);
//解码
String decodeToString = Base64.decodeToString(encodeToString);
System.out.println(decodeToString);
}
/**
* 十六进制的编码和解码
*/
@Test
public void testHex() {
String str = "hello";
//编码
String encodeToString = Hex.encodeToString(str.getBytes());
System.out.println(encodeToString);
//解码
String decodeToString = new String(Hex.decode(encodeToString));
System.out.println(decodeToString);
}
/**
* md5加密
*/
@Test
public void testMd5(){
String str="123";
String s1 = new Md5Hash(str).toString();
String s2 = new Md5Hash(str).toBase64();
String s3 = new Md5Hash(str).toHex();
String s4 = new Md5Hash(str, "anycode", 3).toString(); //加密时指定salt盐值和散列次数
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
/**
* sha加密
* SHA1、SHA256、SHA384、SHA512
*/
@Test
public void testSha(){
String str="123";
String s1 = new Sha1Hash(str).toString();
String s2 = new Sha256Hash(str).toBase64();
String s3 = new Sha384Hash(str, "anycode", 2).toHex();
String s4 = new Sha512Hash(str, "anycode", 2).toHex();
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
/**
* 通用散列算法的工具类SimpleHash
*/
@Test
public void testSimpleHash(){
String str="123";
String s1 = new SimpleHash("md5", str, "anycode", 3).toString();
String s2 = new SimpleHash("sha-256", str, "anycode", 3).toString();
System.out.println(s1);
System.out.println(s2);
}
/**
* PasswordService
*/
@Test
public void testPwd(){
String password="123";
//PasswordService passwordService=new DefaultPasswordService();
//String encryptPassword = passwordService.encryptPassword(password);
//System.out.println(encryptPassword);
//boolean flag = passwordService.passwordsMatch(password, encryptPassword);
//System.out.println(flag);
PasswordServiceImpl passwordService=new PasswordServiceImpl();
passwordService.setAlgorithmName("md5");
passwordService.setSalt("anycode");
passwordService.setHashInteration(3);
String encryptPassword = passwordService.encryptPassword(password);
boolean flag = passwordService.passwordsMatch(password, encryptPassword);
System.out.println(flag);
}
/**
* 模拟用户注册
*/
@Test
public void regist(){
String username="admin";
String password="111";
System.out.println(new SimpleHash("md5",password,username,3).toString());
System.out.println(new SimpleHash("md5",password,username,3).toString());
System.out.println(new SimpleHash("md5","222","tom",3).toString());
}
shiro密码加密
最新推荐文章于 2023-07-08 10:18:13 发布