2016-05-17 10 views
0

セキュリティとApache Shiroの新機能です。私は最近、シロを学んでいます。 私が知っている限り、md5を使用して値をハッシュする2つの方法があります。コードは以下のとおりです。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の両方が同じアルゴリズム、同じ塩、同じ入力と同じ繰り返しを使用しますが、出力が異なる2つのハッシュ値が違う

md5hash to hex: 93a2bb8a10727716085e3ae234a90fc8 
simple hash to hex: e691e292d8300f29c6e0c448f1ecba76 

何?

+0

は 'iterations'が何かを変えていますか? – Ownaginatious

+1

はい。反復が削除された場合、両方とも同じハッシュ値を返します(デフォルトは1)。 SimpleHashのapi:hashIterationsに基づいて、ソースの引数が攻撃の回復力のためにハッシュした回数。反復が2に設定されている場合、ハッシュアルゴリズムは2回実行され、2回目の入力(ソース)は最初のラウンドの出力で、同じ塩を使用し、2回目の同じハッシュ値を生成すると考えられます。私が間違っていれば私を修正してください。 –

答えて

0

その2反復SimpleHash経由しながら、変更がなければなりませんので、あなたが作成しているmd5Hashだけの繰り返しでである:

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()); 
    } 
関連する問題