note 97536 added to function.sha1

From: Date: Sun, 25 Apr 2010 01:03:33 +0000
Subject: note 97536 added to function.sha1
Groups: php.notes 
Request: Send a blank email to php-notes+get-168608@lists.php.net to get a copy of this message
I have created simple functions that allow you to create tokens that expire within a certain amount of time. Call generateTimedHash with your data to encode and a salt: <?php $token = generateTimedHash("This is data.", "SALTY"); ?> Now you can call verifyTimedHash, with your token, data, salt, and lifetime of the token. The function will return true if the token is correct and within the lifetime of the token. It will return false if the token is incorrect and/or the token has expired. To verify a token that expires 30 seconds after it is created, use this: <?php $valid = verifyTimedHash($token, "This is data.", "SALTY", 30); ?> The functions: <?php function generateTimedHash($data, $salt) { // Salt the token with a defined salt and the time return sha1($data . $salt . time()); } function verifyTimedHash($token, $data, $salt, $life) { // Get the current time $time = time(); // Verify hash with current time if ($token == sha1($data . $salt . $time)) return true; // If that didn't work, try verifiying it, each time subtracting 1 from $time, $life times for ($offset = 1; $offset <= $life; $offset++) if ($token == sha1($data . $salt . ($time - $offset))) return true; return false; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 69.142.233.248 ---- Manual Page -- http://www.php.net/manual/en/function.sha1.php Edit -- https://master.php.net/note/edit/97536 Del: integrated -- https://master.php.net/note/delete/97536/integrated Del: useless -- https://master.php.net/note/delete/97536/useless Del: bad code -- https://master.php.net/note/delete/97536/bad+code Del: spam -- https://master.php.net/note/delete/97536/spam Del: non-english -- https://master.php.net/note/delete/97536/non-english Del: in docs -- https://master.php.net/note/delete/97536/in+docs Del: other reasons-- https://master.php.net/note/delete/97536 Reject -- https://master.php.net/note/reject/97536 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168608) next »