【发布时间】:2016-05-17 23:48:00
【问题描述】:
我是安全和 Apache Shiro 的新手。我最近在学习 Shiro。 据我所知,使用 md5 哈希值有两种方法,代码如下。
public static void main(String[] args) {
SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();
ByteSource nextByteSource = generator.nextBytes();
Md5Hash md5Hash = new Md5Hash("234", ByteSource.Util.bytes(nextByteSource));
int iterations = 2;
md5Hash.setIterations(iterations);
System.out.println("md5hash to hex: "+md5Hash.toHex());
SimpleHash hash = new SimpleHash("md5","234",ByteSource.Util.bytes(nextByteSource),iterations);
System.out.println("simple hash to hex: "+hash.toHex());
}
Md5Hash 和 SimpleHash 使用相同的算法,相同的盐,相同的输入和相同的迭代,但输出不同:
md5hash to hex: 93a2bb8a10727716085e3ae234a90fc8
simple hash to hex: e691e292d8300f29c6e0c448f1ecba76
是什么让这两个散列值不同?
【问题讨论】:
-
更改
iterations有什么作用吗? -
是的。如果删除迭代,它们都返回相同的散列值(默认为 1)。基于 SimpleHash 的 api: hashIterations - 源参数散列攻击弹性的次数。如果迭代设置为 2,我认为哈希算法运行了两次,第二轮的输入(源)是第一轮的输出并且它们使用相同的盐,它应该为第二轮生成相同的哈希值。如果我错了,请纠正我。