Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit b504cc5

Browse files
committed
Add explaination of PBKDF2 options and use SHA256 instead of SHA1
1 parent 5a1f8d9 commit b504cc5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

nodecg-io-core/extension/persistenceManager.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,26 @@ export function deriveEncryptionKey(password: string, salt: string): string {
9595

9696
return crypto
9797
.PBKDF2(password, saltWordArray, {
98+
// Generate a 256 bit long key for AES-256.
9899
keySize: 256 / 32,
100+
// Iterations should ideally be as high as possible.
101+
// OWASP recommends 310.000 iterations for PBKDF2 with SHA-256 [https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2].
102+
// The problem that we have here is that this is run inside the browser
103+
// and we must use the JavaScript implementation which is slow.
104+
// There is the SubtleCrypto API in browsers that is implemented in native code inside the browser and can use cryptographic CPU extensions.
105+
// However SubtleCrypto is only available in secure contexts (https) so we cannot use it
106+
// because nodecg-io should be usable on e.g. raspberry pi on a local trusted network.
107+
// So were left with only 5000 iterations which were determined
108+
// by checking how many iterations are possible on a AMD Ryzen 5 1600 in a single second
109+
// which should be acceptable time for logging in. Slower CPUs will take longer,
110+
// so I didn't want to increase this any further.
111+
112+
// For comparison: the crypto.js internal key generation function that was used in nodecg.io <0.3 configs
113+
// used PBKDF1 based on a single MD5 iteration (yes, that is really the default in crypto.js...).
114+
// So this is still a big improvement in comparison to the old config format.
99115
iterations: 5000,
116+
// Use SHA-256 as the hashing algorithm. crypto.js defaults to SHA-1 which is less secure.
117+
hasher: crypto.algo.SHA256,
100118
})
101119
.toString(crypto.enc.Hex);
102120
}

0 commit comments

Comments
 (0)