Skip to content

Commit 66ddd95

Browse files
committed
feat: add getPasswordMethod & test for it
1 parent 2e0c230 commit 66ddd95

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/node/util.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ export const isHashLegacyMatch = (password: string, hashPassword: string) => {
162162
return safeCompare(hashedWithLegacy, hashPassword)
163163
}
164164

165+
const passwordMethods = ["SHA256", "ARGON2", "PLAIN_TEXT"] as const
166+
export type PasswordMethod = typeof passwordMethods[number]
167+
168+
/**
169+
* Used to determine the password method.
170+
*
171+
* There are three options for the return value:
172+
* 1. "SHA256" -> the legacy hashing algorithm
173+
* 2. "ARGON2" -> the newest hashing algorithm
174+
* 3. "PLAIN_TEXT" -> regular ol' password with no hashing
175+
*
176+
* @returns {PasswordMethod} "SHA256" | "ARGON2" | "PLAIN_TEXT"
177+
*/
178+
export function getPasswordMethod(hashedPassword: string | undefined): PasswordMethod {
179+
if (!hashedPassword) {
180+
return "PLAIN_TEXT"
181+
}
182+
183+
// This is the new hashing algorithm
184+
if (hashedPassword.includes("$argon")) {
185+
return "ARGON2"
186+
}
187+
188+
// This is the legacy hashing algorithm
189+
return "SHA256"
190+
}
191+
165192
const mimeTypes: { [key: string]: string } = {
166193
".aac": "audio/x-aac",
167194
".avi": "video/x-msvideo",

test/unit/node/util.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { hash, isHashMatch, hashLegacy, isHashLegacyMatch } from "../../../src/node/util"
1+
import {
2+
hash,
3+
isHashMatch,
4+
PasswordMethod,
5+
getPasswordMethod,
6+
hashLegacy,
7+
isHashLegacyMatch,
8+
} from "../../../src/node/util"
29

310
describe("getEnvPaths", () => {
411
describe("on darwin", () => {
@@ -203,3 +210,25 @@ describe("isHashLegacyMatch", () => {
203210
expect(isHashLegacyMatch(password, _hash)).toBe(true)
204211
})
205212
})
213+
214+
describe("getPasswordMethod", () => {
215+
it("should return PLAIN_TEXT for no hashed password", () => {
216+
const hashedPassword = undefined
217+
const passwordMethod = getPasswordMethod(hashedPassword)
218+
const expected: PasswordMethod = "PLAIN_TEXT"
219+
expect(passwordMethod).toEqual(expected)
220+
})
221+
it("should return ARGON2 for password with 'argon2'", () => {
222+
const hashedPassword =
223+
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
224+
const passwordMethod = getPasswordMethod(hashedPassword)
225+
const expected: PasswordMethod = "ARGON2"
226+
expect(passwordMethod).toEqual(expected)
227+
})
228+
it("should return SHA256 for password with legacy hash", () => {
229+
const hashedPassword = "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af"
230+
const passwordMethod = getPasswordMethod(hashedPassword)
231+
const expected: PasswordMethod = "SHA256"
232+
expect(passwordMethod).toEqual(expected)
233+
})
234+
})

0 commit comments

Comments
 (0)