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

Perl

184 readers
1 users here now

founded 1 year ago
MODERATORS
10
fun with /dev/random (lemmy.sdf.org)
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/perl
 

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
[โ€“] [email protected] 2 points 1 year ago (1 children)

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:]' /cO(T$VNBT
=HsfK@gdJ34hv&amp;"+G)O$\AB.U
f/qEpZ^&amp;-.$cyR8N/JG'stTfV
kFG;Us|w^A9qp&amp;9#wzi/B@**]
=_.`nU&amp;]5L&amp;Id]%Y')@Bri3KX
Lu..z:f2Q&amp;#Q-a}E-.ZJ/"%!N
-Hu~YmwLv#Gp!j8Ap!#+EXRL2
[โ€“] sotolf 2 points 1 year ago

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