note 105949 added to function.crypt

From: Date: Tue, 27 Sep 2011 11:34:16 +0000
Subject: note 105949 added to function.crypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-183474@lists.php.net to get a copy of this message
I made a nice little wrapper function for crypt(): function hasher($info, $encdata = false) { $strength = "08"; //if encrypted data is passed, check it against input ($info) if ($encdata) { if (substr($encdata, 0, 60) == crypt($info, "$2a$".$strength."$".substr($encdata, 60))) { return true; } else { return false; } } else { //make a salt and hash it with input, and add salt to end $salt = ""; for ($i = 0; $i < 22; $i++) { $salt .= substr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", rand(0, 61), 1); } //return 82 char string (60bit hash & salt) return crypt($info, "$2a$".$strength."$".$salt).$salt; } } This wrapper will accept a string as input and hash it, and output the hash result of the string and salt together, plus the salt added on the end. You can then store that output in a db, and pass it on to the function as the 2nd parameter when you go to verify it, along with the user input or whatever as the first. Examples: $hash = hasher($userinput); if ($hash == hasher($userinput, $hash) {//authed} Neat huh? ---- Server IP: 69.147.83.197 Probable Submitter: 86.174.30.136 ---- Manual Page -- http://www.php.net/manual/en/function.crypt.php Edit -- https://master.php.net/note/edit/105949 Del: integrated -- https://master.php.net/note/delete/105949/integrated Del: useless -- https://master.php.net/note/delete/105949/useless Del: bad code -- https://master.php.net/note/delete/105949/bad+code Del: spam -- https://master.php.net/note/delete/105949/spam Del: non-english -- https://master.php.net/note/delete/105949/non-english Del: in docs -- https://master.php.net/note/delete/105949/in+docs Del: other reasons-- https://master.php.net/note/delete/105949 Reject -- https://master.php.net/note/reject/105949 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183474) next »