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.
Hi, I've been looking into making a shuffle algorithm more secure that I've written in Bash. I wanted to know how can I make this more random? I've tried to avoid the random bias by using a range, but are there other methods I should be aware of? Here's my code:
Code:
myShuffle() {
local len i j
len=${#myPasswd[@]}
for (( i=len-1; i > 0; i-- )); do
while (( (j=$RANDOM) < 32768 % (i+1) )); do :; done;
j=$(( j % (i+1) ))
temp=${myPasswd[i]}
myPasswd[i]=${myPasswd[j]}
myPasswd[j]=$temp
done
}
Last edited by Lucien Lachance; 05-14-2013 at 03:21 PM.
You could try this concept. Can't explain much but please try to understand the code. I believe this is secure enough unless there's something ultra-intelligent light-speed tracer stalking [within] your machine.
Code:
#!/bin/bash
CHARS=(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 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)
CHARS_COUNT=${#CHARS[@]}
PASSWORD_LENGTH=8
PASSWORD=''
for (( I = 1; I <= PASSWORD_LENGTH; ++I )); do
PASSWORD=${PASSWORD}${CHARS[RANDOM % CHARS_COUNT]}
done
echo "Password: $PASSWORD"
You could add more characters to CHARS, and/or customize PASSWORD_LENGTH if you like. Of course the output could be tapped but that's a different issue and no longer about the strength of the generation of the password.
Last edited by konsolebox; 05-16-2013 at 07:09 AM.
Furthermore, it seems to me that the earlier versions would not necessarily work as intended. But I'm baffled to wonder why one would do such a thing, anyway, in Bash! (There are plenty of "real" programming languages at your fingertips ...)
The algorithm is developed as follows:
The "common sense version" is that you start with a deck of 52 cards at the left hand, and an empty pile at the right. Select a card at random from the left, remove it from the pile, and put it on the top of the pile at right. Repeat this 52 times and you're done.
The "clever realization" is that, since there are always exactly 52 cards, you don't actually need two piles. It is equivalent to select a card at-random and exchange it with the card at a cursor-position [52 downto 2]: the "left-hand pile" consists of those cards at-or-below the cursor position, while the "right-hand pile" systematically accumulates at-and-below the cursor.
The algorithm is thorough and instantaneous because there is always a card to choose from, and always a place to put it. There's no possibility that a card would be selected twice, or not selected at all.
But why, oh why, would you do it in Bash?
Last edited by sundialsvcs; 05-16-2013 at 09:32 AM.
That could be done in Bash but I don't see how such an algorithm that shuffles contents could give difference with the strength of password generation.
Yeah, I know I could've easily have done this in Python, but I really wanted to see how a generator was built. A lot of the times whenever I've used an algorithm to sort elements I've never put much thought into how secure or how "random" rand() really was until now. Plus, I'm kind of getting into security and this is something I've always wanted handy whenever I'm in Vim and need to a good password for an application.
I've never put much thought into how secure or how "random" rand() really was until now. Plus, I'm kind of getting into security and this is something I've always wanted handy whenever I'm in Vim and need to a good password for an application.
Yet again how would the randomness of rand() be really helpful? It's not as if something would be trying to "guess-trace" it.
Btw, how would you think the script I posted contribute to your requirement?
For bash alone I don't think there's a way, but for other commands like tput perhaps, perhaps.
Try to find a command that prints the characters of [:graph:] or [:punct:], or perhaps other classes as well as interpreted in regex/glob parsers like grep.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.