Rosetta Code Challenges - SHA-256

Tell us what’s happening:

function SHA256(input) {
    async function sha256(input){
    const msgBuffer = new TextEncoder().encode(input);
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
    return hashHex;
}
sha256(input).then(hash => console.log(typeof hash));
}

why this code of mine is not passing the test cases despite getting the right solution.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Rosetta Code Challenges - SHA-256

Your outer function isn’t returning anything.

Also, it can’t return a promise.

1 Like
function SHA256(input) {
  let hashedval;
  async function sha256(input) {
    const msgBuffer = new TextEncoder().encode(input);
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
    return hashHex;
  }
  sha256(input).then(hash => hashedval = hash);

  return hashedval;
}

Can I do like this to solve this issue, (still the test cases are failing.)

No, that will return undefined (or whatever the initial value of hashedval is). That is just async code for you.

function SHA256(input) {
  let hashedval = "Initial value";
  async function sha256(input) {
    const msgBuffer = new TextEncoder().encode(input);
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
    const hashArray = Array.from(new Uint8Array(hashBuffer));
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
    return hashHex;
  }
  sha256(input).then(hash => hashedval = hash);

  return hashedval;
}

console.log(SHA256("Rosetta code")) // "Initial value"
1 Like

Ok Now I understood. btw thank you so much for the help.

I don’t think this is how you are meant to solve this challenge. Using the built-in API is really not much different from copying code. On the other hand, I’m sure plenty of people have solved this challenge by copying code. The algorithm isn’t exactly that easy to come up with.

1 Like

I know, like I saw the hint of the question. The code is too long. I tried to understand how it works ( didn’t understand it fully but, I tried ) eventually I will ask someone or AI to help me understand it by breaking it into smaller chunks.

Hi @myselfprincee

Did you succeed in understanding the exercise and the code? Let us know if you still have questions. Is there a specific aspect about how the code works that you would like to discuss? Probably something that you learned after consulting other sources like AI? Could you maybe share valuable prompts for other people to use?

And also if you can mention what prompted you to go directly to the solution? What do you think that made it difficult for you to start the exercise by yourself in the first place?