Cryptographic Hash Functions like SHA256 are one-way functions. This means that if you have the input, it's relatively trivial to find the output. On the other hand, if you have the output, it is infeasible to find the input.
However, if you knew the hashes of some common inputs, then you could brute-force guess at the output or create a Rainbow Table to determine what that input is.
It's easy to find that the SHA256 hash of "apples" is 0xf5903f...0f74d9
. If this was a likely input, a hacker could search for it specifically and know that the input was "apples"! 😱
⚠️ For security purposes, it's important to remember to use a random salt which you can add to your input to make it unguessable via the methods mentioned above!
Given a SHA256 hash, find the color input that would generate that hash. You can assume that all the hashes be generated only from colors provided in the COLORS
array.
- To take the hash of a color, first use
utf8ToBytes
to translate the string to bytes. Then, usesha256
to hash it. - When you want to compare two hashes, first use toHex to turn each hash from a Uint8Array to a string of hexadecimal characters.
So comparing two hashes would look like this:
const a = "apple";
const b = "banana";
const aBytes = utf8ToBytes(a);
const bBytes = utf8ToBytes(b);
const aHash = sha256(aBytes);
const bHash = sha256(bBytes);
console.log(toHex(aHash) === toHex(aHash)); // true
console.log(toHex(aHash) === toHex(bHash)); // false
📖 Wondering what utf8 stands for? The UTF-8 standard translates all the possible keyboard characters you can think of into bytes. This is an agreed upon standard to ensure we all get the same bit values representing the letters and words we see on the screen. Learn more about utf8 here.
-
Fork this repository to your GitHub account.
-
Clone your forked repository to your local machine:
git clone https://github.com/nitk-surathkal-unidao/week-1.git cd week-1
-
Create a new branch for your implementation:
git checkout -b <your_name>_solution_week1
-
Install the required dependencies:
npm install
-
Open the
index.js
file and implement thefindColor
function. This is where you'll write your solution. -
To run the tests:
npm test
-
Keep modifying your implementation until all tests pass.
-
After implementing your solution and ensuring all tests pass, commit your changes:
git add . git commit -m "Implement findColor function"
-
Push your branch to your forked repository:
git push origin <your_name>_solution_week1
-
Go to your forked repository on GitHub and create a new pull request from your
<your_name>_solution_week1
branch to the original repository's main branch. -
In the pull request description, briefly explain your approach and any challenges you faced.
- Use the
utf8ToBytes
function to convert each color string to bytes. - Use the
sha256
function to hash the bytes. - Use the
toHex
function to convert the hash to a hexadecimal string for comparison. - Iterate through the
COLORS
array, hash each color, and compare it with the given hash. - Return the matching color when found.
- Git Branching - Basic Branching and Merging
- Creating a pull request from a fork
- JavaScript Array methods
Good luck!