zsh-workers
 help / color / mirror / code / Atom feed
From: Clinton Bunch <cdb_zsh@zentaur.org>
To: zsh-workers@zsh.org
Subject: Re: [PATCH] zsh/random module [UPDATED]
Date: Fri, 18 Nov 2022 11:08:42 -0600	[thread overview]
Message-ID: <a0ea90d6-72c3-83e2-d4d2-0f9cafa63b4c@zentaur.org> (raw)
In-Reply-To: <20221118162325.7i3qzqljyx4a7z3h@chazelas.org>


On 11/18/2022 10:23 AM, Stephane Chazelas wrote:
> I should point out first that I'm not a zsh maintainer. I have
> been using zsh daily for over 25 years though, and have been
> following this ML on and off for a few years.
>
> I should also point out that I've not read the past discussions
> about your module. Sorry if I'm repeating points that have been
> made earlier.
>
> Thanks for contributing to zsh, it's always good to get more
> hands on it!
>
> In this instance though I'm not particularly convinced that that
> new module is particularly needed.
>
> We already have $RANDOM and $(( rand48() )). Most if not all of
> the systems I've ever used had /dev/random / /dev/urandom
RANDOM and rand48 are predictable and low quality.
>
> You can already read n bytes from them with:
>
> set +o multibyte
> read -ru0 -k20 bytes < /dev/random
>
> or
>
> sysread -s 20 bytes < /dev/urandom
>
> Split into individual bytes in an array with:
>
> array=( ${(s[])bytes )
>
> convert those from/to decimal/octal/hexadecimal with printf or
> the # flag or #/## arithmetic operators:
>
> hex=(); printf -v hex %02x \'$^array
>
> For instance or:
>
> set -o extendedglob
> hex_string=${bytes//(#m)?/${(l[2][0])$(([#16] #MATCH))}}
>
> So it seems the functionality of that module could be
> implemented in a few lines of zsh. Likely less efficiently and
> more awkwardly, but maybe adding capabilities to decode bytes
> (a la perl's pack/unpack) would be more useful and could be used
> for other things than /dev/random.
>
> Some comments about the API:
>
>>    -c COUNT
>>         Sets the number of returned random data.  Defaults to 8,
>>         unless -i, -L, or -U is specified without -a, in which case
>>         the default is 1.  COUNT must be between 1 and 64.
> Why those arbitrary limits? Why not from 0 to inf?

the getrandom function only guarantees 256 bytes.  64 32-bit integers.  
Allowing 256 uint8 would be possible, but harder to explain and doesn't 
work well with bounding.  As the difference between the lower and upper 
limit surpasses half the max, the number of bytes thrown away to allow 
uniformity of distribution becomes unwieldy.

Also zero would not be particularly useful :)

>
> Without -i you get uint8 in hex and with -i uint32 in decimal.
> Why the different bases?

It's possible to break a hex string into bytes in a rather straight 
forward way.   A string of decimal numbers not so much, but decimal 
numbers are easier to manipulate.

If I didn't think it should do *something* when given no arguments, I'd 
eliminate the hex string altogether.

> Why limiting the bounds to unsigned 32
> bits when zsh arithmetic is in signed 64 bits?
compatibility with SRANDOM in bash.  Return size of arc4random. Reducing 
maximum count to 32.
>
> Other than that, it looks good to me.
>
> Trying to use it to get a random word with it, I came up with:
>
> $ set -o extendedglob
> $ l=({A..Z} {a..z} {0..9} _)
> $ getrandom -a a -c20 -L1 -U$#l
> $ echo ${(j[])a//(#m)*/$l[MATCH]}
> EMhKvFuQlmCVSuet5uh0
>
> Doing it straight by reading /dev/urandom would have been a lot
> more awkward. Could that be improved/simplified?
It could be done, but a list of acceptable characters would need to be 
agreed upon and documented.  Are symbols acceptable?  About half of 
passwords require them, but only accept certain ones and that differs 
from website to website.
>


  reply	other threads:[~2022-11-18 17:09 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-02 17:13 [PATCH] zsh/random module Clinton Bunch
2022-11-03 17:50 ` Bart Schaefer
2022-11-04  3:17 ` dana
2022-11-04  6:22   ` Clinton Bunch
2022-11-04  7:27     ` dana
2022-11-04 12:57       ` Clinton Bunch
2022-11-08  0:18         ` [PATCH] zsh/random module [UPDATED] Clinton Bunch
2022-11-18 14:30           ` Clinton Bunch
2022-11-19  6:42             ` Lawrence Velázquez
2022-11-18 16:23           ` Stephane Chazelas
2022-11-18 17:08             ` Clinton Bunch [this message]
2022-11-18 18:12               ` Stephane Chazelas
2022-11-18 18:38                 ` Clinton Bunch
2022-11-23 19:52                   ` Daniel Shahaf
2022-11-24 16:19                     ` Stephane Chazelas
2022-11-24 16:30                       ` Roman Perepelitsa
2022-11-24 22:39                         ` Clinton Bunch
2022-11-25  8:53                           ` Stephane Chazelas
2022-11-25  9:40                             ` Stephane Chazelas
2022-11-28 16:37                               ` further discussion of zsh/random (was [PATCH] zsh/random module [UPDATED]) Clinton Bunch
2022-11-21  1:07           ` [PATCH] zsh/random module [UPDATED] Matthew Martin
2022-11-21  1:59             ` Clinton Bunch
2022-11-21  2:21               ` Matthew Martin
2022-11-21  2:57                 ` Clinton Bunch
2022-11-21  3:14                   ` Lawrence Velázquez
2022-11-21  4:17                     ` Bart Schaefer
2022-11-21  5:05                       ` Clinton Bunch
2022-11-22 13:42                         ` dana
2022-11-23 19:49                         ` Daniel Shahaf
2022-11-22 17:44                       ` Oliver Kiddle
2022-11-22 19:48                         ` Clinton Bunch
2022-11-23  1:23                   ` Matthew Martin
2022-11-23  2:58                     ` Clinton Bunch
2022-11-23  4:14                       ` Matthew Martin
2022-11-23 13:41                         ` Clinton Bunch
2022-11-23 20:33                           ` Daniel Shahaf
2022-11-23 21:42                             ` dana
2022-11-23 23:54                               ` Daniel Shahaf
2022-11-24  0:17                                 ` Daniel Shahaf
2022-11-24  1:05                                 ` dana
2022-11-24 13:52                               ` Clinton Bunch
2022-11-23 19:46           ` Daniel Shahaf
2022-11-24  2:58             ` Clinton Bunch
2022-11-24 10:07               ` nimaje+zml
2022-11-24 13:19                 ` Clinton Bunch
2022-11-24 14:33             ` Clinton Bunch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a0ea90d6-72c3-83e2-d4d2-0f9cafa63b4c@zentaur.org \
    --to=cdb_zsh@zentaur.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).