LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-14-2013, 09:47 AM   #1
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Rep: Reputation: Disabled
Shuffle in Bash


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.
 
Old 05-14-2013, 01:07 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Please use [ CODE] .. [/CODE] tags around your code. They are generated by pressing the hash (#) in the menu bar of the edit window.

Have a check at this http://tldp.org/LDP/abs/html/randomvar.html

jlinkels
 
1 members found this post helpful.
Old 05-14-2013, 01:09 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Yes, you can easily use /dev/urandom for this. Here is some info and examples:

http://man7.org/linux/man-pages/man4/random.4.html
 
1 members found this post helpful.
Old 05-14-2013, 01:44 PM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 05-14-2013, 03:24 PM   #5
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
Thanks for showing me /dev/urandom I'll be sure to check that out.
 
Old 05-16-2013, 07:07 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 05-16-2013, 08:52 AM   #7
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
http://en.wikipedia.org/wiki/Fisher%...3Yates_shuffle -- better than pairwise swaps
 
Old 05-16-2013, 09:30 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Quote:
Originally Posted by linosaurusroot View Post
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.
 
Old 05-16-2013, 09:50 AM   #9
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 linosaurusroot View Post
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.
 
Old 05-16-2013, 01:10 PM   #10
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
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.
 
Old 05-16-2013, 05:52 PM   #11
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
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?
 
Old 05-16-2013, 08:03 PM   #12
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
I think I missed the point of shuffling. If all you need is a random string, you could do this, for example:
Code:
tony> pass=`head /dev/urandom | tr -dc [:alnum:] | head -c 10`
tony> echo $pass
dFq166jIi1
Are you thinking it will be even more random if you shuffle the bytes?
 
Old 05-17-2013, 09:45 PM   #13
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by konsolebox View Post
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?
Yes, it did actually. I declared an array using this approach:

Code:
password=( {a..z} {A..Z} {0..9} )
Is there a way I can encapsulate special characters much like the {a..z} expansion that's specifically for special characters?

Last edited by Lucien Lachance; 05-18-2013 at 01:40 PM.
 
Old 05-18-2013, 09:24 AM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 05-18-2013, 01:41 PM   #15
Lucien Lachance
Member
 
Registered: May 2013
Posts: 82

Original Poster
Rep: Reputation: Disabled
I'll look into it thanks for the help as well, I really appreciate it.

Last edited by Lucien Lachance; 05-19-2013 at 11:20 PM.
 
  


Reply



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
ipod shuffle HOWTO please :-) aquaboot Ubuntu 2 06-25-2006 03:04 PM
Help on iPod Shuffle Autofill BASH Script dgeronimo Programming 3 01-04-2006 05:23 PM
Ipod Shuffle help ziggis-soft General 8 12-29-2005 10:47 PM
ipod shuffle speel *BSD 2 06-10-2005 06:09 PM
ipod shuffle, again. JoeUser11 Linux - Newbie 8 05-17-2005 07:58 PM

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

All times are GMT -5. The time now is 02:21 AM.

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