-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsalt.ts
41 lines (34 loc) · 941 Bytes
/
salt.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
import { bytes } from "./deps.ts";
import { SALT_LENGTH } from "./const.ts";
/**
* SaltLengthError is thrown when salt length is of the wrong size.
*/
export class SaltLengthError extends Error {}
/**
* Salt define crypto salt used for HTTP Encryption Coding.
*/
export class Salt extends Uint8Array {
constructor(salt?: Uint8Array | ArrayBuffer) {
if (salt === undefined) {
super(SALT_LENGTH);
crypto.getRandomValues(this);
return;
}
if (salt.byteLength != SALT_LENGTH) {
throw new SaltLengthError(
`salt length must be ${SALT_LENGTH}: got ${salt.byteLength}`,
);
}
if (salt instanceof ArrayBuffer) {
const copy = new Uint8Array(SALT_LENGTH);
bytes.copy(new Uint8Array(salt), copy);
super(copy);
return;
}
super(SALT_LENGTH);
this.set(salt, 0);
}
public equals(other: Salt): boolean {
return bytes.equals(this, other);
}
}