git 字符串的SHA1值给出不同的结果

wgx48brx  于 2023-09-29  发布在  Git
关注(0)|答案(1)|浏览(110)

我正在阅读关于对象存储的git文档,我试图理解摘要是如何创建的?https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
他们提到以下内容:

>> store = header + content
=> "blob 16\u0000what is up, doc?"
>> require 'digest/sha1'
=> true
>> sha1 = Digest::SHA1.hexdigest(store)
=> "bd9dbf5aae1a3862dd1526723246b20206e5fc37"

我试着用散列法“blob 16\u0000怎么了,医生?“在不同的在线工具中,但仍然没有相同的结果。我假设是空字节\u0000导致了这里的问题。
也尝试在main.c文件中测试它,但仍然不是相同的结果:

int main() {
    std::string store = "blob 16\u0000what is up, doc?"; // The input string

    unsigned char hash[SHA_DIGEST_LENGTH]; // SHA-1 produces a 20-byte (160-bit) hash

    // Calculate the SHA-1 hash
    SHA1(reinterpret_cast<const unsigned char*>(store.c_str()), store.length(), hash);

    // Convert the hash to a hexadecimal string
    char hash_str[2 * SHA_DIGEST_LENGTH + 1];
    for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
        sprintf(&hash_str[i * 2], "%02x", hash[i]);
    }
    hash_str[2 * SHA_DIGEST_LENGTH] = '\0';

    std::cout << "SHA-1 Hash: " << hash_str << std::endl;

    return 0;
}

我得到:

SHA-1 Hash: ef04a980b37021f6534130548617df7588eac70f

如何才能得到同样的结果?

pbgvytdp

pbgvytdp1#

$ printf 'blob 16\0what is up, doc?' | sha1sum
bd9dbf5aae1a3862dd1526723246b20206e5fc37  -
$

你会被不同语言对nul字节的处理所困扰。C++使用C字符串约定,字符串作为指针传递到第一个nul的第一个字节结束,但告诉它底层的char数组,它可以处理:

$ sh -x <<\---- 
cat <<\EOD >stwnul.cc
#include <string>
#include <iostream>
int main() {
        std::string foo("blob 16\u0000what is up, doc?");
        std::cout << foo.size() <<' ' <<foo <<'\n';
        const char data[] = "blob 16\u0000what is up, doc?";
        std::string bar(data,sizeof data -1);
        std::cout << bar.size() <<' ' <<bar <<'\n';
}
EOD
c++ -march=native -pipe -Os    stwnul.cc -o stwnul
./stwnul
----
+ cat
+ c++ -march=native -pipe -Os stwnul.cc -o stwnul
+ ./stwnul
7 blob 16
24 blob 16what is up, doc?
$

相关问题