SHA1 Hash on Hex String

为君一笑 提交于 2019-12-04 14:01:06
gleber

First convert your hex into byte[] using for example this: Convert a string representation of a hex dump to a byte array using Java?

After it use

byte[] data = hexStringToByteArray(hexData);
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(data, 0, data.length);
byte[] sha1hash = md.digest();
Jon Skeet

Basically you just need to find a hex parser - there are plenty around, with one example here, or Apache Commons Codec for this and other conversions. While Java bytes are indeed signed, you'll get the same bit pattern as if they were unsigned, so they'll have the same way. Unless you're performing your own arithmetic/bit-shifting on byte values, you can usually ignore the fact that bytes are signed in Java.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!