Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraVault/PasswordQuality.swift8// Module : Qyvora Vault9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Coarse password-strength estimate for the Quantum Shield. It is a guide for the user, not a guarantee:44/// the entropy figure is an upper-bound heuristic (length × alphabet size) reduced by penalties for the patterns45/// attackers try first. The password itself is never stored or logged.46public enum PasswordQuality {47 public enum Level: Int, Sendable, Comparable, Codable, CaseIterable {48 case veryWeak = 0, weak, fair, strong, veryStrong4950 public static func < (a: Level, b: Level) -> Bool { a.rawValue < b.rawValue }51 }5253 public enum Warning: String, Sendable, Codable, CaseIterable {54 case tooShort55 case singleCharacterClass56 case commonPassword57 case repeatedCharacters58 case sequentialCharacters59 case keyboardPattern60 case yearOrDate61 case mostlyDigits62 }6364 public struct Estimate: Sendable, Equatable, Codable {65 /// Estimated guessing entropy in bits (heuristic).66 public let entropyBits: Double67 public let level: Level68 public let warnings: [Warning]69 public let length: Int70 /// Lower case, upper case, digits, symbols/other: how many of the four are present.71 public let characterClasses: Int72 }7374 static let common: Set<String> = [75 "password", "passw0rd", "p@ssw0rd", "123456", "1234567", "12345678", "123456789", "1234567890", "qwerty", "qwertyuiop",76 "azerty", "abc123", "letmein", "welcome", "admin", "iloveyou", "monkey", "dragon", "football", "baseball", "master",77 "sunshine", "princess", "shadow", "superman", "michael", "trustno1", "bitcoin", "ethereum", "wallet", "crypto",78 "satoshi", "qyvora", "secret", "changeme", "motdepasse", "soleil", "bonjour", "111111", "000000", "654321", "696969",79 ]8081 static let keyboardRows = ["qwertyuiop", "asdfghjkl", "zxcvbnm", "azertyuiop", "qsdfghjklm", "1234567890"]8283 public static func estimate(_ password: String) -> Estimate {84 let scalars = Array(password.unicodeScalars)85 let length = scalars.count86 guard length > 0 else {87 return Estimate(entropyBits: 0, level: .veryWeak, warnings: [.tooShort], length: 0, characterClasses: 0)88 }89 var lower = false, upper = false, digit = false, other = false, wide = false90 var digitCount = 091 for s in scalars {92 switch s.value {93 case 0x61...0x7a: lower = true94 case 0x41...0x5a: upper = true95 case 0x30...0x39:96 digit = true97 digitCount += 198 case 0x20...0x7e: other = true99 default: wide = true100 }101 }102 var alphabet = 0.0103 if lower { alphabet += 26 }104 if upper { alphabet += 26 }105 if digit { alphabet += 10 }106 if other { alphabet += 33 }107 if wide { alphabet += 100 }108 let classes = [lower, upper, digit, other || wide].filter { $0 }.count109110 var bits = Double(length) * log2(alphabet)111 var warnings: [Warning] = []112 let folded = password.lowercased()113 let foldedScalars = Array(folded.unicodeScalars)114115 if length < 10 { warnings.append(.tooShort) }116 if classes == 1 { warnings.append(.singleCharacterClass) }117118 // a common word, alone or decorated with a few digits or symbols119 let core = String(String.UnicodeScalarView(foldedScalars.filter { (0x61...0x7a).contains($0.value) }))120 if common.contains(folded) || (core.count >= 5 && common.contains(core)) || common.contains(where: { $0.count >= 6 && folded.contains($0) }) {121 warnings.append(.commonPassword)122 bits = Swift.min(bits, 10 + Double(Swift.max(0, length - core.count)) * 3)123 }124125 // distinct characters: "aaaaaaaa" or "abababab" carry little information126 let distinct = Set(foldedScalars).count127 if length >= 4, Double(distinct) <= Double(length) * 0.4 {128 warnings.append(.repeatedCharacters)129 bits *= Double(distinct) / Double(length) + 0.15130 }131132 // runs such as "abcd", "4321"133 var longestRun = 1, run = 1134 if length >= 2 {135 for i in 1..<length {136 let d = Int(foldedScalars[i].value) - Int(foldedScalars[i - 1].value)137 let previous = i >= 2 ? Int(foldedScalars[i - 1].value) - Int(foldedScalars[i - 2].value) : d138 run = abs(d) == 1 && (run == 1 || d == previous) ? run + 1 : 1139 longestRun = Swift.max(longestRun, run)140 }141 }142 if longestRun >= 4 {143 warnings.append(.sequentialCharacters)144 bits -= Double(longestRun) * log2(alphabet) * 0.7145 }146147 // keyboard walks of four keys or more148 var walk = 0149 for row in keyboardRows {150 let reversed = String(row.reversed())151 for size in stride(from: Swift.min(row.count, length), through: 4, by: -1) where size > walk {152 for start in 0...(row.count - size) {153 let a = row.dropFirst(start).prefix(size), b = reversed.dropFirst(start).prefix(size)154 if folded.contains(a) || folded.contains(b) { walk = size }155 }156 }157 }158 if walk >= 4, longestRun < 4 || walk > longestRun {159 warnings.append(.keyboardPattern)160 bits -= Double(walk) * log2(alphabet) * 0.7161 }162163 // years 1900–2099164 if folded.range(of: "(19|20)[0-9]{2}", options: .regularExpression) != nil {165 warnings.append(.yearOrDate)166 bits -= 6167 }168 if digitCount * 10 >= length * 8, classes > 1 || length < 14 {169 warnings.append(.mostlyDigits)170 }171172 bits = Swift.max(0, bits)173 let level: Level174 switch bits {175 case ..<28: level = .veryWeak176 case ..<45: level = .weak177 case ..<64: level = .fair178 case ..<90: level = .strong179 default: level = .veryStrong180 }181 return Estimate(entropyBits: (bits * 10).rounded() / 10, level: level, warnings: warnings, length: length, characterClasses: classes)182 }183}184