@tylix/auth 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -6
- package/src/password/hashPassword.js +80 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tylix/auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tylix",
|
|
@@ -12,9 +12,11 @@
|
|
|
12
12
|
"test": "node --test src/**/*.test.js"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@tylix/
|
|
16
|
-
"@tylix/
|
|
17
|
-
"@tylix/
|
|
18
|
-
|
|
15
|
+
"@tylix/generator": "^0.6.0",
|
|
16
|
+
"@tylix/orm": "^0.6.0",
|
|
17
|
+
"@tylix/shared": "^0.6.0"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@tylix/core": "^0.6.0"
|
|
19
21
|
}
|
|
20
|
-
}
|
|
22
|
+
}
|
|
@@ -6,15 +6,82 @@ import {
|
|
|
6
6
|
import { promisify } from 'node:util'
|
|
7
7
|
|
|
8
8
|
const scrypt = promisify(scryptCallback)
|
|
9
|
+
|
|
9
10
|
const KEY_LENGTH = 64
|
|
11
|
+
const PREFIX = 'scrypt'
|
|
12
|
+
|
|
13
|
+
// New hashes: N=2^15, r=8, p=3 (about 32 MiB per hash, an OWASP-recommended
|
|
14
|
+
// scrypt setting). The parameters are stored inside each hash string, so they
|
|
15
|
+
// can be raised later without breaking existing hashes.
|
|
16
|
+
const COST = 1 << 15
|
|
17
|
+
const BLOCK_SIZE = 8
|
|
18
|
+
const PARALLELISM = 3
|
|
19
|
+
|
|
20
|
+
// Sanity limits when reading parameters back from storage, so a corrupted or
|
|
21
|
+
// tampered row can't make login allocate gigabytes.
|
|
22
|
+
const MAX_COST = 1 << 20
|
|
23
|
+
const MAX_BLOCK_SIZE = 32
|
|
24
|
+
const MAX_PARALLELISM = 16
|
|
25
|
+
const MAX_KEY_LENGTH = 128
|
|
26
|
+
|
|
27
|
+
function maxmemFor(cost, blockSize, parallelism) {
|
|
28
|
+
return 128 * blockSize * (cost + parallelism + 2) * 2
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isPowerOfTwo(value) {
|
|
32
|
+
return Number.isInteger(value) && value > 1 && (value & (value - 1)) === 0
|
|
33
|
+
}
|
|
10
34
|
|
|
11
35
|
export async function hashPassword(plainPassword) {
|
|
12
36
|
const salt = randomBytes(16).toString('hex')
|
|
13
|
-
const derivedKey = await scrypt(plainPassword, salt, KEY_LENGTH
|
|
14
|
-
|
|
37
|
+
const derivedKey = await scrypt(plainPassword, salt, KEY_LENGTH, {
|
|
38
|
+
N: COST,
|
|
39
|
+
r: BLOCK_SIZE,
|
|
40
|
+
p: PARALLELISM,
|
|
41
|
+
maxmem: maxmemFor(COST, BLOCK_SIZE, PARALLELISM),
|
|
42
|
+
})
|
|
43
|
+
return `${PREFIX}$${COST}$${BLOCK_SIZE}$${PARALLELISM}$${salt}$${derivedKey.toString('hex')}`
|
|
15
44
|
}
|
|
16
45
|
|
|
17
|
-
|
|
46
|
+
async function verifyCurrent(plainPassword, storedHash) {
|
|
47
|
+
const [, n, r, p, salt, key] = storedHash.split('$')
|
|
48
|
+
const cost = Number(n)
|
|
49
|
+
const blockSize = Number(r)
|
|
50
|
+
const parallelism = Number(p)
|
|
51
|
+
|
|
52
|
+
if (
|
|
53
|
+
!salt ||
|
|
54
|
+
!key ||
|
|
55
|
+
!isPowerOfTwo(cost) ||
|
|
56
|
+
cost > MAX_COST ||
|
|
57
|
+
!Number.isInteger(blockSize) ||
|
|
58
|
+
blockSize < 1 ||
|
|
59
|
+
blockSize > MAX_BLOCK_SIZE ||
|
|
60
|
+
!Number.isInteger(parallelism) ||
|
|
61
|
+
parallelism < 1 ||
|
|
62
|
+
parallelism > MAX_PARALLELISM
|
|
63
|
+
) {
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const keyBuffer = Buffer.from(key, 'hex')
|
|
68
|
+
if (keyBuffer.length === 0 || keyBuffer.length > MAX_KEY_LENGTH) return false
|
|
69
|
+
|
|
70
|
+
const derivedKey = await scrypt(plainPassword, salt, keyBuffer.length, {
|
|
71
|
+
N: cost,
|
|
72
|
+
r: blockSize,
|
|
73
|
+
p: parallelism,
|
|
74
|
+
maxmem: maxmemFor(cost, blockSize, parallelism),
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
if (derivedKey.length !== keyBuffer.length) return false
|
|
78
|
+
return timingSafeEqual(derivedKey, keyBuffer)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Hashes created before parameters were stored: "salt:key", Node's default
|
|
82
|
+
// scrypt settings. Still accepted so nobody is locked out; the login flow
|
|
83
|
+
// upgrades them to the current format after a successful login.
|
|
84
|
+
async function verifyLegacy(plainPassword, storedHash) {
|
|
18
85
|
const [salt, key] = storedHash.split(':')
|
|
19
86
|
if (!salt || !key) return false
|
|
20
87
|
|
|
@@ -28,3 +95,13 @@ export async function verifyPassword(plainPassword, storedHash) {
|
|
|
28
95
|
|
|
29
96
|
return timingSafeEqual(derivedKey, keyBuffer)
|
|
30
97
|
}
|
|
98
|
+
|
|
99
|
+
export async function verifyPassword(plainPassword, storedHash) {
|
|
100
|
+
if (typeof storedHash !== 'string' || storedHash.length === 0) return false
|
|
101
|
+
|
|
102
|
+
if (storedHash.startsWith(`${PREFIX}$`)) {
|
|
103
|
+
return verifyCurrent(plainPassword, storedHash)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return verifyLegacy(plainPassword, storedHash)
|
|
107
|
+
}
|