security icon

createpad.pl

#!/usr/bin/perl

use strict;

# Syntax: perl createpad.pl onetimepad_file pad_size

# This creates a file with pad_size bytes of random data prefixed with a bookmark
# that points to the first byte of random data.

# Execution starts here
&main;

sub main()
{
	my $intNumArgs = $#ARGV + 1;
	if ($intNumArgs != 2)
	{
		print "Syntax: perl createpad.pl onetimepad_file pad_size\n";
		return 1;
	}
	
	# Read the arguments on the command line
	my $strPadFilename = $ARGV[0];
	my $intPadSize = $ARGV[1]; # excludes bookmark at start of file
	
	print "Creating pad ".$strPadFilename."\n";

	# Create a file for the one-time pad
	if (!open PAD_FILE, ">$strPadFilename")
	{
		print "ERROR:Can't create file $strPadFilename\n";
		return 1;
	}
	# Handle the file as bytes instead of text
	binmode PAD_FILE;

	# Write bookmark value (32 bit)
	my $lngBookmark = 0x00000004;
	print PAD_FILE pack('L', $lngBookmark);
	
	# Write one-time pad data
	my $intRange = 256;
	for(my $intPadCount = 0; $intPadCount < $intPadSize; $intPadCount++)
	{
		my $intRandomNumber = int(rand($intRange));
		print PAD_FILE pack('C', $intRandomNumber);
	}
	close PAD_FILE;

	print "File created\n";
	return 0;
}

    

Home page