10
submitted 1 year ago* (last edited 1 year ago) by dark776174657273 to c/perl@programming.dev

I've never had a need to use /dev/random before, but then I saw this in some documentation I was reading:

$ head -c20 /dev/random | base64
ZZXB6yF9bZvp103CI3lcREcBVEA=

Which got me thinking about my password generator. This is trivial, but I thought it was fun.

better-password-gen.pl

#!/usr/bin/env perl
use strict; use warnings; use Data::Dumper;
use feature qw< say >;

my @CList = ('a'..'z', 'A'..'Z', 0..9, split(//, q<\<\>\\/()[]{}:;"'`~#!@$%^&*-_=+>));
my $pwdLen = shift @ARGV // 12;
my $BUFF = "\x0" x $pwdLen;
open( my $RND, '<', '/dev/random' ) or die $!;
for (0..9){
    read $RND, $BUFF, $pwdLen;
    say join('', map {$CList[ord($_) % @CList]} split(//, $BUFF));
}
close($RND);

output:

$ ./better-password-gen.pl 25
M6V/$U(CGQ\p!d\lg=9>GpO[x
hlAN*"XiXnShRy)DB6GOCv;7j
NRXqeqSCZ^I\Tt2Bore4t1hYX
~4dIXVo+opRJfu6'b}u'y=N$:
6he=<mYdH'{P>Z4nDlfe4fk1T
Ffv28dOEu1fZxr9<VMhl$nS"n
yt[ol0;me'Rd2'6#ZD7!7plf[
=LZ^68!EOtP6EYJNq^;3T48HJ
V<3fcA%\b@:+"wghSX^)\S/cH
*1q08-+Zo`PHibJA<b)oeM!nx
you are viewing a single comment's thread
view the rest of the comments
[-] sotolf@programming.dev 4 points 1 year ago

The output seems pretty similar to using

tr -cd '[:graph:]' &lt; /dev/urandom | head -c 256

Which is pretty nice as well :) Basically just take /dev/urandom and throw away everything that isn't a typeable character :)

[-] dark776174657273 2 points 1 year ago

tr -cd '[:graph:]' < /dev/urandom | head -c 256

Hey, thanks! I didn't know about the :graph: character set.

$ for i in $(seq 1 10); do tr -cd '[:graph:]' </dev/urandom | head -c 25; echo; done
!jzazhBd-MV9;;`U;G3"DF1Zm
,{*wQ}I3.U;M:bu]'?{nFk.8Y
'`g;7cCp5kBh`*9M$(2Fj@)L*
R)y-"jWn{1G5uh>/cO(T$VNBT
=HsfK@gdJ34hv&"+G)O$\AB.U
f/qEpZ^&-.$cyR8N/JG'stTfV
kFG;Us|w^A9qp&9#wzi/B@**]
=_.`nU&]5L&Id]%Y')@Bri3KX
Lu..z:f2Q&#Q-a}E-.ZJ/"%!N
-Hu~YmwLv#Gp!j8Ap!#+EXRL2
[-] sotolf@programming.dev 2 points 1 year ago

It comes in handy for some weird things like this :)

this post was submitted on 29 Jul 2023
10 points (100.0% liked)

Perl

183 readers
1 users here now

founded 1 year ago
MODERATORS