Password Security Lesson in Playground

Checking for Characters


Now that you're eliminating the most common passwords, you can add some more sophisticated checks to your algorithm. A good policy is to disallow plain dictionary words. If you require the user to include nonalphabetic characters, there'll be some amount of randomness even if the password includes dictionary words (such as "apple1984"). You might also require passwords be a minimum length. The longer a password, the longer it takes for a hacker to try all possibilities. For example, there are only 140,608 simple six-letter passwords. In contrast, the number of 16-character passwords generated according to the rules below has an upper bound of over 30 nonillion, or 30 thousand billion billion billion! That's way too many for a hacker to perform an exhaustive search of possible passwords.

Use the following rules:


At least 16 characters

At least one regular letter

At least one digit

At least one punctuation character


let tenMostCommonPasswords = [

"123456",

"password",

"12345678",

"qwerty",

"12345",

"123456789",

"letmein",

"1234567",

"football",

"iloveyou"

]


let digits = "0123456789"

let punctuation = "!@#$%^&*(),.<>;'`~[]{}\\|/?_-+= "

Implement your updated algorithm below.


To implement your algorithm, you can treat the password as an array of characters. Use a for...in loop to examine all of its characters and to keep a count of digits and punctuation characters. (You can use the contains() method you used in ArraysAndLoops.playground to help with this.) After checking all the characters in the password, write a final conditional statement to check whether you found at least one of each type of required character.


How do I create the correct algorithm using a for..in loop and the contains() method?

this is what I have tried:

let password = "password"

password.count

func correctAuthenticationMethod(ofPassword: String) -> Bool {

for char in password {

if digits.contains(char) {

if punctuation.contains(char) {

return true

} else {

return false

}

} else {

return true

}

}

return false

}

correctAuthenticationMethod(ofPassword: password)


I appreciate any help with this issue 🙂

Post not yet marked as solved Up vote post of Roxy2020 Down vote post of Roxy2020
1.7k views

Replies

Hello Roxy,


This code works for me. Please take a look and tell me what you think, maybe we can improve it.

Waiting for the reply!


Best regards!


let tenMostCommonPasswords = [

"123456",

"password",

"12345678",

"qwerty",

"12345",

"123456789",

"letmein",

"1234567",

"football",

"iloveyou"

]

let digits = "0123456789"

let punctuation = "!@#$%^&*(),.<>;'`~[]{}\\|/?_-+= "

let password = "Football1!sdnsaifinfeanf239"



func doesPasswordHaveUppercase() -> Bool {

for char in password {

if char.isUppercase {

return true

}

}

return false

}

func doesPasswordHaveDigits() -> Bool {

for char in digits {

if password.contains(char) {

return true

}

}

return false

}


func doesYourPasswordHavePunctuation() -> Bool {

for n in punctuation {

if password.contains(n) {

return true

}

}

return false

}


func choosePaswword() -> String {

for pass in tenMostCommonPasswords {

if tenMostCommonPasswords.contains(pass) {

if doesPasswordHaveDigits() == false {

if doesYourPasswordHavePunctuation() == false {

return "Your password: \(password) should not to be one of this passwords: \(tenMostCommonPasswords)"

}

}

}

}

if doesPasswordHaveDigits() == true && doesYourPasswordHavePunctuation() == true && password.count > 16 && doesPasswordHaveUppercase() == true {

return "Your password is strong enough"

}

return "Your password: \(password) need to contain at least one digit, at least one punctuation character, at least one regular letter and needs to have more then 16 characters"

}

choosePaswword()

Hi flaamb,


Thank you for posting your solution! It seems to work just fine. I'm a little confused about what prints to the console from the functions with ...Uppercase, ...Digits, and ...Punctuation when I say print(char) and print(n). I tried putting different passwords in and it either prints 0 or 1, 11 times, and only shows one punctuation and one capital letter. Do you know why that's the case?

this is what I put in the playground:

let tenMostCommonPasswords = [

"123456",

"password",

"12345678",

"qwerty",

"12345",

"123456789",

"letmein",

"1234567",

"football",

"iloveyou"

]

let digits = "0123456789"

let punctuation = "!@#$%^&*(),.<>;'`~[]{}\\|/?_-+= "

let password = "Football1!sdnsaifinfeanf239"



func doesPasswordHaveUppercase() -> Bool {

for char in password {

if char.isUppercase {

print(char)

return true

}

}

return false

}


func doesPasswordHaveDigits() -> Bool {

for char in digits {

if password.contains(char) {

print(char)

return true

}

}

return false

}


func doesYourPasswordHavePunctuation() -> Bool {

for n in punctuation {

if password.contains(n) {

print(n)

return true

}

}

return false

}


func choosePassword() -> String {

for pass in tenMostCommonPasswords {

if tenMostCommonPasswords.contains(pass) {

if doesPasswordHaveDigits() == false {

if doesYourPasswordHavePunctuation() == false {

return "Your password: \(password) should not to be one of this passwords: \(tenMostCommonPasswords)"

}

}

}

}

if doesPasswordHaveDigits() == true && doesYourPasswordHavePunctuation() == true && password.count >= 16 && doesPasswordHaveUppercase() == true {

return "Your password is strong enough: \(password)"

}

return "Your password: \(password) need to contain at least one digit, at least one punctuation character, at least one regular letter and needs to have more then 16 characters"

}


choosePassword()


..and this is the output in the console:

1

1

1

1

1

1

1

1

1

1

1

!

F


Thanks again!

The print is not necesary, I was cheking to see if the function works. You need to delete the lines from those function wich contain "print(n) or print(char).