MCPcopy
hub / github.com/streetwriters/notesnook / deriveKey

Method deriveKey

packages/crypto/src/keyutils.ts:36–64  ·  view source on GitHub ↗
(
    sodium: ISodium,
    password: string,
    salt?: string
  )

Source from the content-addressed store, hash-verified

34
35export default class KeyUtils {
36 static deriveKey(
37 sodium: ISodium,
38 password: string,
39 salt?: string
40 ): EncryptionKey {
41 let saltBytes: Uint8Array;
42 if (!salt)
43 saltBytes = sodium.randombytes_buf(sodium.crypto_pwhash_SALTBYTES);
44 else {
45 saltBytes = sodium.from_base64(salt);
46 }
47
48 if (!saltBytes)
49 throw new Error("Could not generate bytes from the given salt.");
50
51 const key = sodium.crypto_pwhash(
52 sodium.crypto_aead_xchacha20poly1305_ietf_KEYBYTES,
53 password,
54 saltBytes,
55 3, // operations limit
56 1024 * 1024 * 8, // memory limit (8MB)
57 sodium.crypto_pwhash_ALG_ARGON2I13
58 );
59
60 return {
61 key,
62 salt: typeof salt === "string" ? salt : sodium.to_base64(saltBytes)
63 };
64 }
65
66 static deriveKeyPair(sodium: ISodium): EncryptionKeyPair {
67 const keypair = sodium.crypto_box_keypair();

Callers 2

exportKeyMethod · 0.95
transformMethod · 0.95

Calls 4

randombytes_bufMethod · 0.65
from_base64Method · 0.65
crypto_pwhashMethod · 0.65
to_base64Method · 0.65

Tested by

no test coverage detected