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
[–] sotolf 2 points 1 year ago

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