-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.ts
More file actions
43 lines (35 loc) · 929 Bytes
/
utils.ts
File metadata and controls
43 lines (35 loc) · 929 Bytes
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
export type CloudAuthUser = {
id: string;
email?: string;
} | null;
export type User = {
id: string;
username: string | null;
avatar: string;
};
export type Reactions = Record<string, Record<string, true>>;
export type Message = {
id: string;
text: string;
createdAt: number;
userId: string;
reactions?: Reactions;
};
export type ChatDocument = {
messages: Message[]
users: User[]
}
// Create an SHA256 hash of a string
export async function sha256(text: string) {
// Encode the text as a Uint8Array
const encoder = new TextEncoder();
const data = encoder.encode(text);
// Use the Web Crypto API to hash the data
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
// Convert the hash to a hex string
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
return hashHex;
}