题目位置:
官方题目解答:
这题确实没有什么可以讲的, 主要是利用hash表来确定密码是否是单词. 我的第13个检查就是把字符串变成数组, 利用hash表来看每一个字符出现了几次, 如果是第二次出现, ?{} 就会为真, 改对象就会沿着管道进入到%{$dup = $true}中, 也就标记了该密码有重复字符.
param([string]$password)
$script:score = 13;
$rating = "weak","weak","weak","weak","weak","weak","weak",`
"moderately-strong","moderately-strong","moderately-strong","moderately-strong",`
"strong", "strong", "strong"
# 0: prepare ...
$hash = @{}
$chars = @{}
$dup = $false
Get-Content -Path C:\Scripts\wordlist.txt | % { $hash[$_] = $null }
# 1
if ($hash.ContainsKey($password)) {$script:score--; "Password is an actual word"}
# 2
if ($hash.ContainsKey($password.Remove($password.Length-1,1))) {$script:score--; "Password, minus the last letter, is an actual word."}
# 3
if ($hash.ContainsKey($password.Remove(0,1))) {$script:score--; "Password, minus the first letter, is an actual word."}
# 4
if ($hash.ContainsKey(($password -match '0') -and ($password -replace '0','o'))) {$script:score--; "Password simply substitute 0 (zero) for the letter o."}
# 5
if ($hash.ContainsKey(($password -match '1') -and ($password -replace '1','l'))) {$script:score--; "Password simply substitute 1 (one) for the letter l."}
# 6
if ($password.Length -lt 10) {$script:score--; "password is less than 10 characters"}
if ($password.Length -gt 20) {$script:score--; "password is more than 20 characters"}
# 7
if ($password -notmatch '\d') {$script:score--; "No number in password."}
# 8
if ($password -cnotmatch '[A-Z]') {$script:score--; "No uppercase letter in password."}
# 9
if ($password -cnotmatch '[a-z]') {$script:score--; "No lowercase letter in password."}
# 10
if ($password -cnotmatch '[^0-9a-zA-Z]') {$script:score--; "No symbol in password."}
#11
if ($password -cmatch '[a-z]{4,}') {$script:score--; "Four or more consecutive lowercase letters in password."}
#12
if ($password -cmatch '[A-Z]{4,}') {$script:score--; "Four or more consecutive uppercase letters in password."}
#13
$password.ToCharArray() | ? { [bool] $chars["$_"]++ } | % {$dup = $true}
if ($dup) {$script:score--; "Duplicate letters in password.";}
# result
""
"A password score of $script:score indicates a $($rating[$script:score]) password."
阅读(1030) | 评论(0) | 转发(0) |