Click here to Skip to main content
Click here to Skip to main content

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);


$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.


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


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 17:13 
General'yall wrong PinsussAnonymous9-Sep-04 18:33 
GeneralGood PinsussAnonymous6-Jun-04 4:32 
GeneralSample script is WRONG PinmemberKelly Setzer15-Apr-03 13:10 
GeneralRe: Sample script is WRONG PinmemberJohn Aldrich13-Jun-03 19:31 
In the future, I would appreciate a private or atleast a bit more tacetly worded email informing me of any errors in my code rather than an off-color posting to the comments section of my article. That atleast gives me a chance to correct what was obviously an oversight on my part, which I'm more than happy to correct now that I'm aware of it. I endeavor to check my code to the best of my abilities, howver times arise when I am simply to busy or when worse comes to worse, I forget. I'd ask that you take that in consideration in future posts as the implied tone of your email is rather rude and unappreciated. The error will be fixed shortly.

It's good to see kids turning their minds to wholesum activities such as programming, instead of wasting their lives in the hedonistic disciplines of Sex, Drugs, & Rock & Roll... or Sex with Drugs, or Sex with Rocks while Rolling in Drugs, or whatever new-fangled perversions you little monsters have thought up now...
[Shog9 on Kid Programmers]
GeneralRe: Sample script is WRONG PinmemberBlake Coverett13-Jun-03 21:11 
GeneralRe: Sample script is WRONG PinmemberJohn Aldrich14-Jun-03 8:18 
GeneralRe: Sample script is WRONG PinmemberBlake Coverett14-Jun-03 14:45 
GeneralRe: Sample script is WRONG PinmemberJohn Aldrich14-Jun-03 18:10 
GeneralRe: Sample script is WRONG PinsussAnonymous9-Nov-04 12:43 
GeneralRe: Sample script is WRONG PinmemberChristoph Zurnieden11-Aug-06 15: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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 14 Jun 2003
Article Copyright 2003 by John Aldrich
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid