UNPKG

1.49 kBTypeScriptView Raw
1/// <reference types="node" />
2import type { BinaryToTextEncoding } from 'crypto';
3import type { SorterOptions } from './objectSorter';
4/**
5 * Object hasher options
6 */
7export interface HasherOptions extends SorterOptions {
8 /**
9 * Hash algorithm to use
10 * @default 'sha256'
11 */
12 alg?: string;
13 /**
14 * String encoding for hash
15 * @default 'hex'
16 */
17 enc?: BinaryToTextEncoding;
18}
19/**
20 * If object implements Hashable interface then value from toHash
21 * will be used for hash function. It means that the different objects
22 * with the function toHash that return the same value will have the same hash
23 */
24export interface Hashable {
25 toHashableString: () => string;
26}
27export interface Hasher<T = unknown> {
28 /**
29 * Create hash of an object
30 * @param object source object
31 * @returns hash string of an object
32 */
33 hash(object: Hashable | T, opts?: HasherOptions): string;
34 /**
35 * Create sorted string from an object
36 * @param object source object
37 * @returns sorted string from an object
38 */
39 sort(object: T): string;
40 /**
41 * Create sorted string from an object
42 * @param object source object
43 * @returns sorted string from an object
44 * @alias sort
45 */
46 sortObject(object: T): string;
47}
48/**
49 * Hasher constructor
50 * @param options hasher options
51 * @return hasher instance
52 */
53export declare const hasher: (options?: HasherOptions) => Hasher;
54//# sourceMappingURL=hasher.d.ts.map
\No newline at end of file