id-codec


Abstract

Database Entity의 ID를 서비스에 그대로 노출해서는 안되는 경우도 있다. 그래서 Short, Long and Pretty Url 를 참고하여 만들어봤다.

Disclaimer

  • 이 유틸리티는 암호화 및 복호화를 위한 용도로 사용할 수 없다.
  • 이 유틸리티에 의해 생성되는 값은 재사용을 목적으로 저장해서는 안된다.

Java Apidocs

Apache Maven

Check the latest version at Maven Central Repository.

<dependency>
  <groupId>com.googlecode.jinahya</groupId>
  <artifactId>id-codec</artifactId>
  <version>1.0-alpha-2</version>
</dependency>

Usage

import com.googlecode.jinahya.util.IdCodec;

// encode/decode normal long @Id.
{
    final long original = 0L;
    final String encoded = IdCodec.encodeId(original);
    final long decoded = IdCodec.decodeId(encoded);
    assert decoded == original;
}

// encode/decode UUIDs
// using UUID as @Id is not a good idea.
{
    final UUID original = UUID.randomUUID();
    final String encoded = IdCodec.encodeUUID(original);
    final UUID decoded = IdCodec.decodeUUID(encoded);
    assert decoded.equals(original);
    assert decoded.getMostSignificantBits() == original.getMostSignificantBits();
    assert decoded.getLeastSignificantBits() == original.getLeastSignificantBits();
}

Test

동일한 ID(long)값에 대하여 매번 다른 값이 생성된다. 물론 decoding은 같은 결과를 반환한다.


                  id             encoded1             encoded2
-------------------- -------------------- --------------------
                  -5      l3k5w7g-nusepu4     bwsohfw-11mxmtzg
                  -5      l3k5w7g-j9eo0gc     182gvf4c-eo0xb2k
                  -4      bwsohfw-swpiw5o      7bexs24-f4kas0c
                  -4      yvpe0cs-obbs6rw      poxwll8-jpy1he4
                  -3     182gvf4c-orv5npo      l3k5w7g-xymn2h8
                  -3      l3k5w7g-orv5npo      gi6f6to-k6heybw
                  -2      7bexs24-tts9u18      l3k5w7g-kn0sf9o
                  -2     uabnaz0-130jr8ss     bwsohfw-17lxhy6k
                  -1      yvpe0cs-yvpe0cs     gi6f6to-13h34pqk
                  -1     182gvf4c-l3k5w7g      uabnaz0-l3k5w7g
                   0                 1e-a                28-1e
                   0                  k-u                 1o-k
                   1                2i-1p                 u-29
                   1                2i-1f                  k-l
                   2                 u-20                14-16
                   2                 1o-w                2i-2k
                   3                  u-n                 u-2l
                   3                14-2b                2i-1r
                   4                 14-e                14-1s
                   4                  k-o                 1o-e
-9223372036854775808            8hcwbuc-a           8hcwbuc-1y
-9223372036854775808          14n139is-1y           quvv5dg-14
 9223372036854775807      qechofo-l3k5w7g     146hpsl0-uabnaz0
 9223372036854775807      cm79kac-yvpe0cs      zl3z378-7bexs24
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s