使用shiro的SimpleHash来生成常用的摘要串
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.2.4</version> </dependency>
引入上述的shiro的POM
SimpleHash类共有6个常用的hash算法的子实现类:
下面以常用的MD5摘要算法为例,使用Md5Hash类来进行测试类编写
Md5Hash的方法目录如下:
例子:
1 import org.apache.shiro.crypto.hash.Md5Hash; 2 import org.junit.Test; 3 4 public class MyTest { 5 @Test 6 public void hashTest(){ 7 String source = "hello"; 8 String salt = "!@#"; 9 System.out.println(new Md5Hash(source, salt).toString()); 10 System.out.println(new Md5Hash(source, salt).toBase64()); 11 System.out.println(new Md5Hash(source, salt).toHex()); 12 } 13 }
输出结果:
b0905398766e3e35d3598aaa597388c8 sJBTmHZuPjXTWYqqWXOIyA== b0905398766e3e35d3598aaa597388c8