note 82200 added to function.base64-encode
| From: | Andi at osu1 dot php dot net | Date: | Mon, 31 Mar 2008 21:54:18 +0000 |
| Subject: | note 82200 added to function.base64-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137799@lists.php.net to get a copy of this message | ||
I needed a simple way to obfuscate auto_increment primary keys in databases when they are visible to
users in URIs or API calls. The users should not be able to increment the id in the URL and see the
next data record in the database table.
My solution (uses modified base64 functions by Tom):
function base64url_encode($plainText) {
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_,');
return $base64url;
}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');
$base64 = base64_decode($base64url);
return $base64;
}
function encryptId($int, $class='') {
return base64url_encode($int.'-'.substr(sha1($class.$int.encryptionKey), 0, 6));
}
function decryptId($string, $class='') {
$parts = explode('-', base64url_decode($string));
if (count($parts) != 2) {
return 0;
}
$int = $parts[0];
return substr(sha1($class.$int.encryptionKey), 0, 6) === $parts[1]
? (int)$int
: 0;
}
- The optional 2nd argument is the class name, so two equal ids of different tables will not result
in two equal obfuscated ids.
- encryptionKey is a global secret key for encryption.
- decryptId() checks if the second part of the base64 encoded string is correct.
----
Server IP: 69.147.83.197
Probable Submitter: 84.166.64.135
----
Manual Page -- http://www.php.net/manual/en/function.base64-encode.php
Edit -- https://master.php.net/note/edit/82200
Del: integrated -- https://master.php.net/note/delete/82200/integrated
Del: useless -- https://master.php.net/note/delete/82200/useless
Del: bad code -- https://master.php.net/note/delete/82200/bad+code
Del: spam -- https://master.php.net/note/delete/82200/spam
Del: non-english -- https://master.php.net/note/delete/82200/non-english
Del: in docs -- https://master.php.net/note/delete/82200/in+docs
Del: other reasons-- https://master.php.net/note/delete/82200
Reject -- https://master.php.net/note/reject/82200
Search -- https://master.php.net/manage/user-notes.php