在 Java 中创建哈希的标准方法 [英] Standard way to create a hash in Java

查看:93
本文介绍了在 Java 中创建哈希的标准方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是关于在 Java 中创建哈希的正确方法:让我们假设我有一个正的 BigInteger 值,我想从中创建一个散列.让我们假设下面的 messageDigest 实例是 (SHA-256)

The question is about the correct way of creating a hash in Java: Lets assume I have a positive BigInteger value that I would like to create a hash from. Lets assume that below instance of the messageDigest is a valid instance of (SHA-256)

<代码>公共静态最终的BigInteger B =新的BigInteger( BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC996C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAEEB4012B7D7665238A8E3FB004B117B58",16);byte[] byteArrayBBigInt = B.toByteArray();this.printArray(byteArrayBBigInt);messageDigest.reset();messageDigest.update(byteArrayBBigInt);byte[] outputBBigInt = messageDigest.digest();

现在我只假设下面的代码是正确的,因为根据测试,我生成的哈希与以下生成的哈希匹配:

Now I only assume that the code below is correct, as according to the test the hashes I produce match with the one produced by:

http://www.fileformat.info/tool/hash.htm?十六进制= BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC996C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAEEB4012B7D7665238A8E3FB004B117B58

但是我不确定为什么我们要执行以下步骤,即因为在digest() 调用后返回的字节数组是有符号的,在这种情况下它是负数,我怀疑我们确实需要将它转换为正数,即我们可以使用这样的函数.

However I am not sure why we are doing the step below i.e. because the returned byte array after the digest() call is signed and in this case it is a negative, I suspect that we do need to convert it to a positive number i.e. we can use a function like that.

public static String byteArrayToHexString(byte[] b) {
    String result = "";
    for (int i=0; i < b.length; i++) {
      result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    return result;
}

因此:

String hex = byteArrayToHexString(outputBBigInt)
BigInteger unsignedBigInteger = new BigInteger(hex, 16);

当我从新的十六进制字符串构造一个 BigInteger 并将其转换回字节数组时,我看到符号位,即最重要的位,即最左边的位,被设置为 0表示该数字为正数,而且整个字节由零 ( 00000000 ) 构成.

When I construct a BigInteger from the new hex string and convert it back to byte array then I see that the sign bit, that is most significant bit i.e. the leftmost bit, is set to 0 which means that the number is positive, moreover the whole byte is constructed from zeros ( 00000000 ).

我的问题是:是否有任何 RFC 描述了为什么我们需要始终将哈希转换为正"无符号字节数组.我的意思是即使摘要调用后产生的数字是负数,它仍然是一个有效的哈希,对吗?因此,我们为什么需要那个额外的程序.基本上,我正在寻找一篇论文:标准或 rfc 描述我们需要这样做.

My question is: Is there any RFC that describes why do we need to convert the hash always to a "positive" unsigned byte array. I mean even if the number produced after the digest call is negative it is still a valid hash, right? thus why do we need that additional procedure. Basically, I am looking for a paper: standard or rfc describing that we need to do so.

推荐答案

散列由八位字节字符串(在 Java 中称为字节数组)组成.如何将其转换为大数(Java 中的 BigInteger)完全超出了加密哈希算法的范围.所以不,没有 RFC 来描述它,因为(通常)没有理由将散列视为数字.从这个意义上说,加密哈希与 Object.hashCode() 有很大不同.

A hash consists of an octet string (called a byte array in Java). How you convert it to or from a large number (a BigInteger in Java) is completely out of the scope for cryptographic hash algorithms. So no, there is no RFC to describe it as there is (usually) no reason to treat a hash as a number. In that sense a cryptographic hash is rather different from Object.hashCode().

您只能将十六进制视为无符号是一个问题,但是如果您真的想要,那么您可以先将其转换回字节数组,然后执行 new BigInteger(result).该构造函数确实威胁到已签名的结果中的编码.请注意,在协议中,通常不需要来回转换为十六进制;十六进制主要是供人使用的,电脑用字节没问题.

That you can only treat hexadecimals as unsigned is a bit of an issue, but if you really want to then you can first convert it back to a byte array, and then perform new BigInteger(result). That constructor does threat the encoding within result as signed. Note that in protocols it is often not needed to convert back and forth to hexadecimals; hexadecimals are mainly for human consumption, a computer is fine with bytes.

这篇关于在 Java 中创建哈希的标准方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆