Password Complexity Script

Hi Guys,

i’ve been an avid reader of these forums for my scripting needs, but this time by searching foo has let me down.

Please help with the below.

I’ve created an applescript to check ask the user for a password, when entered it makes sure the password is more than 8 characters & asks for them to verify.

The next step is to make sure the password also contains 2 of the 3: capital letter, number & special character (i.e @!)

not sure how to proceed, i’ve tried creating a list & comparing but that’s failing for me… any help would be greatly appreciated… especially by 1pm est

set userPassword1 to ""
set userPassword2 to ""
tell application "Finder"
	repeat while userPassword1 = ""
		display dialog "Please enter your password below." & "

The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." buttons {"OK"} default answer "" with hidden answer
		set userPassword1 to text returned of result
		if length of userPassword1 < 8 then
			display dialog "Password needs to be a minimum of 8 characters." buttons {"OK"} default answer "" with hidden answer
			set userPassword1 to text returned of result
		else
			exit repeat
		end if
	end repeat
end tell

tell application "Finder"
	display dialog "Please verify your password below." buttons {"OK"} default answer "" with hidden answer
	set userPassword2 to text returned of result
	considering case
		repeat while userPassword2 is not equal to userPassword1
			display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with hidden answer
			set userPassword2 to text returned of result
		end repeat
	end considering
	-- for testing only
	display dialog userPassword1 & userPassword2
end tell




Ok… Looks like the issue is comparing a string against a list… hmm…

This works when appended to above script:

if userPassword1 contains "a" then
	display dialog "yep"
else
	display dialog "nope"
end if

This doesn’t (it gives a boolean -1700 error):

if userPassword1 contains "a" or "b" then
	display dialog "yep"
else
	display dialog "nope"
end if

Ideally, the script should check to see if any 2 of the following are present & if so proceed. (i’m guessing once i get this part working i can add a number to each for loop with a number of 2 or greater being ok.

{“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”, “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “0”, “`”, “~”, “!”, “@”, “#”, “$”, “%”, “^”, “&”, “*”, “(”, “)”, “_”, “+”, “-”, “=”, “[”, “]”, “>”, “?”, “;”, “:”}

ok… getting lost now :frowning:

i can’t get the below to add +1 for each matched character… doh!

set userpassword1 to "Password1"
set passwordComplexity to 0 as integer

repeat with userPasswordChar in userpassword1
	set userPasswordChar to userPasswordChar as text
	if userPasswordChar contains {{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}} then
		passwordComplexity + 1
	else if userPasswordChar contains {{"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":"}} then
		passwordComplexity + 1
		display dialog passwordComplexity
	else if userPasswordChar contains {{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}} then
		passwordComplexity + 1
		display dialog passwordComplexity
		
	else if userPasswordChar = 0 then
		display dialog "nope"
	end if
end repeat
return passwordComplexity
display dialog passwordComplexity

got it…

set userpassword1 to "PASSword1789"
set passwordComplexity to 0
set passwordAlpha to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set passwordSpecial to {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":"}
set passwordNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

repeat with userPasswordChar in userpassword1
	set userPasswordChar to userPasswordChar as text
	--display dialog userPasswordChar
	considering case
		if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
			set passwordComplexity to passwordComplexity + 1
		end if
	end considering
end repeat
return passwordComplexity
display dialog passwordComplexity

Try this amended version of your script


set warning_msg to ""
repeat
	set userpassword1 to text returned of (display dialog "Please enter a password" & warning_msg default answer "" with hidden answer)
	set passwordComplexity to 0
	considering case
		repeat with userPasswordChar in userpassword1
			set userPasswordChar to contents of userPasswordChar
			if {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"} contains userPasswordChar then
				set passwordComplexity to passwordComplexity + 1
			end if
		end repeat
	end considering
	if passwordComplexity is greater than 1 then
		exit repeat
	else
		set warning_msg to " (password must contain at least 2 special characters)"
	end if
end repeat

Thanks Ian… i managed to get the below to work… but what i now can’t do is to get it to keep repeating until conditions are met…

at the moment, if a user enters “test” they will go through all prompts…
hmmm…


set UserPassword to ""
set userPassword2 to ""
set passwordComplexity to 0
set passwordAlpha to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set passwordSpecial to {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":"}
set passwordNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

tell application "Finder"
	repeat while UserPassword = ""
		display dialog "Please enter your password below." & "

The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		if length of UserPassword < 8 then
			display dialog "Password needs to be a minimum of 8 characters." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		else
			exit repeat
		end if
	end repeat
	repeat with userPasswordChar in UserPassword
		set userPasswordChar to userPasswordChar as text
		considering case
			if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
				set passwordComplexity to passwordComplexity + 1
			end if
		end considering
	end repeat
	repeat
		if passwordComplexity < 2 then
			display dialog "Password does not meet security requirements." & "
	
The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." & "

Please retry." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
			exit repeat
		else
			repeat with userPasswordChar in UserPassword
				set userPasswordChar to userPasswordChar as text
				considering case
					if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
						set passwordComplexity to passwordComplexity + 1
					end if
				end considering
			end repeat
		end if
	end repeat
	display dialog "Please verify your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
	set userPassword2 to text returned of result
	considering case
		repeat while userPassword2 is not equal to UserPassword
			display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		end repeat
	end considering
end tell

this is working much better…

just need to somehow get it to check complexity before display dialog (just after subroutine… i.e subroutine 1st then dialog…)…


set UserPassword to ""
set userPassword2 to ""
set passwordComplexity to ""

tell application "Finder"
	repeat while UserPassword = ""
		display dialog "Please enter your password below." & "

The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		repeat while length of UserPassword < 8
			display dialog "Password needs to be a minimum of 8 characters." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		end repeat
	end repeat
end tell

tell me to passwordCheck(UserPassword, 0)

tell application "Finder"
	if passwordComplexity < 2 then
		display dialog "Password does not meet security requirements." & "
	
The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." & "

Please retry." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		tell me to passwordCheck(UserPassword, 0)
	end if
	display dialog "Please verify your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
	set userPassword2 to text returned of result
	considering case
		repeat while userPassword2 is not equal to UserPassword
			display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		end repeat
	end considering
end tell

on passwordCheck(UserPassword, passwordComplexity)
	set passwordComplexity to 0
	set passwordAlpha to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	set passwordSpecial to {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":"}
	set passwordNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	
	repeat with userPasswordChar in UserPassword
		set userPasswordChar to userPasswordChar as text
		considering case
			if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
				set passwordComplexity to passwordComplexity + 1
			end if
		end considering
	end repeat
end passwordCheck

this works… except the last repeat loop:

set UserPassword to ""
set userPassword2 to ""
set passwordComplexity to 0
set passwordAlpha to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set passwordSpecial to {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":"}
set passwordNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

tell application "Finder"
	repeat while UserPassword = ""
		display dialog "Please enter your password below." & "

The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		repeat while length of UserPassword < 8
			display dialog "Password needs to be a minimum of 8 characters." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		end repeat
	end repeat
end tell

tell application "Finder"
	repeat with userPasswordChar in UserPassword
		set userPasswordChar to userPasswordChar as text
		considering case
			if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
				set passwordComplexity to passwordComplexity + 1
			end if
		end considering
	end repeat
	repeat while passwordComplexity < 2
		display dialog "Password does not meet security requirements." & "
	
The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." & "

Please retry." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		repeat with userPasswordChar in UserPassword
			set userPasswordChar to userPasswordChar as text
			considering case
				if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
					set passwordComplexity to passwordComplexity + 1
				end if
			end considering
		end repeat
		
	end repeat
	display dialog "Please verify your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
	set userPassword2 to text returned of result
	considering case
		repeat while userPassword2 is not equal to UserPassword
			display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		end repeat
	end considering
end tell

nvm just seen what i was doing wrong… now to put this into a bash script… yipeee!

set UserPassword to ""
set userPassword2 to ""
set passwordComplexity to 0
set passwordAlpha to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set passwordSpecial to {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", ">", "?", ";", ":"}
set passwordNumbers to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

tell application "Finder"
	repeat while UserPassword = ""
		display dialog "Please enter your password below." & "

The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		repeat while length of UserPassword < 8
			display dialog "Password needs to be a minimum of 8 characters." buttons {"OK"} default answer "" with icon stop with hidden answer
			set UserPassword to text returned of result
		end repeat
	end repeat
end tell

tell application "Finder"
	repeat with userPasswordChar in UserPassword
		set userPasswordChar to userPasswordChar as text
		considering case
			if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
				set passwordComplexity to passwordComplexity + 1
			end if
		end considering
	end repeat
	repeat while passwordComplexity < 2
		display dialog "Password does not meet security requirements." & "
	
The password needs to be a minimum of 8 characters in length & must contain at least 2 of the following 3. A capital letter, number or special character." & "

Please retry." buttons {"OK"} default answer "" with icon stop with hidden answer
		set UserPassword to text returned of result
		repeat with userPasswordChar in UserPassword
			set userPasswordChar to userPasswordChar as text
			considering case
				if userPasswordChar is in passwordAlpha or (userPasswordChar is in passwordSpecial) or (userPasswordChar is in passwordNumbers) then
					set passwordComplexity to passwordComplexity + 1
				end if
			end considering
		end repeat
		
	end repeat
	display dialog "Please verify your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
	set userPassword2 to text returned of result
	considering case
		repeat while userPassword2 is not equal to UserPassword
			display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with icon stop with hidden answer
			set userPassword2 to text returned of result
		end repeat
	end considering
end tell

Your dialog says it must have 2 of the three. You mean that you need at least two different types. Well your check doesn’t work this way. When a password “aaaaAAAA” will also pass your test. Maybe this can help you

checkPassword("aaaaAAAA!")

on checkPassword(thePassword)
	set CapitalCharFlag to false
	set specialCharFlag to false
	set numberCharFlag to false
	
	repeat with theChar in thePassword
		if not CapitalCharFlag then set CapitalCharFlag to isCharCaptial(theChar)
		if not specialCharFlag then set specialCharFlag to isCharSpecial(theChar)
		if not numberCharFlag then set numberCharFlag to isCharNumber(theChar)
		if CapitalCharFlag and specialCharFlag then return true
		if CapitalCharFlag and numberCharFlag then return true
		if specialCharFlag and numberCharFlag then return true
	end repeat
	
	return false
end checkPassword

on isCharCaptial(theChar)
	considering case
		return ((offset of theChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") is not equal to 0) as boolean
	end considering
end isCharCaptial

on isCharSpecial(theChar)
	return ((offset of theChar in "±!@#$%^&*()(){}[]:\";'\\|/?,.<>~`") is not equal to 0) as boolean
end isCharSpecial

on isCharNumber(theChar)
	return ((offset of theChar in "0123456789") is not equal to 0) as boolean
end isCharNumber