password generator

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

password generator

#1 Post 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
Last edited by jpeps on Thu 29 Sep 2011, 22:27, edited 2 times in total.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#2 Post 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

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#3 Post 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

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#4 Post 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 :)

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#5 Post 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

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#6 Post 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 ]

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#7 Post 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

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#8 Post 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.

User avatar
GatorDog
Posts: 138
Joined: Tue 12 Sep 2006, 16:43

#9 Post 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$

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#10 Post 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
Attachments
passgen.png
(9.03 KiB) Downloaded 581 times
passgen-1.0.pet
Menu/System|Password Generator
exec: &quot;pass-gen&quot;
(1.41 KiB) Downloaded 459 times

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

Code: Select all

tr -cd [0-z] </dev/urandom |head -c ${PASSWORDLENGTH:-10}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#12 Post 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.

Post Reply