Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
52 lines
1.5 KiB
Plaintext
52 lines
1.5 KiB
Plaintext
import { distance } from 'fastest-levenshtein'
|
|
import { RankedDictionary } from '../types'
|
|
|
|
const getUsedThreshold = (
|
|
password: string,
|
|
entry: string,
|
|
threshold: number,
|
|
) => {
|
|
const isPasswordToShort = password.length <= entry.length
|
|
const isThresholdLongerThanPassword = password.length <= threshold
|
|
const shouldUsePasswordLength =
|
|
isPasswordToShort || isThresholdLongerThanPassword
|
|
|
|
// if password is too small use the password length divided by 4 while the threshold needs to be at least 1
|
|
return shouldUsePasswordLength ? Math.ceil(password.length / 4) : threshold
|
|
}
|
|
|
|
export interface FindLevenshteinDistanceResult {
|
|
levenshteinDistance: number
|
|
levenshteinDistanceEntry: string
|
|
}
|
|
|
|
const findLevenshteinDistance = (
|
|
password: string,
|
|
rankedDictionary: RankedDictionary,
|
|
threshold: number,
|
|
): Partial<FindLevenshteinDistanceResult> => {
|
|
let foundDistance = 0
|
|
const found = Object.keys(rankedDictionary).find((entry) => {
|
|
const usedThreshold = getUsedThreshold(password, entry, threshold)
|
|
if (Math.abs(password.length - entry.length) > usedThreshold) {
|
|
return false
|
|
}
|
|
const foundEntryDistance = distance(password, entry)
|
|
const isInThreshold = foundEntryDistance <= usedThreshold
|
|
|
|
if (isInThreshold) {
|
|
foundDistance = foundEntryDistance
|
|
}
|
|
return isInThreshold
|
|
})
|
|
if (found) {
|
|
return {
|
|
levenshteinDistance: foundDistance,
|
|
levenshteinDistanceEntry: found,
|
|
}
|
|
}
|
|
return {}
|
|
}
|
|
|
|
export default findLevenshteinDistance
|