1- import { Hash } from "crypto" ;
1+ import type { Hash } from "crypto" ;
22
33const baseEncodeTables = {
44 26 : "abcdefghijklmnopqrstuvwxyz" ,
@@ -11,16 +11,8 @@ const baseEncodeTables = {
1111 64 : "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_" ,
1212} ;
1313
14- type DigestTypes =
15- | "base26"
16- | "base32"
17- | "base36"
18- | "base49"
19- | "base52"
20- | "base58"
21- | "base62"
22- | "base64" ;
23- type BaseEncodings = 26 | 32 | 36 | 49 | 52 | 58 | 62 | 64 ;
14+ type BaseEncoding = keyof typeof baseEncodeTables ;
15+ type DigestType = `base${BaseEncoding } `;
2416
2517/**
2618 * @param {Uint32Array } uint32Array Treated as a long base-0x100000000 number, little endian
@@ -39,10 +31,10 @@ function divmod32(uint32Array: Uint32Array, divisor: number): number {
3931
4032function encodeBufferToBase (
4133 buffer : Buffer ,
42- base : BaseEncodings | number ,
34+ base : BaseEncoding ,
4335 length : number
4436) {
45- const encodeTable = baseEncodeTables [ base as keyof typeof baseEncodeTables ] ;
37+ const encodeTable = baseEncodeTables [ base ] ;
4638
4739 if ( ! encodeTable ) {
4840 throw new Error ( "Unknown encoding base" + base ) ;
@@ -78,7 +70,7 @@ let BulkUpdateDecorator: typeof import("./hash/BulkUpdateDecorator").default;
7870export default function getHashDigest (
7971 buffer : Buffer ,
8072 algorithm : string | "xxhash64" | "md4" | "native-md4" ,
81- digestType : DigestTypes | string ,
73+ digestType : DigestType | string ,
8274 maxLength : number
8375) {
8476 algorithm = algorithm || "xxhash64" ;
@@ -142,13 +134,11 @@ export default function getHashDigest(
142134 digestType === "base58" ||
143135 digestType === "base62"
144136 ) {
145- const digestTypeToDigest : number = digestType . substr (
146- 4
147- ) as unknown as number ;
137+ const digestTypeToDigest = Number ( digestType . substr ( 4 ) ) ;
148138
149139 return encodeBufferToBase (
150140 hash . digest ( ) as Buffer ,
151- digestTypeToDigest ,
141+ digestTypeToDigest as BaseEncoding ,
152142 maxLength
153143 ) ;
154144 } else {
0 commit comments