【问题标题】:Leading zeros when computing SHA-256 hash计算 SHA-256 哈希时的前导零
【发布时间】:2019-07-21 08:00:29
【问题描述】:

我正在尝试将同一文件的 SHA-256 哈希值与 Python 和 Java 进行比较。但是,在某些情况下,Python 哈希值有前导零,而 Java 版本没有。例如,在两个程序中散列 somefile.txt 会产生:

Python:000c3720cf1066fcde30876f498f060b0b3ad4e21abd473588f1f31f10fdd890

Java:c3720cf1066fcde30876f498f060b0b3ad4e21abd473588f1f31f10fdd890

简单地删除前导 0 并进行比较是否安全,或者是否有不产生前导零的实现?

Python 代码

def sha256sum(filename):
    h  = hashlib.sha256()
    b  = bytearray(128*1024)
    mv = memoryview(b)
    with open(filename, 'rb', buffering=0) as f:
        for n in iter(lambda : f.readinto(mv), 0):
            h.update(mv[:n])
    return h.hexdigest()

print(sha256sum('/somepath/somefile.txt'))

# 000c3720cf1066fcde30876f498f060b0b3ad4e21abd473588f1f31f10fdd890

Java 代码

public static String calculateSHA256(File updateFile) {
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Exception while getting digest", e);
        return null;
    }

    InputStream is;
    try {
        is = new FileInputStream(updateFile);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Exception while getting FileInputStream", e);
        return null;
    }

    byte[] buffer = new byte[8192];
    int read;
    try {
        while ((read = is.read(buffer)) > 0) {
            digest.update(buffer, 0, read);
        }
        byte[] shaSum = digest.digest();
        BigInteger bigInt = new BigInteger(1, shaSum);
        String output = bigInt.toString(16);
        return output;
    } catch (IOException e) {
        throw new RuntimeException("Unable to process file for SHA256", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(TAG, "Exception on closing SHA256 input stream", e);
        }
    }
}

Log.i("Output", calculateSHA256(somefile))

// I/Output: c3720cf1066fcde30876f498f060b0b3ad4e21abd473588f1f31f10fdd890

【问题讨论】:

  • 因为你使用的是BigInteger。我认为正是这一点剥离了前导零。

标签: java android python hash sha256


【解决方案1】:

BigInteger 转换忽略了 SHA-256 哈希中的前导零。相反,您应该直接编码byte[]。按照建议in this answer,您可以使用String.format()

StringBuilder sb = new StringBuilder();
for (byte b : shaSum) {
    sb.append(String.format("%02X", b));
}
return sb.toString();

当编码为十六进制字符串时,SHA-256 值有 64 个字符,根据 wiki example

SHA256("")

0x e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

【讨论】:

  • 请注意,“%02X”格式会将字符串大写(参见 Javadoc here)。如果要保留小写字母,请使用“%02x”。
猜你喜欢
  • 2012-06-15
  • 2014-08-19
  • 2023-03-27
  • 2010-12-17
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多