This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.ts
135 lines (122 loc) · 3.62 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {createHash, randomBytes, type BinaryLike} from 'node:crypto';
import type {CryptoAlgorithm, CryptoEncoding} from './type.js';
/**
* Represents the configuration for a hash generator.
*/
export interface HashGeneratorConfig {
/**
* The prefix to be added to the generated hash.
*/
prefix: string;
/**
* The algorithm used for hashing.
*/
algorithm: CryptoAlgorithm;
/**
* The encoding used for the generated hash.
*/
encoding: CryptoEncoding;
/**
* The length of the CRC (Cyclic Redundancy Check) value.
*/
crcLength: number;
}
/**
* Secure **self-validate** hash generator.
*/
export class AlwatrHashGenerator {
/**
* Creates a new instance of the AlwatrHashGenerator class.
* @param config The configuration for the hash generator.
*/
constructor(public config: HashGeneratorConfig) {}
/**
* Generate a random hash.
* @returns The generated hash.
* @example
* ```typescript
* const clientId = hashGenerator.generateRandom();
* ```
*/
generateRandom(): string {
return this.generate(randomBytes(16));
}
/**
* Generate a **self-validate** random hash.
* @returns The generated self-validated hash.
* @example
* ```typescript
* const userId = hashGenerator.generateRandomSelfValidate();
* ```
*/
generateRandomSelfValidate(): string {
return this.generateSelfValidate(randomBytes(16));
}
/**
* Generate a hash from data.
* @param data - The data to generate the hash from.
* @returns The generated hash.
* @example
* ```typescript
* const crcHash = hashGenerator.generate(data);
* ```
*/
generate(data: BinaryLike): string {
return this.config.prefix + createHash(this.config.algorithm).update(data).digest(this.config.encoding);
}
/**
* Generate a crc hash.
* @param data - The data to generate the crc hash from.
* @returns The generated crc hash.
*/
generateCrc(data: BinaryLike): string {
const crc = createHash('sha1').update(data).digest(this.config.encoding);
return this.config.crcLength == null || this.config.crcLength < 1 ? crc : crc.slice(0, this.config.crcLength);
}
/**
* Generate a **self-validate** hash from data.
* @param data - The data to generate the self-validated hash from.
* @returns The generated self-validated hash.
* @example
* ```typescript
* const userId = hashGenerator.generateSelfValidate(data);
* ```
*/
generateSelfValidate(data: BinaryLike): string {
const mainHash = this.generate(data);
const crcHash = this.generateCrc(mainHash);
return mainHash + crcHash;
}
/**
* Verify if the generated hash matches the provided hash.
* @param data - The data to verify.
* @param hash - The hash to compare against.
* @returns `true` if the hash is verified, `false` otherwise.
* @example
* ```typescript
* if (!hashGenerator.verify(data, hash)) {
* new Error('data_corrupted');
* }
* ```
*/
verify(data: BinaryLike, hash: string): boolean {
return hash === this.generate(data);
}
/**
* Verify a **self-validate** hash to check if it was generated by this class (with the same options).
* @param hash - The self-validated hash to verify.
* @returns `true` if the hash is verified, `false` otherwise.
* @example
* ```typescript
* if (!hashGenerator.verifySelfValidate(hash)) {
* new Error('invalid_hash');
* }
* ```
*/
verifySelfValidate(hash: string): boolean {
const gapPos = hash.length - this.config.crcLength;
const mainHash = hash.slice(0, gapPos);
const crcHash = hash.slice(gapPos);
return crcHash === this.generateCrc(mainHash);
}
}