shiro----编码解码(加密)(加salt盐)
Posted 苏格拉的底
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shiro----编码解码(加密)(加salt盐)相关的知识,希望对你有一定的参考价值。
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
DigestsUtil
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import sun.security.util.Password;
import java.util.HashMap;
import java.util.Map;
/**
* @Description:摘要
*/
public class DigestsUtil {
private static final String SHA1 = "SHA-1";//名称固定写法
private static final Integer ITERATIONS =512;
/**
* @Description sha1方法
* @param input 需要散列字符串
* @param salt 盐字符串
* @return
*/
public static String sha1(String input, String salt) {
return new SimpleHash(SHA1, input, salt,ITERATIONS).toString();
}
/**
* @Description 随机获得salt字符串
* @return
*/
public static String generateSalt(){
SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
return randomNumberGenerator.nextBytes().toHex();
}
/**
* @Description 生成密码字符密文和salt密文
* @param
* @return
*/
public static Map<String,String> entryptPassword(String passwordPlain) {
Map<String,String> map = new HashMap<>();
String salt = generateSalt();
String password =sha1(passwordPlain,salt);
map.put("salt", salt);
map.put("password", password);
return map;
}
}
ClientTest
import com.itheima.shiro.tools.DigestsUtil;
import com.itheima.shiro.tools.EncodesUtil;
import org.junit.Test;
import java.util.Map;
/**
* @Description:测试
*/
public class ClientTest {
/**
* @Description 测试16进制编码
*/
@Test
public void testHex(){
String val = "holle";
String flag = EncodesUtil.encodeHex(val.getBytes());
String valHandler = new String(EncodesUtil.decodeHex(flag));
System.out.println("比较结果:"+val.equals(valHandler));
}
/**
* @Description 测试base64编码
*/
@Test
public void testBase64(){
String val = "holle";
String flag = EncodesUtil.encodeBase64(val.getBytes());
String valHandler = new String(EncodesUtil.decodeBase64(flag));
System.out.println("比较结果:"+val.equals(valHandler));
}
@Test
public void testDigestsUtil(){
Map<String,String> map = DigestsUtil.entryptPassword("123");
System.out.println("获得结果:"+map.toString());
}
}
以上是关于shiro----编码解码(加密)(加salt盐)的主要内容,如果未能解决你的问题,请参考以下文章