Java如何做sha256哈希等于php crypt函数?结果不一样?

时间:2013-11-28 15:43:01

标签: java php hash sha sha256

我使用这个http://raginggoblin.wordpress.com/2012/08/11/java-alternative-to-php-crypt-function/相当于php crypt函数,或者它在这里说它是......

但是我在java和php中有相同的值但是散列结果是不同的...我想知道是否不是散列是不同的,因为它没有以相同的方式执行?我将这两个值发布到WS。

JAVA:


 String doc_data="{\"table\":\"1048582\"}";
 String data="$5$rounds=5000$503$La071hYxZERff9GGq0cb.x2k96Xx25\/C4vxQztQ7B96";
 String result=Crypt.crypt(doc_data, data);

PHP: 
 $params['result'] = crypt($params['doc_data'], @$this->initdata['data']);

我记录了它并且我从POST中输入了确切的数据......但结果却不一样..没有办法做到这一点吗?还是没有等价物?

在那个库之前我使用了Guava ..但仍然不一样

public static String crypt_sha256(String password, String salt) {
        int iteration_count = 5000;
        HashFunction func = Hashing.sha256();
        HashCode result = func.hashString(salt + param1, Charsets.UTF_8);

        for (int i = 0; i < iteration_count; i++) {
            result = func.hashBytes(result.asBytes());
        }

        return salt + result.toString();

}

1 个答案:

答案 0 :(得分:0)

Oke,到目前为止,我测试了一些库,而Winner是Apache的Crypt。 http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/Crypt.html

public static String crpyt_sha256_apache(String param1, String salt) {

        return Crypt.crypt(param1, salt);
    }