]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
smallHash: simplified and improved performance
authorAlexandre Alapetite <alexandre@alapetite.fr>
Sun, 10 Nov 2013 21:50:34 +0000 (22:50 +0100)
committerAlexandre Alapetite <alexandre@alapetite.fr>
Sun, 10 Nov 2013 21:50:34 +0000 (22:50 +0100)
Unchanged behaviour

index.php

index 0465a4e5bbca798b5dc1b0ca0cd26a0801450b14..24fbe43611f4fc266d1799d0fa23b8255f342425 100644 (file)
--- a/index.php
+++ b/index.php
@@ -221,7 +221,7 @@ function nl2br_escaped($html)
     return str_replace('>','&gt;',str_replace('<','&lt;',nl2br($html)));
 }
 
-/* Returns the small hash of a string
+/* Returns the small hash of a string, using RFC 4648 base64url format
    eg. smallHash('20111006_131924') --> yZH23w
    Small hashes:
      - are unique (well, as unique as crc32, at last)
@@ -233,10 +233,7 @@ function nl2br_escaped($html)
 function smallHash($text)
 {
     $t = rtrim(base64_encode(hash('crc32',$text,true)),'=');
-    $t = str_replace('+','-',$t); // Get rid of characters which need encoding in URLs.
-    $t = str_replace('/','_',$t);
-    $t = str_replace('=','@',$t);
-    return $t;
+    return strtr($t, '+/', '-_');
 }
 
 // In a string, converts urls to clickable links.