File size: 997 Bytes
884908f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { argon2Verify, argon2id } from "hash-wasm";
import { addLogEntry } from "./logEntries";
import { getLastSearchTokenHash, updateLastSearchTokenHash } from "./pubSub";

export async function getSearchTokenHash() {
  const password = VITE_SEARCH_TOKEN;
  const lastSearchTokenHash = getLastSearchTokenHash();

  try {
    const lastSearchTokenHashIsValid = await argon2Verify({
      password,
      hash: lastSearchTokenHash,
    });

    if (lastSearchTokenHashIsValid) {
      addLogEntry("Using cached search token hash");
      return lastSearchTokenHash;
    }
  } catch (error) {
    void error;
  }

  const salt = new Uint8Array(16);
  crypto.getRandomValues(salt);

  const newSearchTokenHash = await argon2id({
    password,
    salt,
    parallelism: 1,
    iterations: 16,
    memorySize: 512,
    hashLength: 8,
    outputType: "encoded",
  });

  updateLastSearchTokenHash(newSearchTokenHash);

  addLogEntry("New search token hash generated");

  return newSearchTokenHash;
}