algorithm for calculate sha256 from string in typescript

To calculate the SHA256 hash of a string in TypeScript, you can use the crypto module provided by Node.js. Here is an example function that takes a string input and returns its SHA256 hash:

import crypto from 'crypto';

function sha256(input: string): string {
  const hash = crypto.createHash('sha256');
  return hash.digest('hex');
168 chars
8 lines

Here's how to use the sha256 function to calculate the hash of a string:

const input = 'hello world';
const hash = sha256(input);
console.log(hash); // Output: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
154 chars
4 lines

Note that the crypto module is only available in Node.js, so this code won't work in a web browser without using a third-party library.

gistlibby LogSnag