Friday, July 09, 2010

One Time Pad in D

I don't have the original article, but someone was bragging about how small their one time pad code was in Python. Well, I took it as a challenge, so here's my version in D:

import std.mmfile;

void main(string[] args)
{
MmFile input = new MmFile(args[1]);
MmFile keyfile = new MmFile(args[2]);
MmFile output = new MmFile(args[1] ~ ".enc", MmFile.Mode.ReadWriteNew, input.length, null, 0);
for (size_t i = 0; i < input.length; i++)
{
output[i] = cast(ubyte)input[i] ^ cast(ubyte)keyfile[i];
}
}

It's rather minimalistic, but it was just an exercise to see how small I could make it. If you exclude the lines that are just braces, it's only 7 lines long. I tried to make it smaller, but I couldn't get map to work the way I wanted it to. Ah well...it's still cool.

Labels: ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home