A random string generator in a bash script isn't respecting the number of given characters

前端 未结 4 836
时光说笑
时光说笑 2020-12-22 01:08

I am trying to build a random character generator in a bash script on osx 10.8.5 . The goal is to generate random character strings for a script generating salts for the wor

相关标签:
4条回答
  • 2020-12-22 01:18

    No offense, by your coding style is, errr..., not the best I've seen :).

    #!/bin/bash -e
    
    read -p "Number of digits: " digits
    # TODO: test that digits is really a number
    
    chars=( {a..z} {A..Z} {0..9} \, \; \. \: \- \_ \# \* \+ \~ \! \§ \$ \% \& \( \) \= \? \{ \[ \] \} \| \> \< )
    
    function rand_string {
        local c=$1 ret=
        while((c--)); do
            ret+=${chars[$((RANDOM%${#chars[@]}))]}
        done
        printf '%s\n' "$ret"
    }
    
    outputsalt=$(rand_string $digits)
    
    echo "$outputsalt"
    
    0 讨论(0)
  • 2020-12-22 01:27

    Another way of doing this if you fancy one-liners:

    perl -le 'print map { ("a".."z","A".."Z",0..9,",",";",".",":","-","_","#","*","+","~","!","§","\$","%","&","(",")","=","?","{","}","[","]","|","<",">") [rand 87] } 1..63'
    

    or, as you probably won't want a new line at end:

    perl -e 'print map { ("a".."z","A".."Z",0..9,",",";",".",":","-","_","#","*","+","~","!","§","\$","%","&","(",")","=","?","{","}","[","]","|","<",">") [rand 87] } 1..63'
    
    0 讨论(0)
  • 2020-12-22 01:33

    Try a simpler case:

    function rand_char {
      take=$(($RANDOM % 2)); i=0; echo a b | while read -d\  char;
      do
        [ "$i" = "$take" ] && echo "$char\c";
        ((i++));
      done
    }
    

    It will produce a and blanks, but no b.

    We can further reduce the problem down to:

    echo a b | while read -d\  char; do echo "$char"; done
    

    which only writes a and not b. This is because you instruct read to read up to a space, and there's no space after b so it fails. This means that one out of every 88 chars will be dropped, causing your lines to be slightly shorter.

    The simplest fix is to add a dummy argument to force a space at the end:

    echo {a..z} {A..Z} {0..9} (etc etc) \} \| \> \< '' | while read ...
    #                                       Here ---^
    

    Note that your method just adds 15 bits of entropy to the salt, while the absolute minimum should be 64. The significantly easier and more secure way of doing this would be:

    LC_CTYPE=C tr -cd 'a-zA-Z0-9,;.:_#*+~!@$%&()=?{[]}|><-' < /dev/urandom | head -c 64
    

    (note: this replaces your unicode paragraph symbol with an ascii @)

    0 讨论(0)
  • 2020-12-22 01:34

    I see two problems in your script.

    I'm pretty sure

    take=$(($RANDOM % 88));
    

    should be

    take=$(($RANDOM % 87));
    

    Otherwise, it appears you're going past the end of your input stream.

    The other problem is this char:

    Bash is seeing that as two characters (wide char?). I'd delete it from your possibilities.

    Of course, that would mean the above line would be:

    take=$(($RANDOM % 86));
    

    Doing those two things, in fact, works for me.

    Edited:

    @that other guy has a better answer. Adding the space rather than reducing the modulo will ensure that you get every character

    0 讨论(0)
提交回复
热议问题