Click here to Skip to main content
Click here to Skip to main content
Go to top

Generating "Random" Strings for PERL-based Cryptography

, 13 Jun 2003
Rate this:
Please Sign up or sign in to vote.
This article will detail a relatively simple method for generating a random alphanumeric string for using in the implementation of various cryptographic / security schema with PERL.

Password Generation

Perl is a powerful language in that it makes manipulation of nearly all types of data a breeze. What takes scores of lines with C++ or some other language like Java, can be accomplished with minimal effort in Perl. When performing cryptographic related work, this can be a valuable asset, especially when you need to generate information quickly. Let's say for example that you need to quickly generate a random password to secure your site...You could use code like this to make it happen...

sub generate_random_password 
{
    my $passwordsize = shift;
    my @alphanumeric = ('a'..'z', 'A'..'Z', 0..9);
    my $randpassword = join '', 
           map $alphanumeric[rand @alphanumeric], 0..$passwordsize;

    return $randpassword;
}

The code is called in this manner:

$returnvalue = generate_random_password(64);

OR

$returnvalue = &generate_random_password(64);

These are the two contexts in which an unpackaged function may be called in Perl, any other method will throw an error and make your program act like it got hold of some bad LSD....anyhow... What code does is accept $passwordsize as a length argument and returns a string of the specified length using interpolation between 0 and the length passed to the function. You can also use it to generate pad files for OTP encryption. Simply pass it a password size that is symmetric to the size of the data you are encrypting and dump the contents into a file. That simple. This function is small but useful, hence I'm posting it here. Check it out, and I hope you'll find use for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

John Aldrich
Web Developer
United States United States
My name is John Aldrich. I have pursued programming as a hobby for the past 6 years and currently have experience in Perl (basic / intermediate), HTML (advanced), and I have recently begun to learn C/C++. I also have a profound interest in all things graphics related and and constantly working to improve my knowledge in all areas of computing. I run a home based web software company named Professional Design Resources. If you are interested in any custom programming or would be interested in collaberating on a joint project, please feel free to contact me via email, where I'll be happy to discuss such things. Serious projects only please.

Comments and Discussions

 
GeneralInteresting Pinmemberjc_cpu25-May-06 16:13 
General'yall wrong PinsussAnonymous9-Sep-04 17:33 
GeneralGood PinsussAnonymous6-Jun-04 3:32 
GeneralSample script is WRONG PinmemberKelly Setzer15-Apr-03 12:10 
GeneralRe: Sample script is WRONG PinmemberJohn Aldrich13-Jun-03 18:31 
GeneralRe: Sample script is WRONG PinmemberBlake Coverett13-Jun-03 20:11 
GeneralRe: Sample script is WRONG PinmemberJohn Aldrich14-Jun-03 7:18 
GeneralRe: Sample script is WRONG PinmemberBlake Coverett14-Jun-03 13:45 
GeneralRe: Sample script is WRONG PinmemberJohn Aldrich14-Jun-03 17:10 
GeneralRe: Sample script is WRONG PinsussAnonymous9-Nov-04 11:43 
GeneralRe: Sample script is WRONG PinmemberChristoph Zurnieden11-Aug-06 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 14 Jun 2003
Article Copyright 2003 by John Aldrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid