-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake-prng.sh
More file actions
330 lines (276 loc) · 6.57 KB
/
Copy pathfake-prng.sh
File metadata and controls
330 lines (276 loc) · 6.57 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
# fake-prng.sh - Version 1.0
# shellcheck disable=SC2319,SC2312
# Script is finished - 12 Sept 2026 WY
##<> Debug
#reset #<>
#clear #<>
#set -x #<>
set -eu #<>
set -C #<>
shopt -s nullglob inherit_errexit
### Setup
## Traps
trap - EXIT INT
# shellcheck disable=SC2329
_trap_function_(){
trap - EXIT INT;
/bin/kill -s INT "$$";
}
trap _trap_function_ EXIT INT;
## Remove any data files from previous executions of this script.
filename_template=/dev/shm/value
_remove_data_files_()
{
for FF in "${filename_template}"_*;
do
if [[ -f "${FF}" ]] \
&& [[ ! -L "${FF}" ]]
then
: t #<>
if rm -- "${FF}";
then
: t #<>
:
else
: f:$? #<>
printf '\n\tCommand \rm\ failed; exiting.'
exit "${LINENO}"
fi
else : "f:$?" #<>
fi
done
}
_remove_data_files_
#set -x #<>
## Variables
output_max_length=8
# Capture the time in \seconds since the epoch\ format.
start_time=$( date +%s )
# Define a filepath and name for the data file.
file=${filename_template}_${start_time}
output=0
ii=0
current_file=${file}_${ii}
# Digest binaries
digest_binaries=()
for BB in "b2sum" "sha512sum" "sha384sum" "sha256sum" "sha224sum" "sha1sum";
do
:;: "Command must exist in PATH."
if command -v "${BB}" >/dev/null
then
: t #<>
if (( ${#digest_binaries[@]} < 3 ))
then
: t #<>
digest_binaries+=( "${BB}" )
else
: f:$? #<>
break
fi
else
: f:$? #<>
continue
fi
done
:;: "Only add the \\broken\\ digests if the more secure ones are NA."
for AA in md5sum cksum sum;
do
if (( ${#digest_binaries[@]} < 3 ))
then
: t #<>
if command -v "${BB}" >/dev/null
then
: t #<>
printf '\n\tWarning: using insecure digest function: %s\n.' "${AA}"
digest_binaries+=( "${AA}" )
else : f:$? #<>
fi
else
: f:$? #<>
break
fi
done
### Start with some number
:;: "Write a non-random number to file."
# shellcheck disable=SC3028
printf '%s\n' $(( "${PPID:- "$$"}" * "${RANDOM:="${start_time}"}" )) \
| tee "${current_file}" >/dev/null
next_file=${file}_$(( ++ii ))
### Process the number, once per each digest command
xx=0
for CC in "${digest_binaries[@]}";
do
:;: "Index counter. \\break\\ after three rounds."
xx=$(( ++xx ))
:;: "Take the digest of file."
command -p "${CC}" "${current_file}" \
| tee "${next_file}" >/dev/null
current_file=${next_file}
next_file=${file}_$(( ++ii ))
#set -x #<>
:;: "Assign values to variables using data from file."
# Note, \read\ requires a trailing newline.
read -r dividend divisor _ < "${current_file}";
#dividend=$( sha256sum <<< "${dividend}" ) #<>
#declare -p dividend divisor ii current_file next_file #<>
:;: "Remove any filenames from the divisor, or dashes."
if [[ ${divisor} =~ ^(-|/[[:alnum:]]*/.*)$ ]]
then
: t #<>
divisor=;
else : f:$? #<>
fi
:;: "Remove any non-integer characters or leading zeros, "
# Note, \cut\ command is not necc with \sum\ or \cksum\; other
# bins not yet tested.
_remove_non_integers_and_leading_zeros_()
{
local var
var=${1:-};
if (( ${#var} > 0 ))
then
: t #<>
if [[ ${var} =~ [^[:digit:]] ]]
then
: t #<>
var=$( printf '%s' "${var}" \
| sed 's/[^[:digit:]]//g')
else : f:$? #<>
fi
:;: "Also remove leading zeros."
var=$( printf 'scale=0; %s * 1\n' "${var}" \
| bc \
| tr -d '\\\n')
:;: "Print the output to populate the variable assignment"
printf '%s' "${var}"
else
: f:$? #<>
return
fi
}
dividend=$( _remove_non_integers_and_leading_zeros_ "${dividend}" )
divisor=$( _remove_non_integers_and_leading_zeros_ "${divisor}" )
#declare -p dividend divisor #<>
#exit "${LINENO}" #<>
# The last digit of the dividend could be used as a divisor, so remove
# any trailing zeros from the dividend, if the divisor is zero, is not
# an integer, or DNE.
:;: "Dividend should not be all zeros."
if (( dividend != 0 ))
then
: t #<>
else
: f:$? #<>
echo error
exit "${LINENO}"
fi
:;: "Dividend must have at least 2 characters length."
if (( ${#dividend} >= 2 ))
then
: t #<>
else
: f:$? #<>
echo error
exit "${LINENO}"
fi
#divisor=0 #<>
#divisor= #<>
#divisor=- #<>
:;: "If divisor\\s problematic, use trailing non-zero integer from dividend."
if [[ -z ${divisor} ]]
then
: t #<>
while true
do
# Get the last digit of the string.
last=$( printf '%s' "${dividend}" \
| sed 's,'"${dividend%?}"',,')
# If the last digit is a 0 or 1, then remove the digit and
# begin this function\s loop again.
if [[ ${last} == [2-9] ]]
then
: t #<>
dividend=${dividend%?}
divisor=${last}
break
else
: f:$? #<>
dividend=${dividend%?}
fi
done
else
: f:$? #<>
fi
#declare -p dividend divisor #<>
#exit "${LINENO}" #<>
#set -x #<>
:;: "Verify the divisor is an integer."
if printf '%s' "${divisor}" \
| grep -E '^[0-9]+$' >/dev/null
then
: t #<>
: # If it is, then move to the next step.
else
: f$? #<>
echo error
exit "${LINENO}"
fi
:;: "Perform integer division using the \\bc\\ command."
output=$( printf 'scale=0; %s / %d\n' "${dividend}" "${divisor}" \
| bc \
| tr -d '\\\n')
while true
do
if (( ${#output} > output_max_length * 2 ))
then
: t #<>
divisor=$(( divisor + 2 ))
output=$( printf 'scale=0; %s / %d\n' "${output}" "${divisor}" \
| bc \
| tr -d '\\\n')
else
: f:$? #<>
break
fi
done
#declare -p dividend divisor output xx #<>
#exit "${LINENO}" #<>
:;: "Write the output of this round to file, and redefine file names"
printf '%s\n' "${output}" \
| tee "${next_file}" >/dev/null
current_file=${next_file}
next_file=${file}_$(( ++ii ))
:;: "\\break\\ after three rounds."
if (( xx == 3 ))
then
: t #<>
break
else : f:$? #<>
fi
done
output_max_length=$(( --output_max_length )) # For zero-based math.
if (( ${#output} <= output_max_length ))
then
: t #<>
printf '%d\n' "${output}"
else
: f:$? #<>
extra=$(( ${#output} - output_max_length ))
start=$(( extra / 2 ))
while true
do
if (( ${output:${start}:1} == 0 ))
then
: t #<>
start=$(( ++start ))
else
: f:$? #<>
break
fi
done
#declare -p extra start #<>
printf '%d\n' "${output:${start}:${output_max_length}}"
fi
_remove_data_files_
exit 00