Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda