【问题标题】:Apache Shiro MD5Hash and SimpleHash generate different valuesApache Shiro MD5Hash 和 SimpleHash 生成不同的值
【发布时间】: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,我认为哈希算法运行了两次,第二轮的输入(源)是第一轮的输出并且它们使用相同的盐,它应该为第二轮生成相同的哈希值。如果我错了,请纠正我。

标签: java security shiro


【解决方案1】:

您正在创建的 md5Hash 只是一次迭代,而通过 SimpleHash 它的 2 次迭代,所以更改应该是:

public static void main(String[] args) {
int iterations = 2;     

SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();
        ByteSource nextByteSource = generator.nextBytes();
        Md5Hash md5Hash = new Md5Hash("234", ByteSource.Util.bytes(nextByteSource),iterations);


        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());
    }

【讨论】:

    猜你喜欢
    • 2020-02-06
    • 2014-04-07
    • 2016-04-04
    • 2012-04-11
    • 2013-03-13
    • 2014-11-16
    • 2015-09-18
    • 1970-01-01
    • 2018-09-03
    相关资源
    最近更新 更多