Skip to content

Latest commit

 

History

History
219 lines (167 loc) · 5.14 KB

random.md

File metadata and controls

219 lines (167 loc) · 5.14 KB

Bash-Funk "random" module

The following commands are available when this module is loaded:

  1. -entropy-available
  2. -fill-entropy
  3. -random-number
  4. -random-string
  5. -test-random

License

Copyright (c) 2015-2018 Vegard IT GmbH, http://vegardit.com

SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

-entropy-available

Usage: -entropy-available [OPTION]...

Determines if enough entropy bits are available perform a non-blocking read from /dev/random. Exit code 1 indicates entropy pool is not sufficiently filled.

Options:
    --help 
        Prints this help.
    --selftest 
        Performs a self-test.
    --
        Terminates the option list.

Examples:
$ -entropy-available 
/proc/sys/kernel/random/entropy_avail: 64
/proc/sys/kernel/random/read_wakeup_threshold: 2429

Implementation:

if [[ ! -e /proc/sys/kernel/random/read_wakeup_threshold ]]; then
    echo "-entropy-available: Warning: Kernel parameter /proc/sys/kernel/random/read_wakeup_threshold is not present, assuming sufficient entropy is available."
    return 0
fi

local avail=$(cat /proc/sys/kernel/random/entropy_avail)
local required=$(cat /proc/sys/kernel/random/read_wakeup_threshold)
echo "/proc/sys/kernel/random/entropy_avail: $avail"
echo "/proc/sys/kernel/random/read_wakeup_threshold: $required"
[[ $(( avail > required )) ]]

-fill-entropy

Usage: -fill-entropy [OPTION]... [DURATION]

Fills /dev/random with pseudo-random values from /dev/urandom.

Requirements:
  + Command 'rngd' must be available.
  + Sudo 'rngd' is required.

Parameters:
  DURATION (default: '1', integer: 1-?)
      Number of seconds the entropy pool will be filled.

Options:
    --help 
        Prints this help.
    --selftest 
        Performs a self-test.
    --
        Terminates the option list.

Examples:
$ -fill-entropy 
Available entropy bits before: 1000
Generating for 1 second(s)...
Available entropy bits after: 1013
$ -fill-entropy 2
Available entropy bits before: 1013
Generating for 2 second(s)...
Available entropy bits after: 1039

Implementation:

echo -n "Available entropy bits before: "
cat /proc/sys/kernel/random/entropy_avail

echo "Generating for ${_DURATION} seconds..."
if rngd --help | grep -q -- --timeout; then
    sudo rngd -r /dev/urandom -o /dev/random -f --timeout ${_DURATION}
else
    -timeout ${_DURATION} sudo rngd -r /dev/urandom -o /dev/random -f
fi

echo -n "Available entropy bits after: "
cat /proc/sys/kernel/random/entropy_avail

-random-number

Usage: -random-number [OPTION]... RANGE

Generates a random number of the given range. The range is inclusive.

Parameters:
  RANGE (required, pattern: "[1-9][0-9]*-[1-9][0-9]*")
      The numeric range LOW-HIGH, e.g. 1-5.

Options:
    --help 
        Prints this help.
    --selftest 
        Performs a self-test.
    --
        Terminates the option list.

Examples:
$ -random-number 1-1
1
$ -random-number 1-5
4
$ -random-number 200-299
253

Implementation:

shuf -i ${_RANGE} -n 1

-random-string

Usage: -random-string [OPTION]... LENGTH [CHARS]

Prints a random string of the given length containing the given characters.

Parameters:
  LENGTH (required, integer: 1-?)
      Length of the string to generate.
  CHARS (default: '[:graph:]')
      String to choose random characters from.

Options:
    --help 
        Prints this help.
    --selftest 
        Performs a self-test.
    --
        Terminates the option list.

Examples:
$ -random-string 12 0-9
686026902293
$ -random-string 8 a-zA-Z
ECtQptCz
$ -random-string 10 [:alnum:]
w2TZ8fHaRw
$ -random-string 10 [:alnum:][:punct:]
4pH?+40D):

Implementation:

local rc
env LC_CTYPE=C tr -dc "$_CHARS" < /dev/urandom | fold -w ${_LENGTH} | head -n 1

# https://stackoverflow.com/questions/19120263/why-exit-code-141-with-grep-q
rc=$?
(( rc == 141 )) && return 0 || return $rc

-test-random

Usage: -test-random [OPTION]...

Performs a selftest of all functions of this module by executing each function with option '--selftest'.

Options:
    --help 
        Prints this help.
    --selftest 
        Performs a self-test.
    --
        Terminates the option list.

Implementation:

-entropy-available --selftest && echo || return 1
-fill-entropy --selftest && echo || return 1
-random-number --selftest && echo || return 1
-random-string --selftest && echo || return 1