/
sha.ts
76 lines (57 loc) · 1.46 KB
/
sha.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
import shajs from "sha.js";
export interface HashFunction {
readonly blockSize: number;
readonly update: (_: Uint8Array) => HashFunction;
readonly digest: () => Uint8Array;
}
export class Sha1 implements HashFunction {
public readonly blockSize = 512 / 8;
private readonly impl: any;
public constructor(firstData?: Uint8Array) {
this.impl = shajs("sha1");
if (firstData) {
this.update(firstData);
}
}
public update(data: Uint8Array): Sha1 {
this.impl.update(data);
return this;
}
public digest(): Uint8Array {
return new Uint8Array(this.impl.digest());
}
}
export class Sha256 implements HashFunction {
public readonly blockSize = 512 / 8;
private readonly impl: any;
public constructor(firstData?: Uint8Array) {
this.impl = shajs("sha256");
if (firstData) {
this.update(firstData);
}
}
public update(data: Uint8Array): Sha256 {
this.impl.update(data);
return this;
}
public digest(): Uint8Array {
return new Uint8Array(this.impl.digest());
}
}
export class Sha512 implements HashFunction {
public readonly blockSize = 1024 / 8;
private readonly impl: any;
public constructor(firstData?: Uint8Array) {
this.impl = shajs("sha512");
if (firstData) {
this.update(firstData);
}
}
public update(data: Uint8Array): Sha512 {
this.impl.update(data);
return this;
}
public digest(): Uint8Array {
return new Uint8Array(this.impl.digest());
}
}