git shasum和节点sha1不会产生相同的哈希

时间:2017-12-09 13:07:23

标签: javascript node.js git md5 sha

class Superclass {
    Superclass() {
        this(0);
        System.out.println("1");
    }

    Superclass(int x) {
        System.out.println("2" + x);
    }
}

class Subclass extends Superclass {
    Subclass(int x) {
        System.out.println("3" + x);
    }

    Subclass(int x, int y) {
        System.out.println("4" + x + y);
    }
}

public class practiceTest {
    public static void main(String[] args) {
        Subclass s = new Subclass(10, 20);
    }
}

生成:$ echo -e 'blob 14\0Hello, World!' | shasum

在js / node中运行:

8ab686eafeb1f44702738c8b0f24f2567c36da6d

产生

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

1 个答案:

答案 0 :(得分:5)

由于输入中换行符的不同,散列不相等。 echo添加换行符。请改用printf

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

这也可以,但不是可移植的,因为echo的标记在所有系统中都不会被预测支持:

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