ランダム文字ぽいものをつくる

1,442 views

Published on

PHPでランダム文字列ぽいものを作る方法

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,442
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ランダム文字ぽいものをつくる

  1. 1. koyama@hoge.org 1
  2. 2. about me id:koyhoge 2007 OSS 2
  3. 3. agenda id generate encode/decode 3
  4. 4. RFC4648 The Base16, Base32, and Base64 Data Encodings Base 64 Encoding with URL and Filename Safe Alphabet base64url + → -, / → _ 4
  5. 5. id generate DB Sequence id 1 → NWoZK3kTsExUV00Ywo1G5jlU 2 → 2kuSN7rMzfGcB2DKt67EqDWQ 5
  6. 6. static public function genUniqHash($input, $options = null) { $seed = array_val($options, 'seed', 0); $length = array_val($options, 'length', 18); / 18 = non-padding max / static $search = array('+', '/'); static $replace = array('-', '_'); / limit check: sha1() returns 20 bytes / if (($length <= 0) || (20 < $length)) { $length = 20; } if (($seed + $length) > 20) { $seed = 20 - $length; } $out = base64_encode(substr(sha1($input, true), $seed, $length)); return str_replace($search, $replace, $out); } 6
  7. 7. encode/decode gzip +base64url 7
  8. 8. static public function tinyEncode($input) { static $search = array('+', '/'); static $replace = array('-', '_'); $gzed = gzdeflate($input); $encoded = base64_encode($gzed); $result = str_replace($search, $replace, $encoded); / trim trailing '=' / return rtrim($result, '='); } 8
  9. 9. static public function tinyDecode($encoded) { static $search = array('-', '_'); static $replace = array('+', '/'); $str = str_replace($search, $replace, $encoded); $decoded = base64_decode($str); return gzinflate($decoded); } 9
  10. 10. Thanks for hearing 10

×