英文:
Problem with receive the same sha256 from float value in Java as in php
问题
我在Java中创建与PHP中相同的哈希(SHA256)存在问题。
这个PHP函数
<?php
echo hash('sha256', 1.00);
返回的哈希值是
6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
http://sandbox.onlinephpfunctions.com/code/cc5ed1eb8b72e8e18774c39404f9be84be00e551
我的目标是在Java中获得相同的哈希值,但是当我尝试使用
org.apache.commons.codec.digest.DigestUtils.sha256Hex()
时,我总是得到与PHP中不同的值。我尝试过将Double、BigDecimal或标准浮点数转换为字节数组,然后再进行哈希处理。但是这并没有帮助。我应该怎么做?
英文:
I have a problem with create the same hash (SHA256) in Java as in PHP.
This PHP function
<?php
echo hash('sha256', 1.00);
returns hash
6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b
http://sandbox.onlinephpfunctions.com/code/cc5ed1eb8b72e8e18774c39404f9be84be00e551
My goal is receive the same hash in Java, but when I'm trying it by
org.apache.commons.codec.digest.DigestUtils.sha256Hex()
allways I'm receiving different values than in PHP. I was trying by converting Double, BigDecimal or standard float to byte array and than try to hash it. But it doesn't help. What I should do?
答案1
得分: 0
php实际上并未对浮点数值进行哈希处理,而是将其转换为一个字节的字符串"1"。只需在此在线工具中输入1,即可亲自查看。
在Java中,您可以通过以下方式获得相同的结果:
MessageDigest.getInstance("SHA-256").digest(new byte[]{(byte) '1'});
英文:
php is not actually hashing the float value, instead it converts it to the one byte string "1". Just type 1 into this online tool to see for yourself.
In Java, you get the same result with:
MessageDigest.getInstance("SHA-256").digest(new byte[]{(byte) '1'});
专注分享java语言的经验与见解,让所有开发者获益!
评论