Last active
December 17, 2020 00:38
-
-
Save gabrielhesposito/9c3766b726d1ed09b693b66bcf414f1f to your computer and use it in GitHub Desktop.
cat_and_dev_random_in_perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# object to make equivalent | |
# cat /dev/random | |
#!/bin/perl | |
use strict; | |
use Data::Dumper; | |
#just an interface for dev/random ..urandom? | |
use Crypt::Random qw( makerandom_octet); | |
#entropy | |
use Net::Ping; | |
##OO function b/c lib example from perl.org/Net/Ping | |
## cycles through ips, trims off last digit in duration of ping | |
## sums up ^^ returns value | |
sub entropy | |
{ | |
my $p = Net::Ping->new('icmp'); | |
my @friendly_hosts = ('8.8.8.8', '8.8.4.4'); | |
my $calc_num=0; | |
foreach my $host (@friendly_hosts) | |
{ | |
my ($failure, $duration, $dest_ip) = $p->ping($host); | |
#print " ".$failure." ".$duration." ".$dest_ip." "; | |
## converstion of type is important - ~ might not work right otherwise | |
my $duration_string = sprintf("%10.20s", $duration); | |
my @duration_array = split //, $duration_string; | |
$calc_num = $calc_num + pop(@duration_array) | |
} | |
return $calc_num; | |
} | |
sub cat | |
{ | |
#print $_[0]; | |
#use dumper to avoid breaking terminal/multiplexer | |
print Dumper($_[0]); | |
} | |
sub dev_random | |
{ | |
my $rand_block = makerandom_octet ( Length => $_[0] ); | |
return $rand_block; | |
} | |
# like a command line | |
sub main | |
{ | |
my $final_block; | |
for (my $i=entropy(); $i <= 50; $i++) | |
{ | |
$final_block = $final_block . dev_random( entropy() ) | |
} | |
cat( $final_block ); | |
} | |
# | |
# block size | |
# | |
my $control_int = entropy(); | |
main( $control_int ); |
rev3 - got ping working testing entropy function
rev5 - done
$ sudo perl dev_random.pl
result (trimmed b/c warning from github ..actual variable dump much larger....can still break terminal):
$VAR1 = '7��C�!����hSr����%\\';
Dependency
cpan install Crypt::Random
Final thoughts; this could have been done with bash and existing /bin/ utilities easier... entropy is not done properly... using ping time for random ints is risky as cache at different levels could result in a constant trip time etc etc.
Explanation of code; /dev/random is called via a perl interface, each call returns a different size block, the concatenation of the of those blocks results in the final dump
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rev1- functional ints
rev2- cleaned rev1 + moved to octet more like /dev/random