2 # ---------------------------------------------------------------------------
3 # Multi-Phasic Applications: SquirrelJME
4 # Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 # Copyright (C) Multi-Phasic Applications <multiphasicapps.net>
6 # ---------------------------------------------------------------------------
7 # SquirrelJME is under the GNU General Public License v3, or later.
8 # See license.mkd for licensing and copyright information.
9 # ---------------------------------------------------------------------------
10 # DESCRIPTION: Outputs some random bytes as hexadecimal
15 # Directory of this script
16 __exedir
="$(dirname -- "$0")"
21 echo "Usage: $0 (count)"
25 # If outputting no bytes, ignore
27 if [ "$__count" -le "0" ]
32 # Real random source, if it exists
33 if [ -c /dev
/urandom
] ||
[ -c /dev
/random
]
36 (if [ -c /dev
/urandom
]
38 dd if=/dev
/urandom
"bs=$__count" count
=1
42 dd if=/dev
/random
"bs=$__count" count
=1
43 fi) 2> /dev
/null |
hexdump -x |
44 sed 's/^[0-9a-fA-F]\{1,\}//g' |
tr -d ' ' |
tr -d '\n' |
tr -d '\t'
49 # Not really random, but something
51 # Seed the random number generator and get a value
52 __seed
="$( (echo -n $$ & echo -n $!; date +%s & echo -n $!) |
53 tr -d ' ' | tr -d '\n' | sed 's/[^0-9]*//g' | cut -c 1-8)"
57 while [ "$__i" -lt "$__count" ]
60 __a
="$(expr $__seed '*' 1337 | cut -c 1-8)"
67 # Add all of the seed values together
72 while [ -n "$__rest" ]
74 __char
="$(echo "$__rest" | cut -c 1)"
79 __xa
="$(expr "$__xa" + "$__char")"
82 __xb
="$(expr "$__xb" + "$__char")"
87 __rest
="$(echo "$__rest" | cut -c 2-)"
91 __ya
="$(expr $__xa % 16)"
92 __yb
="$(expr $__xb % 16)"
94 # Echo individual hex characters
95 for __q
in $__ya $__yb
100 12) printf '%s' "c";;
101 13) printf '%s' "d";;
102 14) printf '%s' "e";;
103 15) printf '%s' "f";;
104 *) printf '%s' "$__q";;
109 __i
="$(expr $__i + 1)"