Skip to content

Commit 026a732

Browse files
fixed type errors
1 parent 9a4d5dd commit 026a732

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/lib/crypto-utils.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ const HASH_ALGORITHM = "SHA-256";
88

99
export async function encryptWhim(
1010
message: string,
11-
otp: string
11+
otp: string,
1212
): Promise<EncryptedWhim> {
1313
const encoder = new TextEncoder();
1414
const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
1515

1616
const key = await crypto.subtle.importKey(
1717
"raw",
1818
encoder.encode(otp),
19-
{ name: "PBKDF2" },
19+
{ name: KEY_ALGORITHM },
2020
false,
21-
["deriveKey"]
21+
["deriveKey"],
2222
);
2323

2424
const derivedKey = await crypto.subtle.deriveKey(
2525
{
26-
name: "PBKDF2",
26+
name: KEY_ALGORITHM,
2727
salt,
2828
iterations: 100000,
29-
hash: "SHA-256",
29+
hash: HASH_ALGORITHM,
3030
},
3131
key,
3232
{ name: CIPHER_ALGORITHM, length: KEY_LENGTH },
3333
false,
34-
["encrypt"]
34+
["encrypt"],
3535
);
3636

3737
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
@@ -42,7 +42,7 @@ export async function encryptWhim(
4242
iv,
4343
},
4444
derivedKey,
45-
encoder.encode(message)
45+
encoder.encode(message),
4646
);
4747

4848
return {
@@ -54,7 +54,7 @@ export async function encryptWhim(
5454

5555
export async function decryptWhim(
5656
encryptedWhim: EncryptedWhim,
57-
otp: string
57+
otp: string,
5858
): Promise<string> {
5959
const encoder = new TextEncoder();
6060
const decoder = new TextDecoder();
@@ -64,37 +64,37 @@ export async function decryptWhim(
6464
encoder.encode(otp),
6565
{ name: KEY_ALGORITHM },
6666
false,
67-
["deriveKey"]
67+
["deriveKey"],
6868
);
6969

7070
const derivedKey = await crypto.subtle.deriveKey(
7171
{
7272
name: KEY_ALGORITHM,
73-
salt: encryptedWhim.salt,
73+
salt: new Uint8Array(encryptedWhim.salt),
7474
iterations: ITERATIONS,
7575
hash: HASH_ALGORITHM,
7676
},
7777
key,
7878
{ name: CIPHER_ALGORITHM, length: KEY_LENGTH },
7979
false,
80-
["decrypt"]
80+
["decrypt"],
8181
);
8282

8383
const decryptedData = await crypto.subtle.decrypt(
8484
{
8585
name: CIPHER_ALGORITHM,
86-
iv: encryptedWhim.iv,
86+
iv: new Uint8Array(encryptedWhim.iv),
8787
},
8888
derivedKey,
89-
encryptedWhim.encryptedMessage
89+
new Uint8Array(encryptedWhim.encryptedMessage),
9090
);
9191

9292
return decoder.decode(decryptedData);
9393
}
9494

9595
export function generateOtp(): string {
9696
const randomValue = crypto.getRandomValues(new Uint32Array(1))[0];
97-
return (randomValue % 9000 + 1000).toString();
97+
return ((randomValue % 9000) + 1000).toString();
9898
}
9999

100100
type EncryptedWhim = {

0 commit comments

Comments
 (0)