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

241
Visualizações
Why do I have to use both .toString() and JSON.stringify()?
const SHA256 = require("crypto-js/sha256");

// the possible colors that the hash could represent
const COLORS = ['red', 'green', 'blue', 'yellow', 'pink', 'orange'];

// given a hash, return the color that created the hash
function findColor(hash) {
    let hashedColor;

    COLORS.forEach(color => {
        let hashedColor = SHA256(JSON.stringify(color)).toString();
        console.log(hashedColor);
        if (hashedColor === hash) {
            return color;
        };
    });
};


module.exports = findColor;

I'm experimenting with some hashing and manually using a rainbow table. When I import the SHA256 method, I find that I can create a hash for each of the colors but that this returns an object, which I then need to .stringify() to be able to console.log() the actual hash.

My question is, why do I have to both JSON.stringify() and .toString()?

I think I don't quite understand what data type the SHA256 function gives me, and I can't find it anywhere.

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

0

JSON.stringify() is serializing the value of color as JSON. This is required in most cases because your hash library can only do strings (or probably buffers too, but not just native objects).

However, you already have strings, and thus have no need to serialize the strings as JSON. So, you can remove the extraneous JSON.stringify() here.

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