How to compute SHA-256 in Javascript synchronously?

Viewed 583

I have a vanilla javascript project that uses ClosureCompiler to generate a minified js file. This file is an SDK that people would include in their own websites. I need to compute a SHA-256 hash but its turning out to be trickier than it should be.

What I want:

const data = "test"
const hashedData = SHA256(data);

Requirements:

  1. Should be synchronous.
  2. Should work with vanilla javascript.
  3. Should compile with closure compiler.
  4. Should not require dependency on an external website. I am fine with copying the code from an external library into my own project. But it should be a trusted/reliable library like SJCL.

What I have tried:

Webcrypto.subtle: This returns a Promise, not a string. It would need me to make my SHA256 function async. Which would require a lot of re-architecture.

SJCL: Does not compile with Closure Compiler.

0 Answers
Related