From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 280F615817D for ; Fri, 21 Jun 2024 13:14:18 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 238C6E2A38; Fri, 21 Jun 2024 13:14:17 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 0CF6AE2A38 for ; Fri, 21 Jun 2024 13:14:17 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 48E6433BF29 for ; Fri, 21 Jun 2024 13:14:16 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 7A0481D4D for ; Fri, 21 Jun 2024 13:14:14 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1718176001.5ee035a364bea8d12bc8abfe769014e230a212a6.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh test-functions X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 5ee035a364bea8d12bc8abfe769014e230a212a6 X-VCS-Branch: master Date: Fri, 21 Jun 2024 13:14:14 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 1cb040d6-af4f-4449-9f49-148ce662d7fc X-Archives-Hash: 7eaeee353219a505ef564068fdaf8259 commit: 5ee035a364bea8d12bc8abfe769014e230a212a6 Author: Kerin Millar plushkava net> AuthorDate: Sun Jun 2 17:46:43 2024 +0000 Commit: Sam James gentoo org> CommitDate: Wed Jun 12 07:06:41 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=5ee035a3 Add the srandom() function This is based on the behaviour of the special SRANDOM variable in bash. It should be noted that sh is capable of bitwise arithmetic. For instance, it is possible to clamp the number in such a way that it does not exceed the minimum possible value of RAND_MAX (32767). n=$(srandom) && : $(( n >>= 17 )) Signed-off-by: Kerin Millar plushkava.net> functions.sh | 27 +++++++++++++++++++++++++++ test-functions | 22 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/functions.sh b/functions.sh index a97dde7..0c35b2d 100644 --- a/functions.sh +++ b/functions.sh @@ -12,6 +12,7 @@ # The following variables affect initialisation and/or function behaviour. # BASH : whether bash-specific features may be employed +# BASH_VERSINFO : whether bash-specific features may be employed # BASHPID : potentially used by _update_columns() to detect subshells # COLUMNS : potentially used by _update_columns() to get the column count # EERROR_QUIET : whether error printing functions should be silenced @@ -473,6 +474,32 @@ vewend() fi } +# +# Generates a random uint32 with the assistance of the kernel CSPRNG. +# +srandom() +{ + # shellcheck disable=3028 + if [ "${BASH_VERSINFO:-0}" -ge 5 ]; then + srandom() + { + printf '%d\n' "${SRANDOM}" + } + elif [ -c /dev/urandom ]; then + srandom() + { + printf '%d\n' "0x$( + LC_ALL=C od -vAn -N4 -tx1 /dev/urandom | tr -d '[:space:]' + )" + } + else + warn "srandom: /dev/urandom doesn't exist as a character device" + return 1 + fi + + srandom +} + # # Prints a diagnostic message prefixed with the basename of the running script. # diff --git a/test-functions b/test-functions index 8ff6380..0c4a222 100755 --- a/test-functions +++ b/test-functions @@ -397,6 +397,27 @@ test_yesno() { iterate_tests 3 "$@" } +test_srandom() { + set -- \ + eq 0 \ + eq 0 \ + eq 0 \ + eq 0 \ + eq 0 + + row=0 + + callback() { + number=$(srandom) + test_description="srandom ($(( row += 1 ))/5: ${number:-blank})" + is_int "${number}" \ + && test "${number}" -ge 0 \ + && test "${number}" -le 4294967295 + } + + iterate_tests 2 "$@" +} + iterate_tests() { slice_width=$1 shift @@ -457,6 +478,7 @@ test_is_visible || rc=1 test_yesno || rc=1 test_die || rc=1 test_edo || rc=1 +test_srandom || rc=1 cleanup_tmpdir