Click here to Skip to main content
14,975,961 members
Articles / Programming Languages / Perl
Posted 18 Feb 2003


7 bookmarked

Generating "Random" Strings for PERL-based Cryptography

Rate me:
Please Sign up or sign in to vote.
2.92/5 (9 votes)
13 Jun 20031 min read
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 Pin
jc_cpu25-May-06 16:13
Memberjc_cpu25-May-06 16:13 
General'yall wrong Pin
Anonymous9-Sep-04 17:33
MemberAnonymous9-Sep-04 17:33 
GeneralGood Pin
Anonymous6-Jun-04 3:32
MemberAnonymous6-Jun-04 3:32 
GeneralSample script is WRONG Pin
Kelly Setzer15-Apr-03 12:10
MemberKelly Setzer15-Apr-03 12:10 
GeneralRe: Sample script is WRONG Pin
John Aldrich13-Jun-03 18:31
MemberJohn Aldrich13-Jun-03 18:31 
GeneralRe: Sample script is WRONG Pin
Blake Coverett13-Jun-03 20:11
MemberBlake Coverett13-Jun-03 20:11 
GeneralRe: Sample script is WRONG Pin
John Aldrich14-Jun-03 7:18
MemberJohn Aldrich14-Jun-03 7:18 
GeneralRe: Sample script is WRONG Pin
Blake Coverett14-Jun-03 13:45
MemberBlake Coverett14-Jun-03 13:45 
GeneralRe: Sample script is WRONG Pin
John Aldrich14-Jun-03 17:10
MemberJohn Aldrich14-Jun-03 17:10 
GeneralRe: Sample script is WRONG Pin
Anonymous9-Nov-04 11:43
MemberAnonymous9-Nov-04 11:43 
GeneralRe: Sample script is WRONG Pin
Christoph Zurnieden11-Aug-06 14:56
MemberChristoph Zurnieden11-Aug-06 14:56 

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

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