Page 1 of 1

password generator

Posted: Thu 29 Sep 2011, 06:03
by jpeps
This generates a 12 char password using numbers/symbols/large and small cap letters.

Code: Select all

 #!/bin/sh

## Generates Passwords

x="0"  # length of password 
flag="0"  # limit number of symbols 
cflag="0" # limit number of digits

while ((x < 12)); do 
var="$(echo $RANDOM | cut -c 1-2)"

## get symbols 

if [ "$flag" -lt "2" ]; then 
   if [  "$var" -lt 27 -a  "$var" -gt 22 ]; then
       printf "\x${var}" 
           flag=$((flag + 1))
           x=$((x + 1)) 
   fi
fi
## get numbers

if [ "$cflag" -lt "3" ]; then 
   if [  "$var" -lt 40 -a  "$var" -gt 30 ]; then
       printf "\x${var}" 
           cflag=$((cflag + 1))
           x=$((x + 1)) 
   fi
fi

## get letters
if [ "$var" -gt 40 -a "$var" -lt 80 ]; then
       printf "\x${var}" 
              x=$((x + 1)) 
fi
done
echo "
"

Code: Select all

/mnt/sda2/Desktop $ password-generate 
BU7f!y1&&2wC

Posted: Thu 29 Sep 2011, 09:03
by GatorDog
Hi jpeps,

Very possible I'm missing something but if I'm reading you code right it won't be generating -
0 (zero)
J - O and Z (caps)
j - o and z (lower case)

For zero change
if [ "$var" -lt 40 -a "$var" -gt 30 ]; then
to
if [ "$var" -lt 40 -a "$var" -ge 30 ]; then

The 2-digit random numbers generated are decimal.

Code: Select all

var="$(echo $RANDOM | cut -c 1-2)" 
The hex digits A-F won't be generated and so the above mentioned letters won't be selected for duty.
---------------------
This will generate a 2-digit (1-byte) hex number:

Code: Select all

var=$(2>/dev/null dd if=/dev/urandom bs=1 count=1 |od -An -tx1)
But it might be better to generate a number less than 127, do the if/then
testing in decimal, then convert the selected number to hex.
I think this will generate a random number less than 127

Code: Select all

var=$(($RANDOM % 127))
Convert var to a hex number-

Code: Select all

var=$(printf "%x" $var)
Then again maybe I'm full of it and should leave well enough alone. :?
gDog

Posted: Thu 29 Sep 2011, 09:52
by vovchik
Hi guys,

I did something a bit similar once (but in BaCon) and it is here: http://208.109.22.214/puppy/viewtopic.p ... ef15d93b82

With kind regards,
vovchik

Posted: Thu 29 Sep 2011, 15:45
by jpeps
GatorDog wrote:
The 2-digit random numbers generated are decimal.

Code: Select all

var="$(echo $RANDOM | cut -c 1-2)" 
The hex digits A-F won't be generated and so the above mentioned letters won't be selected for duty.
The code for letters is below it, and gets large caps/small caps. Large caps are 41-59 (z excluded). You can see them in the sample printout.

I didn't bother z, (z is "5a", so I'd have to include a special line of code for it since the other letters are simple digits). Sometimes 0's can confused with O's, so I didn't bother with that either. There are also symbols I didn't include, but they could be added with additional code. Some get tricky when sending ftp, though, due to escape issues.

Hi vovchik....I'm too paranoid to use a password that I can read :)

Posted: Thu 29 Sep 2011, 21:25
by GatorDog

Code: Select all

Large caps are 41-59
Yes, but codes 41-59 are the hex codes. (41,42,43,44,45,46,47,48,49,4A,4B,4C,4D,4F, 50,51,52,53,54,55,56,57,58,59) for A thru Z

Code: Select all

var="$(echo $RANDOM | cut -c 1-2)" 
generates the decimal numbers 10-99; so the hex numbers 4A 4B 4C 4D 4E 4F (J-O) and 6A 6B 6C 6D 6E 6F (j-o) won't be generated.
You can see them in the sample printout.
BU7f!y1&&2wC
There are no J-O or j-o. There are the letters before and after, but none of those.

With the script as is, I don't think you'll ever see those characters.
Again I could be missing something; but I ran the script a number of times
and those letters didn't appear. (maybe a bad $RANDOM :? )
On the other hand, there is that Eastern philosophy that just because
something has never happened does not mean it will never happen :wink:

I mod'ed your code a little. This will include the upper/lower JKLMNO and Z

Code: Select all

#!/bin/sh
## Generates Passwords

x="0"  # length of password
flag="0"  # limit number of symbols
cflag="0" # limit number of digits
psword_len=12
lower=32
upper=123
range=$(($upper - $lower))

print_chr (){
	var_hex=$(printf "%x" $var) #covert decimal to hex number
	printf "\x${var_hex}"
	x=$((x + 1))
}

while ((x < $psword_len)); do
	var=$((($RANDOM % $range) + $lower))

	## get symbols
	if [ "$flag" -lt 3 -a "$var" -lt 39 -a "$var" -gt 32 ]; then
		print_chr
		flag=$((flag + 1))
		continue
	fi

	## get numbers
	if [ "$cflag" -lt 3 -a "$var" -lt 58 -a "$var" -gt 47 ]; then
		print_chr
		cflag=$((cflag + 1))
		continue
	fi

	## get upper case letters
	if [ "$var" -gt 64 -a "$var" -lt 91 ]; then
		print_chr
		continue
	fi

	## get lower case letters
	if [ "$var" -gt 97 -a "$var" -lt 123 ]; then
		print_chr
	fi
done
echo " " 
!PCbpd8Nk6qm

I'd be a little leery of using most of those symbols in a password. Quotes (") for instance; but guess it depends on what your needs are.

gDog

Posted: Thu 29 Sep 2011, 21:53
by jpeps
GatorDog wrote: generates the decimal numbers 10-99; so the hex numbers 4A 4B 4C 4D 4E 4F (J-O) and 6A 6B 6C 6D 6E 6F (j-o) won't be generated.
Right...I didn't bother with non-digit hex numbers....which goes beyond my purpose. You edited the post I responded to re: (A-F).

re: "quotes" Think I'll edit that out..so

Code: Select all

 if [  "$var" -lt 27 -a  "$var" -gt 22 ]

Posted: Fri 30 Sep 2011, 00:36
by jpeps
GatorDog raises an interesting point regarding use of hex. In his revision, he first converts number to hex, instead of trying to read hex directly. That prevents the following error:

Code: Select all

printf "\x106" 
6
printf "%x" 106 
6a
printf "\x6a"  
j

Posted: Fri 30 Sep 2011, 02:43
by GatorDog
Right...I didn't bother with non-digit hex numbers....which goes beyond my purpose. You edited the post I responded to re: (A-F).
Yep, sometimes going to the nth degree gets to be overkill. I'll have to check back up through this thread and see which post was edited.

Posted: Fri 30 Sep 2011, 05:24
by GatorDog
Just for fun, here's a Q & D Bacon version-

Code: Select all

Master_1$ = "_ - + . 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"
Master_2$ = " 0 1 2 3 4 5 6 7 8 9 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"
Master$ = CONCAT$(Master_1$, Master_2$)
SPLIT Master$ BY " " TO Chr$ SIZE Count
Pw$ = "[{<(@^&*)>}]"
FOR Index = 1 TO LEN(Pw$)
	Pw$ = REPLACE$(Pw$, MID$(Pw$, Index, 1), Chr$[RANDOM(Count + 1)])
NEXT Index
PRINT Pw$

Posted: Wed 07 Dec 2011, 10:00
by jpeps
Decided to create a gtkdialog front-end, which incorporates the above ideas in addition to making sure the end result has numbers, symbols, and letters. Generates to editable entry box

front end:

Code: Select all

 
#!/bin/sh

## Front end for password-generator.sh. jpeps 12/11


touch /tmp/pass
export GEN="/usr/local/passgen/password-generate.sh" 

Generate() {

"$GEN" >/tmp/pass



## check for symbols & numbers
  
while ([ ! "$var" ]); do

 # check for numbers
 var="$(cat /tmp/pass | grep "[0-9]")"
 ## check for sympbols
 [ "$var" ] &&  var="$(cat /tmp/pass | sed 's/[0-9a-Z]//g')"   

 ## if no sybols and numbers, re-generate
 [ "$var" ] || "$GEN" >/tmp/pass
done
}
export -f Generate


export MAIN_DIALOG='
<window title="Random Password" default-width="210">
  <vbox>
    <entry>
       <variable>ENTRY</variable>
       <input file>/tmp/pass</input>
    </entry>
    <hbox>
      <button tooltip-text="Generate new password" space-expand="true">
         <input file icon="gexec"></input>
         <label>" Generate "</label>
         <action>Generate</action>
         <action>refresh:ENTRY</action>
      </button>
   
      <button cancel></button>
    </hbox>
  </vbox>
</window>
'

gtkdialog --program=MAIN_DIALOG
rm /tmp/pass

Posted: Thu 08 Dec 2011, 10:28
by technosaurus

Code: Select all

tr -cd [0-z] </dev/urandom |head -c ${PASSWORDLENGTH:-10}

Posted: Thu 08 Dec 2011, 16:25
by jpeps
technosaurus wrote:

Code: Select all

tr -cd [0-z] </dev/urandom |head -c ${PASSWORDLENGTH:-10}
Seems to work better:

Code: Select all

tr -cd '0-z' </dev/urandom |head -c ${PASSWORDLENGTH:-10}
For some reason, this tended to hang up from within a program, requiring me to kill the process.