2016-05-17 24 views
0

我是安全和Apache Shiro的新手。我最近在學習Shiro。 據我所知,有兩種方法通過使用md5來散列值,代碼如下。Apache Shiro MD5Hash和SimpleHash生成不同的值

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 

什麼使所述兩個散列值的不同?

+0

是否改變'iterations'做什麼? – Ownaginatious

+1

是的。如果迭代被刪除,它們都會返回相同的哈希值(默認值爲1)。基於SimpleHash的api:hashIterations - 源參數散列用於攻擊彈性的次數。如果迭代設置爲2,我認爲哈希算法運行了兩次,第二輪的輸入(源)是第一輪的輸出,它們使用相同的鹽,它應該爲第二輪生成相同的哈希值。如果我錯了,請糾正我。 –

回答

0

要創建的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()); 
    }