The Global Helpdesk Logo

Programing > Web > PHP

Correctly using crypt() with SHA512

Published By: lankanmon
On: 5 September 2013 at 12:52am

There are many ways to use crypt() in PHP, but I have found this to be useful and practical.

For generating Salt:

$salt = substr(str_shuffle("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345‌​6789"), 0, 8);

or if you have a linux server, this is a much more random way of generating salt

$fp = fopen('/dev/urandom', 'r');
$randomString = fread($fp, 32);

Base 64 encode to ensure that some characters will not cause problems for crypt

$salt = base64_encode($randomString);

For Hashing:

$hashed = crypt($passwordInput, '$6$'.$salt);

To Confirm:

    // Valid Action
    } else {
    // Invalid Action

I am open to any input, so please leave a comment if you have any improvements.





Share this page: PrintLights Out


More from lankanmon

Here are some other articles from the author of this page.

Correctly using crypt() with SHA512

-- There are many ways to use crypt() in PHP, but I have found this to be the most useful and practical.

Recovering drive with bad sectors without Formatting on Windows

-- This post will show how to recover a drive without having to reformat the drive with minimal to no data loss.

The Do's and Don'ts of Test Prep

-- An infographic that I made on the Do's and Don'ts of test prep.



No Comments Yet... Maybe you can be the first commenter!
Please login to your account to comment.


You are Already Logged in to your Lankanmon Inc. Account! Would you like to login on this site as well?


Yes, Log me In! No, I'll log in Later...