-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathindex.ts
37 lines (25 loc) · 1.19 KB
/
index.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
"use strict";
import { createHash, createHmac } from 'crypto';
import { arrayify, BytesLike } from '@ethersproject/bytes';
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
export enum SupportedAlgorithm { sha256 = "sha256", sha512 = "sha512" };
export function ripemd160(data: BytesLike): string {
return "0x" + createHash("ripemd160").update(Buffer.from(arrayify(data))).digest("hex")
}
export function sha256(data: BytesLike): string {
return "0x" + createHash("sha256").update(Buffer.from(arrayify(data))).digest("hex")
}
export function sha512(data: BytesLike): string {
return "0x" + createHash("sha512").update(Buffer.from(arrayify(data))).digest("hex")
}
export function computeHmac(algorithm: SupportedAlgorithm, key: BytesLike, data: BytesLike): string {
if (!SupportedAlgorithm[algorithm]) {
logger.throwError("unsupported algorithm - " + algorithm, Logger.errors.UNSUPPORTED_OPERATION, {
operation: "computeHmac",
algorithm: algorithm
});
}
return "0x" + createHmac(algorithm, Buffer.from(arrayify(key))).update(Buffer.from(arrayify(data))).digest("hex");
}