38
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで短いハッシュ

Last updated at Posted at 2013-02-09

base64_encodeで圧縮しURLセーフにしています。

short_hash.php
<?php
$shortHash = function ($data, $algo = 'CRC32') {
    return strtr(rtrim(base64_encode(pack('H*', hash($algo, $data))), '='), '+/', '-_');
};
 
$a = 'hello';

echo CRC32($a); // 907060870
echo $shortHash($a); // kHBghwA
echo md5($a) . PHP_EOL; // 5d41402abc4b2a76b9719d911017c592
echo $shortHash($a, 'md5'); // XUFAKrxLKna5cZ2REBfFkg
echo hash('sha256', $a); // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
echo $shortHash($a, 'sha256'); // LPJNul-wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ


38
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
35

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?