create a sha512 hash from a string in typescript

To create a SHA512 hash from a string in TypeScript, you can use the createHash function from the built-in crypto module of Node.js. Here's an example code:

index.ts
import crypto from 'crypto';

function sha512(input: string): string {
  const hash = crypto.createHash('sha512');
  hash.update(input);
  return hash.digest('hex');
}

const input = 'your string to hash';
const output = sha512(input);
console.log(output); // prints the hash value
282 chars
12 lines

In this code, we define a function sha512 that takes a string input, creates a SHA512 hash object using crypto.createHash('sha512'), updates the hash with the input using hash.update(input), and finally returns the hexadecimal representation of the hash using hash.digest('hex').

To use this function, simply call it with your input string (in this case, 'your string to hash') and store the output hash value in a variable (in this case, output). You can then print or use the hash value as needed.

gistlibby LogSnag