Alright people who know how to tell there computer what to do and how to get there... I bring you a challenge from the interweb.
THE 4K CODE CHALLENGE
Objective: To create the most productive/useful code that as source code is no larger than 4 kilobytes (4096 bytes). There is no restrictions as to what your code does, or what language it is used in.
Rules:
Gentlemen, START YOUR TEXTEDITORS
I've been learning Perl recently, and I made this small program to encrypt/decrypt text and files using one-time pads (more on one-time pads here, if there's interest: http://en.wikipedia.org/wiki/One-time_pad)
Don't expect this code to be very good, but I would love any constructive criticisim (or destructive, whatever)
#!/usr/bin/perl -w
use strict;
=pod
-g output-file num: generates keytext file of length num. makes file of length 1024
if num is omitted.
-e file-to-encrypt output-file a-keyfile : encrypts file-to-encrypt with a-keyfile. if
a-keyfile is omitted, script uses file 'keyfile'.
-d file-to-decrypt output-file a-keyfile : decrypts file-to-decrypt with a-keyfile. if
a-keyfile is omitted, script uses file 'keyfile'.
=cut
if (not @ARGV)
{
print "try supplying some arguments : )\nview the source for a guide.\n";
exit(0);
}
sub rand_char{
return chr(rand 255);
}
sub crypt($$$){
my($arg,$letter,$keyletter)=@_;
return
($arg eq "-e" )?chr(($letter+$keyletter)%255):
($arg eq "-d" )?chr(($letter-$keyletter)%255):
undef;
}
if($ARGV[0] eq '-ga' || $ARGV[0] eq '-g')
{
my $keytext='';
my $length=($ARGV[2])?$ARGV[2]:1024;
$keytext.=rand_char while $length--;
open KEYFILE, ">$ARGV[1]" or die "couldn't open $ARGV[1] for output: $!";
print KEYFILE $keytext;
}
wow, I already fucked that up. here's the bottom half of the code
if($ARGV[0] eq '-e'||$ARGV[0] eq '-ea'||$ARGV[0] eq '-d'||$ARGV[0] eq '-da')
{
open INPUT, "<$ARGV[1]" or die "couldn't open $ARGV[1] for input: $!";
open OUTPUT, ">$ARGV[2]" or die "couldn't open $ARGV[2] for output: $!";
open KEY, ($ARGV[3])?"<$ARGV[3]":"<keyfile";
my @input=split //, join("\n", <INPUT>);
my @key=split //, <KEY>;
foreach (@input)
{
my $letter=ord($_);
my $keyletter=ord(shift @key);
print OUTPUT &crypt($ARGV[0],$letter,$keyletter);
}
close INPUT;
close OUTPUT;
close KEY;
}