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
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
The above copyright notice and this permission notice (including the next
|
||||
paragraph) shall be included in all copies or substantial portions of the
|
||||
Software.
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
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
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
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
|
||||
-- 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
|
||||
-- 100% POSIX compliant; if /dev/urandom is missing PIDs are used
|
||||
---
|
||||
```
|
||||
|
|
47
ran
47
ran
|
@ -4,25 +4,18 @@ mseed() {
|
|||
[ -c /dev/urandom ] && {
|
||||
read -r seed < /dev/urandom # read a single line of urandom
|
||||
seed=${#seed}
|
||||
} || { # otherwise generate a seed by counting stuff
|
||||
n=0;
|
||||
[ -x /bin/ ] && { # if we can access /bin count the files in it
|
||||
for i in /bin/*; do
|
||||
[ -f "$i" ] && : $((n+=1))
|
||||
done
|
||||
}
|
||||
[ -x /tmp/ ] && {
|
||||
for i in /tmp/*; do
|
||||
[ -e "$i" ] && : $((n+=1))
|
||||
done
|
||||
}
|
||||
[ -x /var/ ] && {
|
||||
for i in /var/*; do
|
||||
[ -e "$i" ] && : $((n+=1))
|
||||
done
|
||||
}
|
||||
: $((n+=${#PWD})); : $((n+=${#0}))
|
||||
seed="${n:-100}"
|
||||
} || { # otherwise generate a seed using pids
|
||||
echo "!! WARNING: /dev/urandom IS MISSING -- HARDWARE RANDOMNESS CANNOT BE USED -- USING PID !!" >&2
|
||||
echo "!! -- CONTINUE AT YOUR OWN RISK -- !!" >&2
|
||||
: & wait; o="$!"; for i in 1 2 3 4 5; do
|
||||
: & wait # generate new $!
|
||||
o=$((o%o+$!*o))
|
||||
done
|
||||
for i in 1 2 3; do
|
||||
: & wait
|
||||
done
|
||||
seed="$((o%o+$!*${#o}))" # greatly shorten seed
|
||||
# large seeds produce large numbers = more randomness
|
||||
}
|
||||
} # make seed
|
||||
mseed
|
||||
|
@ -109,7 +102,21 @@ done
|
|||
# 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
|
||||
# -- 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
|
||||
o=${o#?}
|
||||
done #; echo "$o -"
|
||||
|
|
Loading…
Reference in New Issue