LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2013, 10:45 PM   #16
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,339

Rep: Reputation: Disabled

Password recommendations deserve a thread of their own, but the consensus the last 10 or so years has been that an 8 character password is reasonably safe if you don't use a dictionary word and mix characters from three of these four groups:
  • lower case letters
  • upper case letters
  • numbers
  • special characters (punctuation etc)
This was not really a good recommendation, and lead to people using rubbish passwords (typically years appended to events that happened that year, or dictionary words with common letter-number substitutions) that they still kept forgetting but automated scripts had a field day with.

Personally, I would recommend raising the minimum length to something like 15 or 20 characters, and teach users how to generate strong passwords that are also easy to remember. And make sure they understand the risks associated with password reuse as well.
 
Old 07-31-2013, 10:45 PM   #17
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by Lucien Lachance View Post
No, I have the user enter a password size they would like to generate. This number can range from 8-64. However, if this exceeds these bounds continue to ask the user again and then ask for the size of password again. $1 in my helper function represents the input of the size they would like to generate. I print the password by saying:
Code:
printf "%s\n "${my_password[*]:0:length}"
Does that make sense?
So the input is a number that should be between 8 and 64, inclusive. Why not
Code:
if [ $((length > 7))) && $((length < 65)) ]
then
  printf "%s\n "${my_password[*]:0:length}"
  return 0
else
  echo "Your password length must be between 8 and 64, inclusive. Please try again." > 2
  return 1
fi
 
Old 08-01-2013, 12:24 AM   #18
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Using regex would give more internal processing which is slower and is difficult to reconfigure whereas using simpler conditional arithmetic expressions would be more flexible e.g.
Code:
is_valid() {
    [[ $1 -ge 8 && $1 -le 64 ]]
}
# or
is_valid() {
    (( $1 >= 8 && $1 <= 64 ))
}
Or
Code:
MIN_PASSWORD_LENGTH=8   ## can be placed inside the function instead
MAX_PASSWORD_LENGTH=64  ##
is_valid() {
    [[ $1 -ge MIN_PASSWORD_LENGTH && $1 -le MAX_PASSWORD_LENGTH ]]
}
And as for checking password strings directly this could be one form:
Code:
is_password_valid() {
    PASSWORD_LENGTH=${#1} MIN_PASSWORD_LENGTH=8 MAX_PASSWORD_LENGTH=64
    [[ PASSWORD_LENGTH -ge MIN_PASSWORD_LENGTH && PASSWORD_LENGTH -le MAX_PASSWORD_LENGTH ]]
}
You could even add other checks like:
Code:
shopt -s extglob

is_password_valid() {
    PASSWORD=$1 PASSWORD_LENGTH=${#1} MIN_PASSWORD_LENGTH=8 MAX_PASSWORD_LENGTH=64
    [[ (PASSWORD_LENGTH -ge MIN_PASSWORD_LENGTH && PASSWORD_LENGTH -le MAX_PASSWORD_LENGTH) && $PASSWORD == *+([[:alpha:]])* && $PASSWORD == *+([[:digit:]])* && $PASSWORD != +([^[:print:]]) ]] ## Password should contain letters and numbers, and could only have printable characters.
}

Last edited by konsolebox; 08-01-2013 at 12:40 AM. Reason: && not ||
 
Old 08-01-2013, 01:22 AM   #19
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
Appreciate the answer konsolebox, I was having some difficulty testing certain values with regex. I tried doing

Code:
if [[ $length =~ ^[8-9]|1[0-5]$
When I entered the value 90 or 99, the password would print and have a length of 90 characters (when it should catch that error), I didn't run into any of complications with the conditional logic. Is there any reason why you wrote the conditional logic with &&. I know it's a matter of preference, I'm just always used to writing || and wanted to know your answer. Also, could you explain the bash internals and why regex would negatively influence the internals?

Last edited by Lucien Lachance; 08-01-2013 at 01:24 AM.
 
Old 08-01-2013, 01:28 AM   #20
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
^([8-9]|1[0-5])$

note the ()
 
Old 08-01-2013, 01:37 AM   #21
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by Lucien Lachance View Post
Is there any reason why you wrote the conditional logic with &&. I know it's a matter of preference, I'm just always used to writing || and wanted to know your answer. Also, could you explain the bash internals and why regex would negatively influence the internals?

the reason for [[ $1 -ge 8 && $1 -le 64 ]]

is when $1 is greater than or equal to 8 AND less than or equal to 64, it returns TRUE (0)

which is handy if you want to know if your password 'fits'
if you do it the other way round (( <= 8 || >= 64 )) , you get FALSE (1) when your password is good, and TRUE when not.. which makes no sense
 
Old 08-01-2013, 07:29 AM   #22
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by Lucien Lachance View Post
Is there any reason why you wrote the conditional logic with &&. I know it's a matter of preference
Not exactly just about preference. With ||, you would need to negate and use the opposite form:
Code:
[[ ! ($1 -lt 8 || $1 -gt 64) ]]
If an input is invalid or if another condition or error makes the
($1 -lt 8 || $1 -gt 64) invalid like if $1 is not an integer, would make the condition valid which is not something that you would expect whereas in [[ $1 -ge 8 && $1 -le 64 ]] anything that would make the condition valid is only what you would really just expect.

And you can't do [[ $1 -ge 8 || $1 -le 64 ]] since apparently that would make any integer number valid.

Quote:
Also, could you explain the bash internals and why regex would negatively influence the internals?
I don't really know how Bash implements it but just imagine it. Commonly with regex a sequence of logic implementers are generated out of the regex string and is used to parse the input or the other operand string, which is clearly a heavy task compared to just evaluating the value of the two integral operands and use a numerical comparison with it. If you know assembly and basic numerical instructions compared to string manipulation mechanisms you would understand what I mean.

Last edited by konsolebox; 08-01-2013 at 07:30 AM.
 
Old 08-01-2013, 08:24 AM   #23
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
I understand, thanks for the feedback guys. I now have a better understanding of this problem and learned quite a bit from both of your answers.
 
  


Reply

Tags
bash, linux, regex, unix



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash if regex porphyry5 Programming 6 06-24-2011 08:20 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
[SOLVED] More Bash regex help please... arashi256 Linux - Newbie 7 09-21-2010 07:18 PM
Need help with bash + regex onesikgypo Programming 2 01-10-2009 10:08 AM
Regex in bash? BeholdMyGlory Linux - Newbie 4 01-10-2009 08:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration