Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!