Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/One-time-pad/Raku/one-time-pad-1.raku
Normal file
29
Task/One-time-pad/Raku/one-time-pad-1.raku
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
sub MAIN {
|
||||
put "Generate data for one time pad encryption.\n" ~
|
||||
"File will have .1tp extension.";
|
||||
my $fn;
|
||||
loop {
|
||||
$fn = prompt 'Filename for one time pad data: ';
|
||||
if $fn !~~ /'.1tp' $/ { $fn ~= '.1tp' }
|
||||
if $fn.IO.e {
|
||||
my $ow = prompt "$fn aready exists, over-write? y/[n] ";
|
||||
last if $ow ~~ m:i/'y'/;
|
||||
redo;
|
||||
}
|
||||
last;
|
||||
}
|
||||
|
||||
put 'Each line will contain 48 characters of encyption data.';
|
||||
my $lines = prompt 'How many lines of data to generate? [1000] ';
|
||||
$lines ||= 1000;
|
||||
generate($fn, $lines);
|
||||
say "One-time-pad data saved to: ", $fn.IO.absolute;
|
||||
|
||||
sub generate ( $fn, $lines) {
|
||||
use Crypt::Random;
|
||||
$fn.IO.spurt: "# one-time-pad encryption data\n" ~
|
||||
((sprintf(" %s %s %s %s %s %s %s %s\n",
|
||||
((('A'..'Z')[crypt_random_uniform(26)] xx 6).join) xx 8))
|
||||
xx $lines).join;
|
||||
}
|
||||
}
|
||||
52
Task/One-time-pad/Raku/one-time-pad-2.raku
Normal file
52
Task/One-time-pad/Raku/one-time-pad-2.raku
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
sub s2v ($s) { $s.uc.comb(/ <[ A..Z ]> /)».ord »-» 65 }
|
||||
sub v2s (@v) { (@v »%» 26 »+» 65)».chr.join }
|
||||
|
||||
sub hide ($secret, $otp) { v2s(s2v($secret) »+» s2v($otp)) }
|
||||
sub reveal ($hidden, $otp) { v2s(s2v($hidden) »-» s2v($otp)) }
|
||||
|
||||
sub otp-data ($fn, $lines) {
|
||||
my $fh = $fn.IO.open :rw;
|
||||
my $data;
|
||||
my $count = 0;
|
||||
repeat {
|
||||
my $pos = $fh.tell;
|
||||
my $line = $fh.get;
|
||||
if $line.substr(0,1) ne '-'|'#' {
|
||||
$data ~= $line;
|
||||
$fh.seek($pos);
|
||||
$fh.put: '-' ~ $line.substr(1);
|
||||
$count++;
|
||||
}
|
||||
} until $count == $lines or $fh.eof;
|
||||
note "Insufficient lines of data remaining in $fn" if $count != $lines;
|
||||
$data;
|
||||
}
|
||||
|
||||
sub otp-size (Str $string) { ceiling $string.uc.comb(/ <[ A..Z ]> /) / 48 }
|
||||
|
||||
sub otp-encrypt ( $secret, $fn ) {
|
||||
my $otp-size = otp-size $secret;
|
||||
my $otp-data = otp-data($fn, $otp-size);
|
||||
my $encrypted = hide $secret, $otp-data;
|
||||
# pad encryted text out to a full line with random text
|
||||
$encrypted ~= ('A'..'Z').roll while $encrypted.chars % 48;
|
||||
join "\n", $encrypted.comb(6).rotor(8, :partial).map:
|
||||
{ sprintf "{ join ' ', "%s" xx $_ }", $_ };
|
||||
}
|
||||
|
||||
sub otp-decrypt ( $secret, $fn ) {
|
||||
my $otp-size = otp-size $secret;
|
||||
my $otp-data = otp-data($fn, $otp-size);
|
||||
my $plain-text = reveal $secret, $otp-data;
|
||||
join "\n", $plain-text.comb(6).rotor(8, :partial).map:
|
||||
{ sprintf "{ join ' ', "%s" xx $_ }", $_ };
|
||||
}
|
||||
|
||||
my $otp-encrypt-fn = 'rosettacode.1tp';
|
||||
my $otp-decrypt-fn = 'rosettacopy.1tp';
|
||||
|
||||
my $secret = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
|
||||
say "Secret:\n$secret\n\nEncrypted:";
|
||||
say my $encrypted = otp-encrypt $secret, $otp-encrypt-fn;
|
||||
say "\nDecrypted:\n", otp-decrypt $encrypted, $otp-decrypt-fn;
|
||||
Loading…
Add table
Add a link
Reference in a new issue