From 01c0c8807b5d454c9128032415e46e921ac51226 Mon Sep 17 00:00:00 2001 From: dougy147 Date: Fri, 29 Sep 2023 17:32:59 +0200 Subject: [PATCH] Improving randomness quality with 'od' (retro-compatible if not available) --- bin/mcbash | 8 +++++++- func/00_dependencies | 2 +- func/08_mac | 8 +++++++- misc/infos.txt | 1 + 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/bin/mcbash b/bin/mcbash index b2923ab..40b15ab 100755 --- a/bin/mcbash +++ b/bin/mcbash @@ -338,7 +338,13 @@ random_mac() { missing_mac_values=$(( max_mac_length - number_chars_prefix )) suffix="" for (( i=0;i<${missing_mac_values};i++ )); do - suffix=$(echo $suffix)$(echo ${a[RANDOM%16]}) + #suffix=$(echo $suffix)$(echo ${a[RANDOM%16]}) + random_number=$(echo "$(od -An -N4 -tu4 /dev/random) % 16" | bc) 2>/dev/null + if [[ -z "${random_number+x}" ]]; then + suffix=$(echo $suffix)$(echo ${a[RANDOM%16]}) + else + suffix=$(echo $suffix)$(echo ${a[${random_number}]}) + fi done mac_without_colon=$(echo $mac_prefix | sed 's/://g')$(echo $suffix) new_mac=$(echo $mac_without_colon | sed -r 's/.{2}/&:/g ; s/:$//') diff --git a/func/00_dependencies b/func/00_dependencies index 5110acc..ca88ec6 100644 --- a/func/00_dependencies +++ b/func/00_dependencies @@ -9,7 +9,7 @@ check_dependencies() { exit 1 fi done - opt_dep=("md5sum" "sha256sum") # optional dependencies + opt_dep=("md5sum" "sha256sum" "od") # optional dependencies for o in ${opt_dep[@]}; do if ! [[ $(which $o 2>/dev/null) ]]; then echo -e "${_YELLOW}${_BOLD}WARNING: mcbash could need optional dependency ${_RED}${o}${_YELLOW}. \nConsider installing it on your machine.${_RESET}" diff --git a/func/08_mac b/func/08_mac index 144d33e..db58dec 100644 --- a/func/08_mac +++ b/func/08_mac @@ -12,7 +12,13 @@ random_mac() { missing_mac_values=$(( max_mac_length - number_chars_prefix )) suffix="" for (( i=0;i<${missing_mac_values};i++ )); do - suffix=$(echo $suffix)$(echo ${a[RANDOM%16]}) + #suffix=$(echo $suffix)$(echo ${a[RANDOM%16]}) + random_number=$(echo "$(od -An -N4 -tu4 /dev/random) % 16" | bc) 2>/dev/null + if [[ -z "${random_number+x}" ]]; then + suffix=$(echo $suffix)$(echo ${a[RANDOM%16]}) + else + suffix=$(echo $suffix)$(echo ${a[${random_number}]}) + fi done mac_without_colon=$(echo $mac_prefix | sed 's/://g')$(echo $suffix) new_mac=$(echo $mac_without_colon | sed -r 's/.{2}/&:/g ; s/:$//') diff --git a/misc/infos.txt b/misc/infos.txt index 0ae7825..eab4c5d 100644 --- a/misc/infos.txt +++ b/misc/infos.txt @@ -4,6 +4,7 @@ Dependencies : NAME : PACKAGE TO INSTALL - echo : core/utils - md5sum : core/utils - sha256sum : core/utils + - od : core/utils dougy147 [2022] https://github.com/dougy147/mcbash