Code: Select all
dd if=/dev/urandom bs=12 count=1 status=none | base64(The longer Internet version is along the lines of
Code: Select all
dd if=/dev/urandom count=1 2> /dev/null | uuencode -m - | sed -ne 2p | cut -c-8Code: Select all
dd if=/dev/urandom bs=12 count=1 status=none | base64Code: Select all
dd if=/dev/urandom count=1 2> /dev/null | uuencode -m - | sed -ne 2p | cut -c-8Code: Select all
tr -dc '[allovedcharacters]' < /dev/random | head -c <length>Code: Select all
0100100100100000011000010110110100100000
0100111001100001010011100010000100100000
0100100100100000011000010110110100100000
0110000100100000011011010110000101101110
00100001Code: Select all
# passphrase
bewaitered*Aile%retrovaccinate
tycoondiceboxsuppleheptene
patientpoisedkenoticxerotic.
pyxides sail letter distad.
calaisdeadeyeinfamyamididCode: Select all
#!/bin/sh
set -efu
declare -a w n
w=( $( shuf -n3 /usr/share/dict/words ) )
n=( $( shuf -n2 -e \! \" \# \$ \% \& \' \( \) \* \+ \, \- \. \/ \: \; \< \= \> \? \@ \[ \\ \] \^ \_ \` \{ \| \} \~ ) )
for i in {0..2}; do
if [[ $(( $RANDOM % 2 )) = 0 ]]; then
w[$i]="${w[$i]^}"
fi
done
printf '%s%s%s%s%s\n' "${w[0]}" "${n[0]}" "${w[1]}" "${n[1]}" "${w[2]}"
###############################################################################
# Remove words with:
# - too many or too few characters
# - apostrophes, hyphens, or capitalization
# - Removing spaces is optional. (eccerr0r)
echo $(egrep '^[a-z]{4,7}$' /usr/share/dict/words|shuf -n4)|tr -d ' '
###############################################################################
# - avoid use of tr with printf (Hu)
printf '%s%s%s%s\n' $(grep -E '^[a-z]{4,7}$' /usr/share/dict/words|shuf -n4).
# With spaces
printf '%s %s %s %s\n' $(grep -E '^[a-z]{4,7}$' /usr/share/dict/words|shuf -n4).
# work regardless of the shuffle count
# relies on printf to
# - discard the whitespace (newlines)
# - then uses a bare echo to emit a newline at the end.
{ printf '%s' $(grep -E '^[a-z]{4,7}$' /usr/share/dict/words|shuf -n4); echo; }Code: Select all
openssl rand -base64 12
This is good but it demonstrates a common misunderstanding as to how tr(1) works. The square brackets should not be present unless either a) you wish for both brackets to be among the characters to be selected or b) you are writing a character class. For example:Zucca wrote:I've usedCode: Select all
tr -dc '[allovedcharacters]' < /dev/random | head -c <length>
Code: Select all
# This is fine.
tr -dc 'allowedliterals'
# And so is this.
tr -dc '[:alnum:]'Code: Select all
# Where LC_CTYPE is effectively a UTF-8 locale, tr(1) will be unable to decode
# the input stream as UTF-8, with some implementations raising errors such as
# "tr: Illegal byte sequence". Coreutils tr(1) tolerates it, however.
tr -dc '[:alnum:]' < /dev/urandom
# Safer (and a bit faster, if using coreutils). Recommended if writing code that
# you intended to save or keep.
LC_ALL=C tr -dc '[:alnum:]' < /dev/urandomI'm not sure if I meant to write <allowedcharacters> (as in mandatory argument) or if I mistakenly added '[]' there.RumpletonBongworth wrote:This is good but it demonstrates a common misunderstanding as to how tr(1) works. The square brackets should not be present unless either a) you wish for both brackets to be among the characters to be selected or b) you are writing a character class.Zucca wrote:I've usedCode: Select all
tr -dc '[allovedcharacters]' < /dev/random | head -c <length>
And in some occurances setting LC_ALL=C will even boost the performance. I remember at least grep gaining a boost. But I've not encountered a tr version where unicode was a problem to it. Although my testing only includes coreutils and busybox. toybox, oddly, doesn't seem to have tr.RumpletonBongworth wrote:Code: Select all
# Where LC_CTYPE is effectively a UTF-8 locale, tr(1) will be unable to decode # the input stream as UTF-8, with some implementations raising errors such as # "tr: Illegal byte sequence". Coreutils tr(1) tolerates it, however. tr -dc '[:alnum:]' < /dev/urandom # Safer (and a bit faster, if using coreutils). Recommended if writing code that # you intended to save or keep. LC_ALL=C tr -dc '[:alnum:]' < /dev/urandom
Code: Select all
0100100100100000011000010110110100100000
0100111001100001010011100010000100100000
0100100100100000011000010110110100100000
0110000100100000011011010110000101101110
00100001
Yeah. For some reason, tr(1) tends to get a free pass when it comes to disregarding the effective character set and being multibyte-encoding unaware. Better implementations can be found in the BSD camp. Here's an interesting test that I conducted in macOS.Zucca wrote:But I've not encountered a tr version where unicode was a problem to it. Although my testing only includes coreutils and busybox. toybox, oddly, doesn't seem to have tr.
Code: Select all
$ printf '\303\277' | tr -d '\377' | od -t x1
0000000 0a
0000001Code: Select all
$ printf '\303\277' | tr -d '\377' | od -t x1
0000000 c3 bf
0000002Code: Select all
¨oCode: Select all
öCode: Select all
0100100100100000011000010110110100100000
0100111001100001010011100010000100100000
0100100100100000011000010110110100100000
0110000100100000011011010110000101101110
00100001
It doesn't. In fact, it cannot without a substantial update. Another example may prove illustrative, so let's compare coreutils tr(1) to GNU sed(1).Zucca wrote:Also the coreutils example you showed... seems to work correctly.
Perhaps I'm missing something... Since I haven't played with unicode related stuff too much.
Code: Select all
$ uname -o
GNU/Linux
$ printf '\303\277' | sed -e 's/[[:alpha:]]//' | od -An -t x1
$Code: Select all
$ uname -o
GNU/Linux
$ printf '\303\277' | tr -d '[:alpha:]' | od -An -t x1
c3 bf
$Code: Select all
$ uname -o
Darwin
$ printf '\303\277' | tr -d '[:alpha:]' | od -An -t x1
$Code: Select all
$ uname -o
Darwin
$ tr -dc '[:alpha:]' < /dev/urandom
tr: Illegal byte sequenceCode: Select all
zucca@NBLK-WAX9X ~ $ printf 'ö' | od -An -t o1
303 266
zucca@NBLK-WAX9X ~ $ printf 'ä' | od -An -t o1
303 244
zucca@NBLK-WAX9X ~ $ printf '\303\244' | tr -d 'a-ö' | od -An -t x1
zucca@NBLK-WAX9X ~ $ printf '\303\244' | tr -d 'a-\303\244' | od -An -t x1Code: Select all
0100100100100000011000010110110100100000
0100111001100001010011100010000100100000
0100100100100000011000010110110100100000
0110000100100000011011010110000101101110
00100001