ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.