forked from bitjson/typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.ts
38 lines (36 loc) · 973 Bytes
/
hash.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createHash } from 'crypto';
import shaJs from 'sha.js';
/**
* Calculate the sha256 digest of a string.
*
* ### Example (es imports)
* ```js
* import { sha256 } from 'typescript-starter'
* sha256('test')
* // => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
* ```
*
* @returns sha256 message digest
*/
export function sha256(message: string): string {
return shaJs('sha256')
.update(message)
.digest('hex');
}
/**
* A faster implementation of [[sha256]] which requires the native Node.js module. Browser consumers should use [[sha256]], instead.
*
* ### Example (es imports)
* ```js
* import { sha256Native as sha256 } from 'typescript-starter'
* sha256('test')
* // => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
* ```
*
* @returns sha256 message digest
*/
export function sha256Native(message: string): string {
return createHash('sha256')
.update(message)
.digest('hex');
}