Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
SHA256 on Terminal vs. Node.js giving two different hashes

I want to get a SHA256 Hash that gives me the same result as I get on the terminal

echo -n "hello world" | shasum -0 -a 256

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

I tried two different scripts

const {Sha256} = require('@aws-crypto/sha256-js');

(async () => {
    const hash = new Sha256();
    hash.update('hello world');
    const result = await hash.digest();
    let hex = Buffer.from(result).toString('hex');
    console.log(hex);
})()

And

var hash = require('hash.js')

console.log(hash.sha256().update('hello world').digest('hex'))

They both give me the hash b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

How can I get the same hash from the shasum in my terminal?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you ask the man page of shasum you will see;

-0, --01          read in BITS mode
                           ASCII '0' interpreted as 0-bit,
                           ASCII '1' interpreted as 1-bit,
                           all other characters ignored

That reads only 0s and 1s from the file ( pipe in your case). Now remove the parameter -0 or better define the default

-t, --text read in text mode (default)

$ echo -n "hello world" | shasum -t -a 256 | cut -c -64 b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Now we have the same output.

Note 1: that the cut is used to remove the space and dash in the end.

Not 2 : NIST has test vector in Cryptographic Algorithm Validation Program and SHA-256 value of empty string (length 0) is given as in the file SHA256ShortMsg.rsp;

Len = 0
Msg = 00
MD = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Note 3: For the NIST's hash function, one should not rely on the third-party result. They need to look at NIST vectors.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda