crypt accesses the standard Unix crypt() function, which takes a string and a salt (a random two-byte string) and using a one-way hash function encrypts the password. The salt gets assigned randomly when the password is created, and its purpose is that even if you use the same password on two different places, the encrypted passwords will be different. There are some packages in CPAN that provide similar interfaces using MD5 and other algorithms. Do a search for "crypt".

Sample use for crypt would be as follows. The first function receives the plain-text password and returns the encrypted password string. The second one takes an encrypted password string (as generated by the first function) and a plain-text password, and tell you whether the password is correct. Notice how the salt is stored in the first two bytes of the encrypted password. Also note that crypt only uses the first 8 characters of the password.

sub encrypt_passwd { my $pw=shift; # Seed random number generator. From Camel book p. 223. # This should be outside this function, in the program # initialization, otherwise calls to this function very # close in time will result in the same salt. srand ( time() ^ ($$ + ($$ << 15)) ); my @c=('a'..'z', 'A'..'Z', '0'..'9','.','/'); my $s=$c[rand(@c)].$c[rand(@c)]; return crypt($pw, $s); } sub verify_passwd { my ($epw, $pw)=@_; my $s=substr($epw,0,2); return $epw eq crypt($pw,$s); }

In reply to Re: P@$$w0rd$ in perl? by ZZamboni
in thread P@$$w0rd$ in perl? by skazat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.