title: How can I hash a string with SHA256 in JS?
body: <h1>Description</h1>
  <p>I'm looking to hash a string locally with SHA256 in Javascript.
  I've been looking around thinking there would be some sort of official library or function,
  but all I found were loads of different projects, each with different scripts, and I'm not so sure scripts to trust (as I'm not an expert and definitely not qualified to evaluate them) or how to implement them.
  <strong>EDIT:</strong> I need the output in text, not hexes, sorry if I didn't explain that when posting the original question.</p>
  <h1>Code</h1>
  <p>Here's what I've tried so far:</p>
  <pre><code>async function sha256(message) {
    // encode as UTF-8
    const msgBuffer = new TextEncoder('utf-8').encode(message);
  
    // hash the message
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
  
    // convert ArrayBuffer to Array
    const hashArray = Array.from(new Uint8Array(hashBuffer));
  
    // convert bytes to hex string
    const hashHex = hashArray.map(b =&gt; ('00' + b.toString(16)).slice(-2)).join('');
    console.log(hashHex);
    return hashHex;
  }
  sha256(passwordInput); 
  </code></pre>
  <h1>Console Output:</h1>
  <blockquote>
  <p>Uncaught (in promise) TypeError: Cannot read property 'digest' of undefined</p>
  </blockquote>
  <p>I'm new to javascript and I'm open to all suggestions, so yeah.</p>
  <h1>Update</h1>
  <p>Although most of your suggestions work, for those of you who are looking to use the Web Crypto API, the answer was on line #5. I needed to change <code>crypto.subtle.digest</code> to <code>window.crypto.subtle.digest</code></p>