create a blake2s hash from a string in typescript

To create a Blake2s hash from a string in TypeScript, you can use the built-in crypto module available in Node.js. First, you need to install blake2s-js library which provides an implementation of Blake2s in JavaScript.

You can install this library using NPM:

$ npm install blake2s-js
25 chars
2 lines

Once you have installed the blake2s-js library, you can use the following code snippet to create a Blake2s hash from a string:

index.ts
import Blake2s from 'blake2s-js';

const str: string = 'The quick brown fox jumps over the lazy dog.';
const hash = Blake2s(str);

console.log(hash.toString('hex'));
166 chars
7 lines

Here, we first import the Blake2s class from the blake2s-js library. We then create a str variable with the string we want to hash. We then pass str as a parameter to the Blake2s constructor, which returns the hash as a Uint8Array. Finally, we convert the Uint8Array to a hexadecimal string using the toString() method.

The output of the above code snippet will be:

index.ts
f9cb6c16ae0f446aec041dbb18c7b66f98c6bd08b5c07dcc1db9610824259b59
65 chars
2 lines

which is the Blake2s hash of the input string.

gistlibby LogSnag