【问题标题】:git shasum and node sha1 do not produce the same hashegit shasum 和 node sha1 不会产生相同的哈希
【发布时间】:2018-05-23 13:22:32
【问题描述】:
$ echo -e 'blob 14\0Hello, World!' | shasum

产生:8ab686eafeb1f44702738c8b0f24f2567c36da6d

在 js/node 中运行:

var sha1 = require('sha1');

const fileContents = "Hello, World!";
const length = fileContents.length + 1;

const blobString = `blob ${length}\0${fileContents}`;

const hash = sha1(blobString);

console.log(blobString);
console.log(hash);

产生:

blob 14Hello, World!
d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6

为什么哈希不相等? (8ab686eafeb1f44702738c8b0f24f2567c36da6d != d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6)

【问题讨论】:

    标签: javascript node.js git md5 sha


    【解决方案1】:

    由于输入中换行符的不同,哈希值不相等。 echo 添加一个换行符。请改用printf

    printf 'blob 14\0Hello, World!' | shasum
    # prints: d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6
    

    这也有效,但不那么可移植,因为并非所有系统都支持echo 的标志:

    echo -ne 'blob 14\0Hello, World!' | shasum
    

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 2013-12-20
      • 2012-02-05
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      相关资源
      最近更新 更多