-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrandom.functions
169 lines (147 loc) · 5.08 KB
/
random.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# This file is part of shellfire random. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/random/master/COPYRIGHT. No part of shellfire random, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire random. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/random/master/COPYRIGHT.
random_generateBetween0To255()
{
if core_variable_isUnset _random_generateBetween0To255_implementation; then
_random_generateBetween0To255_determineImplementation
fi
${_random_generateBetween0To255_implementation}
}
random_generateBetween0AndModulus()
{
local modulus=$1
local randomNumber="$(random_generateBetween0To255)"
printf '%s' $(($randomNumber % $modulus))
}
random_characterForEncodingLowerCaseOnly()
{
local number0To35=$(random_generateBetween0AndModulus 36)
if [ $number0To35 -lt 26 ]; then
local subtract=0
local base=97
else
local subtract=26
local base=48
fi
printf '%s' $((number0To35 - subtract + base))
}
random_characterForEncodingLettersAndNumbers()
{
local number0To61=$(random_generateBetween0AndModulus 62)
if [ $number0To61 -lt 26 ]; then
local subtract=0
local base=65
elif [ $number0To61 -lt 52 ]; then
local subtract=26
local base=97
else
local subtract=52
local base=48
fi
printf '%s' $((number0To61 - subtract + base))
}
random_characterForEncodingBase64Like()
{
local number0To63=$(random_generateBetween0AndModulus 64)
if [ $number0To63 -lt 26 ]; then
local subtract=0
local base=65
elif [ $number0To63 -lt 52 ]; then
local subtract=26
local base=97
elif [ $number0To63 -lt 62 ]; then
local subtract=52
local base=48
elif [ $number0To63 -lt 63 ]; then
local subtract=62
local base=47
else
local subtract=63
local base=43
fi
printf '%s' $((number0To63 - subtract + base))
}
core_dependency_oneOf '*' hexdump od openssl gpg awk
random_generateBetween0To255_hexdump()
{
printf '%s' "$(hexdump -e '/1 "%u"' -n 1 "$_random_generateBetween0To255_randomFile")"
}
random_generateBetween0To255_od()
{
# MacOSX od has lots of leading whitespace and two \n
# GNU coreutils od has one to three leading whitespace and single \n
# The UNQUOTED $(...) capture ensures all leading whitespace is removed
# The $(...) capture ensures all trailing \n (single or multiple) are removed
printf '%s' $(od -A n -t u1 -N1 "$_random_generateBetween0To255_randomFile")
}
random_generateBetween0To255_openssl()
{
printf '%d' 0x$(openssl rand -hex 1)
}
random_generateBetween0To255_gpgHexdump()
{
printf '%s' "$(gpg --gen-random 1 1 | hexdump -e '/1 "%u"')"
}
random_generateBetween0To255_gpgOd()
{
# The UNQUOTED $(...) capture ensures all leading whitespace is removed
# The $(...) capture ensures all trailing \n (single or multiple) are removed
printf '%s' $(gpg --gen-random 1 1 | od -A n -t u1)
}
random_generateBetween0To255_RANDOM()
{
printf '%s' $(( RANDOM % 256 ))
}
# seed is time based and I have seen identical values output when invoked in a very tight loop.
random_generateBetween0To255_awk()
{
echo | awk ' { srand(); print int((rand()*1000000)%256)} '
}
_random_generateBetween0To255_determineImplementation()
{
_random_generateBetween0To255_randomFile=''
local randomFilePath
for randomFilePath in /dev/urandom /dev/random
do
if [ -r "$randomFilePath" ]; then
_random_generateBetween0To255_randomFile="$randomFilePath"
break
fi
done
if [ -n "$_random_generateBetween0To255_randomFile" ]; then
if core_compatibility_whichNoOutput hexdump; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_hexdump
return 0
fi
if core_compatibility_whichNoOutput od; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_od
return 0
fi
fi
if core_compatibility_whichNoOutput openssl; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_openssl
return 0
fi
if core_compatibility_whichNoOutput gpg; then
if core_compatibility_whichNoOutput hexdump; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_gpgHexdump
return 0
fi
if core_compatibility_whichNoOutput od; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_gpgOd
return 0
fi
fi
# Nearly everything has this bar dash and original sh, although ash in BuysBox can be configured without it
if core_variable_isSet RANDOM; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_RANDOM
core_message WARN "Non-cryptographic randomness source selected (built-in RANDOM function)"
return 0
fi
if core_compatibility_whichNoOutput awk; then
_random_generateBetween0To255_implementation=random_generateBetween0To255_awk
core_message WARN "Non-cryptographic randomness source selected (awk's srand)"
return 0
fi
core_exitError $core_commandLine_exitCode_OSFILE "No source of randomess could be used"
}