在Java中与php中从浮点数接收相同的sha256存在问题。

huangapple 未分类评论51阅读模式
英文:

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

&lt;?php
echo hash(&#39;sha256&#39;, 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(&quot;SHA-256&quot;).digest(new byte[]{(byte) &#39;1&#39;});

huangapple
  • 本文由 发表于 2020年5月2日 16:31:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61556494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定