2020-11-15 01:42:36 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { createHash, createHmac } from "crypto";
|
|
|
|
|
2022-07-14 04:29:36 +03:00
|
|
|
import hash from "hash.js";
|
|
|
|
|
2020-11-15 01:42:36 +03:00
|
|
|
import { arrayify, BytesLike } from "@ethersproject/bytes";
|
|
|
|
|
|
|
|
import { SupportedAlgorithm } from "./types";
|
|
|
|
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
|
|
|
export function ripemd160(data: BytesLike): string {
|
2022-07-14 04:29:36 +03:00
|
|
|
return "0x" + (hash.ripemd160().update(arrayify(data)).digest("hex"));
|
2020-11-15 01:42:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|