LoginSignup
80

More than 5 years have passed since last update.

Hashidsから、短いユニークID(UUID)を作る

Last updated at Posted at 2016-03-02

短くて一意な複数のID(short unique ids)を簡単に作りたい

ユニークIDの需要はそれなりに高く、かつ出来る限りショートである方が望ましいという需要はそれなりにあります。

方法としては、UUIDを使ってるのではないでしょうか?
もしくは、MD5やSHA256等のハッシュ値をBASE64エンコードして我慢して使用しているのではないでしょうか?
文字列長が超長いけど、ユニークなので、仕方なく、我慢しながら使ってるのではないでしょうか。

ここ最近、ショートでユニークなIDをシンプルに作れるという需要を満たす、Hashids が流行りつつあります。
詳しくは → http://hashids.org/

対応してる言語は、有名どころだとC#以外の殆どの言語に対応しています。
JavaScript, Ruby, Python, Java, Scala, PHP, Perl, Swift, Clojure, Objective-C, C, C++11, D, F#, Go, Erlang, Lua, Haskell, Elixir, Rust, ColdFusion, Groovy, Kotlin, Nim, VBA, ActionScript, CoffeeScript, Bash, R, TSQL and for Node.js & .NET
(但し、Objective-Cだけは、v1.0未対応のようです。 2016/03/02 現在)

使い方が超簡単どころか、codepenのデモまで準備されてるので、もう誰でも体験出来ます。素晴らしすぎます。
http://codepen.io/ivanakimov/pen/bNmExm

注意点としては、

  • 数字からしか生成できないので、文字列の場合はまず数字化しましょう
  • 当然ですが、実運用で何も考えずに使うと値の予測が可能なので、saltキーはちゃんと考えて使いましょう。
  • パスワードには使っちゃダメです。

サンプル

公式ページ http://hashids.org/ のサンプルを引用させて頂きました。

JavaScript
var hashids = new Hashids("this is my salt"),
id = hashids.encode(1, 2, 3),
numbers = hashids.decode(id);
PHP
$hashids = new Hashids\Hashids('this is my salt');
$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);
Python
hashids = Hashids(salt="this is my salt")
id = hashids.encode(1, 2, 3)
numbers = hashids.decode(id)
Ruby
hashids = Hashids.new "this is my salt"
id = hashids.encode(1, 2, 3)
numbers = hashids.decode(id)
Scala
val hashids = Hashids("this is my salt")
val id = hashids.encode(1L, 2L, 3L)
val numbers = hashids.decode(id)
Java
Hashids hashids = new Hashids("this is my salt");
String id = hashids.encode(1, 2, 3);
long[] numbers = hashids.decode(id);
ActionScript
var hashids:Hashids = new Hashids("this is my salt");
var id:String = hashids.encode(1, 2, 3);
var numbers:Vector.<Number> = hashids.decode(id);
Perl
my $hashids = Hashids->new('this is my salt');
my $id = $hashids->encode(1, 2, 3);
my $numbers = $hashids->decode($id);
C++
hashidsxx::Hashids hash("this is my salt");
std::string id = hash.encode({1, 2, 3});
std::vector<uint32_t> numbers = hash.decode(id);
Swift
var hashids = Hashids(salt:"this is my salt");
var hash = hashids.encode(1, 2, 3);
var values = hashids.decode(s!);
Erlang
Ctx = hashids:new([{salt, "this is my salt"}]).
Id = hashids:encode(Ctx, [1, 2, 3]).
Numbers = hashids:decode(Ctx, Id).
Haskell
let hashids = hashidsSimple "this is my salt"
    _id = encodeList hashids [1, 2, 3]
    numbers = decode hashids _id
Go
hd := hashids.NewData()
hd.Salt = "this is my salt"
h := hashids.NewWithData(hd)
id, _ := h.Encode([]int{1, 2, 3})
numbers, _ := h.DecodeWithError(id)
CoffeeScript
hashids = new Hashids "this is my salt"
id = hashids.encode 1, 2, 3
numbers = hashids.decode id
Groovy
Hashids hashids = new Hashids("this is my salt")
String id = hashids.encode(1, 2, 3)
List<Long> numbers = hashids.decode(id)
Elixer
hashids = Hashids.new(salt: "this is my salt")
id = Hashids.encode(hashids, [1, 2, 3])
numbers = Hashids.decode!(hashids, id)
Clojure
(require '[hashids.core :as h])
(def hashids-opts {:salt "this is my salt"})
(h/encode hashids-opts 1, 2, 3)
(h/decode hashids-opts "laHquq")
F#
let config = HashidConfiguration.withSalt "this is my salt"
let id = Hashid.encode config [| 1; 2; 3 |]
let numbers = Hashid.decode config id
Lua
local h = hashids.new("this is my salt");
local id = h:encode(1, 2, 3);
local numbers = h:decode(id);
Objective-C
Hashids *hashids = [Hashids hashidWithSalt:@"this is my salt"];
NSString *id = [hashids encrypt:@1, @2, @3, nil];
NSLog(@"%@", id);
NSLog(@"%@", [hashids decrypt:id]);
R
h = hashid_settings(salt = 'this is my salt')
encode(c(1, 2, 3), h) #"laHquq"
decode("laHquq", h) #c(1, 2, 3)
bash
bashids -e -s MySalt 1 2 3
bashids -d -s MySalt laHquq

bashにまで対応してるとかキモいです(褒め言葉)

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
80