make use of $! to prevent requirement of /dev/urandom
NOTE: generation without it causes large output numbers
This commit is contained in:
parent
b1facd33eb
commit
f6fc31efd7
37
LICENSE
37
LICENSE
|
@ -1,19 +1,24 @@
|
||||||
MIT License Copyright (c) <year> <copyright holders>
|
This is free and unencumbered software released into the public domain.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
distribute this software, either in source code form or as a compiled
|
||||||
in the Software without restriction, including without limitation the rights
|
binary, for any purpose, commercial or non-commercial, and by any
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
means.
|
||||||
copies of the Software, and to permit persons to whom the Software is furnished
|
|
||||||
to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice (including the next
|
In jurisdictions that recognize copyright laws, the author or authors
|
||||||
paragraph) shall be included in all copies or substantial portions of the
|
of this software dedicate any and all copyright interest in the
|
||||||
Software.
|
software to the public domain. We make this dedication for the benefit
|
||||||
|
of the public at large and to the detriment of our heirs and
|
||||||
|
successors. We intend this dedication to be an overt act of
|
||||||
|
relinquishment in perpetuity of all present and future rights to this
|
||||||
|
software under copyright law.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||||
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
For more information, please refer to <https://unlicense.org>
|
||||||
|
|
6
README
6
README
|
@ -1,3 +1,5 @@
|
||||||
----
|
---
|
||||||
# psh-prng
|
psh-prng
|
||||||
-- an attempt at pure POSIX sh random number generation
|
-- an attempt at pure POSIX sh random number generation
|
||||||
|
-- 100% POSIX compliant; if /dev/urandom is missing PIDs are used
|
||||||
|
---
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
```
|
```
|
||||||
----
|
---
|
||||||
# psh-prng
|
psh-prng
|
||||||
-- an attempt at pure POSIX sh random number generation
|
-- an attempt at pure POSIX sh random number generation
|
||||||
|
-- 100% POSIX compliant; if /dev/urandom is missing PIDs are used
|
||||||
|
---
|
||||||
```
|
```
|
||||||
|
|
43
ran
43
ran
|
@ -4,25 +4,18 @@ mseed() {
|
||||||
[ -c /dev/urandom ] && {
|
[ -c /dev/urandom ] && {
|
||||||
read -r seed < /dev/urandom # read a single line of urandom
|
read -r seed < /dev/urandom # read a single line of urandom
|
||||||
seed=${#seed}
|
seed=${#seed}
|
||||||
} || { # otherwise generate a seed by counting stuff
|
} || { # otherwise generate a seed using pids
|
||||||
n=0;
|
echo "!! WARNING: /dev/urandom IS MISSING -- HARDWARE RANDOMNESS CANNOT BE USED -- USING PID !!" >&2
|
||||||
[ -x /bin/ ] && { # if we can access /bin count the files in it
|
echo "!! -- CONTINUE AT YOUR OWN RISK -- !!" >&2
|
||||||
for i in /bin/*; do
|
: & wait; o="$!"; for i in 1 2 3 4 5; do
|
||||||
[ -f "$i" ] && : $((n+=1))
|
: & wait # generate new $!
|
||||||
|
o=$((o%o+$!*o))
|
||||||
done
|
done
|
||||||
}
|
for i in 1 2 3; do
|
||||||
[ -x /tmp/ ] && {
|
: & wait
|
||||||
for i in /tmp/*; do
|
|
||||||
[ -e "$i" ] && : $((n+=1))
|
|
||||||
done
|
done
|
||||||
}
|
seed="$((o%o+$!*${#o}))" # greatly shorten seed
|
||||||
[ -x /var/ ] && {
|
# large seeds produce large numbers = more randomness
|
||||||
for i in /var/*; do
|
|
||||||
[ -e "$i" ] && : $((n+=1))
|
|
||||||
done
|
|
||||||
}
|
|
||||||
: $((n+=${#PWD})); : $((n+=${#0}))
|
|
||||||
seed="${n:-100}"
|
|
||||||
}
|
}
|
||||||
} # make seed
|
} # make seed
|
||||||
mseed
|
mseed
|
||||||
|
@ -109,7 +102,21 @@ done
|
||||||
# the idea here is, a single line of urandom's output is (almost) random data
|
# the idea here is, a single line of urandom's output is (almost) random data
|
||||||
# thus count the length of said line, grab it's last digit, and add it (((a*seed)+p)%m)*m
|
# thus count the length of said line, grab it's last digit, and add it (((a*seed)+p)%m)*m
|
||||||
# -- o should be any number 0-9
|
# -- o should be any number 0-9
|
||||||
read -r o < /dev/urandom; o=${#o}
|
[ -c /dev/urandom ] && {
|
||||||
|
read -r o < /dev/urandom
|
||||||
|
o="${#o}"
|
||||||
|
} || {
|
||||||
|
echo "!! WARNING: /dev/urandom IS MISSING -- HARDWARE RANDOMNESS CANNOT BE USED -- USING PID !!" >&2
|
||||||
|
echo "!! -- CONTINUE AT YOUR OWN RISK -- !!" >&2
|
||||||
|
# warn about lack of hardware randomness
|
||||||
|
: & wait
|
||||||
|
o="$!" # pid is "RANDOM"
|
||||||
|
for i in 1 2 3; do
|
||||||
|
: & wait
|
||||||
|
done # 3 pid calls
|
||||||
|
o="$((o%o*o+$!))" # this allows the last digit to actually be random
|
||||||
|
}
|
||||||
|
|
||||||
until [ "${#o}" -eq 1 ]; do
|
until [ "${#o}" -eq 1 ]; do
|
||||||
o=${o#?}
|
o=${o#?}
|
||||||
done #; echo "$o -"
|
done #; echo "$o -"
|
||||||
|
|
Loading…
Reference in New Issue