The command `strings` produce abnormal output
Normally the shell command cat /dev/urandom | strings -n16
would produce a list of ASCII strings useful for picking passwords. However, after the latest upgrade to MacOS Catalina strings
output garbage ala:
`�e�ID�b,�e���S��qx�u�
7�Ա�H�t�>��L�b2
�g�(���<:�}�TZ,=���צ{���
�;y��9M��(���}=M
��fa��>�-�3&�syiJ`\t}�Dde7�
����\�^���1�uN{>l�V�L3
�E�y�?6�5q��Ii9�
...
What gives?
Solution 1:
It seems that the strings
command now honours the locale settings in the environment, in particular
LC_CTYPE
This variable determines the locale category for character handling functions, such as tolower(), toupper() and isalpha(). This environment variable determines the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters), the classification of characters (for example, alpha, digit, graph) and the behaviour of character classes. Additional semantics of this variable, if any, are implementation-dependent.
Setting LC_CTYPE
to C
restores the previous behaviour:
% cat /dev/urandom | LC_CTYPE=C strings -n 16
X! \*H=:e9c1`nzWk
$Hr/Q9q_b:BE-tS,[
/]59=+fPU"x-B9kk
:"*J9{5Q-tFpL=Xm$>
...
An alternative approach (taken from Shell Script /dev/urandom on Stack Overflow with minor modifications) is to filter out non-printable characters with the tr
tool and take the first 16 characters with dd
:
% cat /dev/urandom | LC_CTYPE=C tr -dc '[:print:]' | dd bs=16 count=1 2> /dev/null
This is much faster because it does not wait for 16 consecutive printable characters from the random source.