Just in time processing on a character stream: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (Added Perl example) |
Thundergnat (talk | contribs) (→{{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 |
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" |
Set srand to set the encode / decode "key". |
||
⚫ | |||
generate a lazy, reproducible stream of "random" |
|||
⚫ | |||
⚫ | |||
exchanging "keys" by a second channel. Default |
|||
⚫ | |||
"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; |
||
⚫ | |||
# random but reproducible infinite stream of characters |
|||
⚫ | |||
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; |
||
} |
} |
||
$ |
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/; |
|||
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: =="; |
|||
⚫ | |||
<pre>== Secret: == |
|||
⚫ | |||
'Twas brillig, and the slithy toves |
|||
Did gyre and gimble in the wabe. |
|||
!@#$%^&*()_+}{[].,><\|/?'";:1234567890 |
|||
⚫ | |||
26j52d6Ie1Ge4Cd26po1GdsQa3Ms52piQd4Cn3Md2Wcf1GtciQz1GwQb5Si2 |
|||
WnQlQmQjQv1GmQra2Wt4Cpc1Gysatu26va1Gq52x4Cp1Gv4CeQv1Gb1Gp4Co |
|||
6IbaQyAUmd26a7Yw3Mh2Wu26v1GfQsQwbQpn26z1Gi1Gl1GmQb1Gfs26v4Ce |
|||
Qy2Wm78xaaa4Cj26x6If3Msqu2Wx2Wku1Gh52ydQh3Mb78rll1GvcQap5Sgy |
|||
Qm26s1Gh26clj1Gm1GzA4y26bat1Gdk1Gs1Gpm1GlQs7Ys52dQw1Giv5Se5S |
|||
u3Mb1Gucn4Cq26h26pQq2Wh5Sh7Yi8OrpQl26p26i3MqtQiQhQi4Ckb3Mi |
|||
⚫ | |||
⚫ | |||
In my opinion, this task is pretty silly. |
|||
'Twas brillig, and the slithy toves |
|||
⚫ | |||
Did gyre and gimble in the wabe. |
|||
26j52dhl2Wp1Gw2WceQj1Go3MyQc1Gd52a1Gb1Ga2Wcq26o4Cwf3MiQsQn2Wglk3MhkQyeQjas3MkQn2Wd26aaQcQj |
|||
!@#$%^&*()_+}{[].,><\|/?'";:1234567890</pre> |
|||
⚫ | |||
⚫ | |||
=={{header|Python}}== |
=={{header|Python}}== |