Just in time processing on a character stream: Difference between revisions

Content added Content deleted
(Added Perl example)
(→‎{{header|Perl 6}}: Made a bit more robust)
Line 571: Line 571:
{{works with|Rakudo|2019.03}}
{{works with|Rakudo|2019.03}}


This is a somewhat fragile toy encoder / decoder and shouldn't be used for anything serious.
This is a something of a toy encoder / decoder and probably shouldn't be used for anything serious.

Default encode/decode key is 'Perl 6' Feed it a pass phrase at the command line to use that instead.

Will handle any visible character in the ASCII range as well as space and new-line.


<lang perl6>#`[
<lang perl6>#`[
Set srand to set the encode / decode "key", and
Set srand to set the encode / decode "key".
Need to use the same "key" and same implementation
generate a lazy, reproducible stream of "random"
of Perl 6 to encode / decode. Gain "security" by
characters. Need to use the same key and same
exchanging "keys" by a second channel. Default
implementation of Perl 6 to encode / decode.
"key" is "Perl 6"
Gain "security" by exchanging keys by a second
channel. Default key is 'Perl 6'
]
]


Line 586: Line 589:
srand $key.comb(/<.alnum>/).join.parse-base(36) % 2**63;
srand $key.comb(/<.alnum>/).join.parse-base(36) % 2**63;


my @stream = (flat "\n", ' ' .. '~').roll(*);
# random but reproducible infinite stream of characters
my @stream = (flat ' ' .. '~').roll(*);


sub jit-encode (Str $str) {
sub jit-encode (Str $str) {
Line 596: Line 598:
my $h;
my $h;
my $l = '';
my $l = '';
++$i until $c eq @stream[$i];
++$i until $i > 1 && $c eq @stream[$i];
my $o = $i - $last;
my $o = $i - $last;
$l = $o % 26;
$l = $o % 26;
$h = $o - $l if $o > 26;
$h = $o - $l if $o >= 26;
$l += 10;
$l += 10;
$enc ~= ($h ?? $h.base(36).uc !! '') ~ ($l.base(36).lc);
$enc ~= ($h ?? $h.base(36).uc !! '') ~ ($l.base(36).lc);
$last = $i;
$last = $i;
}
}
$enc
my $block = 60;
$enc.comb($block).join: "\n"
}
}


sub jit-decode (Str $str) {
sub jit-decode (Str $str is copy) {
$str.=subst("\n", '', :g);
$str ~~ m:g/((.*?) (<:Ll>))/;
$str ~~ m:g/((.*?) (<:Ll>))/;
my $dec = '';
my $dec = '';
Line 620: Line 624:
}
}


my $secret = q:to/END/;
my $enc = jit-encode('In my opinion, this task is pretty silly.');
In my opinion, this task is pretty silly.

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.

!@#$%^&*()_+}{[].,><\|/?'";:1234567890
END

say "== Secret: ==\n$secret";

say "\n== Encoded: ==";
say my $enc = jit-encode($secret);

say "\n== Decoded: ==";
say jit-decode($enc);</lang>

<pre>== Secret: ==
In my opinion, this task is pretty silly.

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe.

!@#$%^&*()_+}{[].,><\|/?'";:1234567890


== Encoded: ==
26j52d6Ie1Ge4Cd26po1GdsQa3Ms52piQd4Cn3Md2Wcf1GtciQz1GwQb5Si2
WnQlQmQjQv1GmQra2Wt4Cpc1Gysatu26va1Gq52x4Cp1Gv4CeQv1Gb1Gp4Co
6IbaQyAUmd26a7Yw3Mh2Wu26v1GfQsQwbQpn26z1Gi1Gl1GmQb1Gfs26v4Ce
Qy2Wm78xaaa4Cj26x6If3Msqu2Wx2Wku1Gh52ydQh3Mb78rll1GvcQap5Sgy
Qm26s1Gh26clj1Gm1GzA4y26bat1Gdk1Gs1Gpm1GlQs7Ys52dQw1Giv5Se5S
u3Mb1Gucn4Cq26h26pQq2Wh5Sh7Yi8OrpQl26p26i3MqtQiQhQi4Ckb3Mi


== Decoded: ==
say "Encoded\n$enc\n\nDecoded\n", jit-decode($enc);</lang>
In my opinion, this task is pretty silly.


'Twas brillig, and the slithy toves
<pre>Encoded
Did gyre and gimble in the wabe.
26j52dhl2Wp1Gw2WceQj1Go3MyQc1Gd52a1Gb1Ga2Wcq26o4Cwf3MiQsQn2Wglk3MhkQyeQjas3MkQn2Wd26aaQcQj


!@#$%^&*()_+}{[].,><\|/?'";:1234567890</pre>
Decoded
In my opinion, this task is pretty silly.</pre>


=={{header|Python}}==
=={{header|Python}}==