Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:15:40+00:00 2026-05-28T15:15:40+00:00

I found the following example on php.net to secure password before storing it. However,

  • 0

I found the following example on php.net to secure password before storing it. However, I don’t quite understand line 3. Could someone explain this a little bit? Thanks!

1 <?php
2 $password = crypt('mypassword'); // let the salt be automatically generated

3 if (crypt($user_input, $password) == $password) {
4   echo "Password verified!";
5 }
6 ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:15:41+00:00Added an answer on May 28, 2026 at 3:15 pm

    crypt is a one-way function and returns a string that already contains the salt,

    When comparing the user input with the crypt result, the function automatically extracts the salt from the string.

    To be more clear :

    crypt() outputs a string that contains both the salt and the result of the hash. When you pass it that string as a salt, it knows to extract only the salt part and ignore the hash part. And it still returns a string containing both the salt and the hash. So these strings can be compared

    You can clearly understand by:

    when user signups for the first time , the process is:

     $password = crypt($user_input); // let the salt be automatically generated
    
     if (crypt($user_input, $password) == $password) {
       echo "Password verified!";
     }
    

    when user tries to login , the process will be :

    if(crypt($user_passsword_currentlyin_db, $user_inputted_password) == $user_inputted_password) {
          echo "Password verified!";
    }
    

    Hope you get the thing 🙂

    EDIT:

    The output of crypt consists of:

    When you pass this output as “salt” back to crypt, it will extract the right algorithm and salt, and use these for the operation. If there is only an algorithm mentioned, it uses this one and generate random salt. Otherwise it will choose a default algorithm and generate random salt. The hash part in the passed salt parameter is ignored.

    So you can simply compare your stored_hash with crypt(password, stored_hash) – if it is equal, it quite likely was the right password.

    Here is an pseudocode explanation (in PHP-like syntax) how crypt works:

    function crypt($password, $salt)
    {
       if (substr($salt,0 1) == "_") {
         $count = substr($salt, 1, 4);
         $real_salt = substr($salt, 5, 4);
         return "_" . $count . $real_salt . crypt_ext_des($password, $count, $salt);
       }
       if(substr($salt, 0, 3) == "$1$") {
         list($ignored, $real_salt, $ignored) = explode("$", $salt);
         return "$1$" . $real_salt . "$" . crypt_md5($password, $real_salt);
       }
       if(substr($salt, 0, 4) == "$2a$") {
          $cost = substr($salt, 4, 2);
          $real_salt = substr($salt, 7, 22);
           return "$2a$" . $cost . "$" . $real_salt . crypt_brypt($password, $real_salt, $cost);
      }
      // ... SHA256 and SHA512 analogons
    
      // no match => STD_DES
      $real_salt = substr($salt, 0, 2);
      return $real_salt . crypt_std_des($password, $real_salt);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
For example the following cast can be found littered throughout the MSDN documentation: (LPTSTR)&lpMsgBuf
I found an example for async ftp upload on msdn which does the following
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
I found the following code example for Java on RosettaCode : public static boolean
Following program i picked from the php site (http://php.net/manual/en/mysqli.real-escape-string.php) and ran on my system
From another forum I found the following example: I was looking for a way
I have tried a very simple example of PHP MySQL Ajax following the example
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select
On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.