note 85831 added to function.base64-encode
| From: | gglocknerATNOSPAMdwafflerDOTcom at osu1 dot php dot net | Date: | Fri, 19 Sep 2008 13:35:56 +0000 |
| Subject: | note 85831 added to function.base64-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144546@lists.php.net to get a copy of this message | ||
I have another solution that is simple and elegant. Create a pseudorandom string of characters.
Then, each time you want to obfuscate your key, append a random substring from the pseudorandom
string and use base64 encoding. When you want to de-obfuscate, convert back from base64. If the
prefix is not in your pseudorandom source, then the value is forged. Otherwise, strip the prefix
and recover your original key.
The advantages are that the string will look different even for the same key, and encoding and
decoding should be extremely fast.
Here's an example:
<?php
// Call makeCksum once upon landing on the homepage
function makeCksum() {
for ($i=0;$i<32;++$i)
$str .= chr(rand(32,126));
$_SESSION['Cksum'] = $str;
}
function encode($x) {
return base64_encode(substr($_SESSION['Cksum'],rand(0,28),4) . $x);
}
function decode($x) {
$y = base64_decode($x);
if (strpos($_SESSION['Cksum'],substr($y,0,4)) === false) return false;
return substr($y,4-strlen($y));
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 24.19.245.57
----
Manual Page -- http://www.php.net/manual/en/function.base64-encode.php
Edit -- https://master.php.net/note/edit/85831
Del: integrated -- https://master.php.net/note/delete/85831/integrated
Del: useless -- https://master.php.net/note/delete/85831/useless
Del: bad code -- https://master.php.net/note/delete/85831/bad+code
Del: spam -- https://master.php.net/note/delete/85831/spam
Del: non-english -- https://master.php.net/note/delete/85831/non-english
Del: in docs -- https://master.php.net/note/delete/85831/in+docs
Del: other reasons-- https://master.php.net/note/delete/85831
Reject -- https://master.php.net/note/reject/85831
Search -- https://master.php.net/manage/user-notes.php