create a sha1 hash from a string in typescript

To create a sha1 hash from a string in Typescript, you can use the crypto library that comes with Node.js. Here's an example function that takes a string as input and returns its sha1 hash:

index.ts
import { createHash } from 'crypto';

function sha1(input: string): string {
  const hash = createHash('sha1');
  hash.update(input);
  return hash.digest('hex');
}
165 chars
8 lines

Here's how you can use this function:

index.ts
const input = 'hello world';
const hash = sha1(input);
console.log(hash); // Output: 2ef7bde608ce5404e97d5f042f95f89f1c232871
126 chars
4 lines

Note that the output is a hexadecimal string. If you want to use the hash for encryption or security purposes, be sure to follow best practices for securely storing and transmitting the hash.

gistlibby LogSnag