I am having problems with encrypting passwords. When a user registers for my site, their password is encrypted with the function below (which I lifed from Apache::Htpasswd - this module is not available on my server and I do not have permission to install it,) the password is stored in my database, and an entry is entered into the .htpasswd file. Everything is fine so far. If they want to change their password, I prompt them (a different script) for the old password, which I then encrypt with the same function and compare with the one stored in the database. Invariably, the encryption result is different (however is still a vaild encryption of the password.) Is there a reason the result changes depending on what script I call the function from?
#From my first script, test data; result = reu3tDNEHBSyc CryptPasswd("username","password"); #From my second script, test data; result = secGBN1BHq1FA CryptPasswd("username","password"); sub CryptPasswd { my ($self) = shift; my ($passwd, $salt) = @_; if ($salt) { # Make sure only use 2 chars $salt = substr ($salt, 0, 2); } else { ($salt = substr ($0, 0, 2)) =~ tr/:/C/; } return crypt ($passwd, $salt); }
If there is a good reason for this happening, can someone suggest a way for me to confirm the old password in order to change the new one?

In reply to encryption confusion by poprishchin

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.