2017-12-09 27 views
0
$ echo -e 'blob 14\0Hello, World!' | shasum 

가 생산하는 같은 hashe 생성하지 않는 : JS이 실행 8ab686eafeb1f44702738c8b0f24f2567c36da6d자식 shasum 및 노드 SHA1이

을/노드 :

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)

답변

5

입력 내용의 개행 문자의 차이로 인해 해시가 동일하지 않습니다. echo 줄 바꿈을 추가합니다. 대신 printf 사용

printf 'blob 14\0Hello, World!' | shasum 
# prints: d4fcfe4d339d4e59d168fdf3c0ad445fa980a7d6 
이 너무 작동

있지만 같은 휴대용를 echo의 플래그가 예상 지원되지 않기 때문에 모든 시스템에서 :

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