[英]two of three byte different in sha256 hash function
我正在使用此函數來計算SHA 256
public static String getSHA1(String plainText) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
md.update(plainText.getBytes(Charset.forName("UTF-8")));
StringBuffer hexString = new StringBuffer();
byte[] bytes = md.digest();
for (int i = 0; i < bytes.length; i++) {
hexString.append(Integer.toHexString(0xFF & bytes[i]));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
為了確保我的結果,我檢查了此在線網站http://onlinemd5.com/
我的代碼和在線代碼之間的結果幾乎相同,但是您知道它必須equal
。 例如
1234567
8BB0CF6EB9B17D0F7D22B456F121257DC1254E1F01665370476383EA776DF414
8bb0cf6eb9b17df7d22b456f121257dc1254e1f1665370476383ea776df414
這是更多示例:
7777777
8C1CDB9CB4DBAC6DBB6EBD118EC8F9523D22E4E4CB8CC9DF5F7E1E499BBA3C10
8c1cdb9cb4dbac6dbb6ebd118ec8f9523d22e4e4cb8cc9df5f7e1e499bba3c10
147258
7A2EC40FF8A1247C532309355F798A779E00ACFF579C63EEC3636FFB2902C1AC
7a2ec4ff8a1247c53239355f798a779e0acff579c63eec3636ffb292c1ac
888888
92925488B28AB12584AC8FCAA8A27A0F497B2C62940C8F4FBC8EF19EBC87C43E
92925488b28ab12584ac8fcaa8a27af497b2c6294c8f4fbc8ef19ebc87c43e
我確實知道這maybe
與編碼有關。 但看起來我用的是utf-8
,這是該網站使用的
這就是問題:
hexString.append(Integer.toHexString(0xFF & bytes[i]));
這將丟失任何前導0-換句話說,小於16的任何字節將以一個十六進制數字而不是兩個十六進制數字出現。
有很多修復程序。 例如:
0
(以ick為單位) String.format("%02x", bytes[i] & 0xff)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.