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.
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.