Home > Mobile >  Has anything changed with /dev/urandom on MacOS Monterey?
Has anything changed with /dev/urandom on MacOS Monterey?

Time:01-03

I used to be able to generate a random character sequence from /dev/urandom using the following line:

cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9-_!@#$%^&*()_ {}|:<>?=' | fold -w 100 | head -n 1 | grep -i '[!@#$%^&*()_ {}|:<>?=]' | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'

but this no longer functions after I upgraded to Monterey. The output I receive is nothing more than Input error. Has something changed with this OS version? How can I now generate a sequence of 100 random characters from /dev/urandom?

CodePudding user response:

Your command does not work for me on macOS Big Sur (11.6) with UTF8 locale:

$ sw_vers
ProductName:    macOS
ProductVersion: 11.6.2
BuildVersion:   20G314
$ locale
LANG=""
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
$ cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9-_!@#$%^&*()_ {}|:<>?=' | fold -w 100 | head -n 1 | grep -i '[!@#$%^&*()_ {}|:<>?=]' | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
tr: Illegal byte sequence
$

Try using LC_ALL=C instead of LC_CTYPE=C:

$ cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9-_!@#$%^&*()_ {}|:<>?=' | fold -w 100 | head -n 1 | grep -i '[!@#$%^&*()_ {}|:<>?=]' | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
TU?S xn4>-|C-SZgBIU2Rm92Tj4Ijq5>V6Oe_:Je{%S9D{7zvey7LnSL7{j%xhDxz%JvAiUu}-aE-M:kk&jjpFtZ*3@#5@)<>>i_
$

And what about this?

$ LC_ALL=C tr -cd '[:alnum:][:punct:]' < /dev/urandom | head -c100; echo
<NhlGfe}]\ggDPiY&Cq_3=i=k}6~_j2seQ_wnF<n`l8eF~6c( 2z0f4V4oW%5'mQv*3*gaS8eaN)5W{YxZ73O`e88=!kAyOI!ks5
$ LC_ALL=C tr -cd '[:alnum:][:punct:]' < /dev/urandom | head -c100; echo
t\gLx;|J~>=P,8wQboiK2e*5dMhN%nBhP5)>a]<9Mt&J6^&2Av1O>rs{fm@\J J9#H9f2*6UgUK]1J624vRu^#7\gVR jL=r"2&6
$

CodePudding user response:

In Monterey, locale shows LANG as set, and the grand override value, LC_ALL as unset, as it is typically set elsewhere for example https://docs.oracle.com/cd/E19455-01/806-0169/utf8-98/index.html To make this work in Monterey, just change the language or the override to the POSIX standard default:

LANG=C or LC_ALL=C

In Monterey, the character type locale could be set to: LC_CTYPE=en.US.UTF-8 to avoid angle-bracket-enclosed escape codes which sneak through given that LANG is set now.

  • Related