Unique하면서 Random으로 키를 생성해 주는 키 생성기
UUID를 이용한 좀 짧은 (24자리) 키 생성기


package net.shiitake.logistics.common.utils;


import java.util.ArrayList;

import java.util.List;

import java.util.UUID;


/*

 * BasicIdGenerator : 키 생성용 시스템 Generator 

 * Key생성은 시스템 프레임워크에서 가장 중요한 부분이다. 

 * 해당 기능은 가능한 simple key 생성을 통해서 원하는 자리수의 키를 생성해주는 기능을 제공한다.

 * 

 *  단 중복도를 피할 경우에는 uuid를 활용하는 것이 좋다.

 */


public class BasicIdGenerator 

{

// array de 64+2 digitos 

private final static char[] DIGITS66 = {

   '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',

   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','.','_','~'

 };

 

 public static String next() 

 {

    UUID u = UUID.randomUUID();

    return toIDString(u.getMostSignificantBits()) + toIDString(u.getLeastSignificantBits());

 }

 private static String toIDString(long i) {

     char[] buf = new char[32];

     int z = 64; // 1 << 6;

     int cp = 32;

     long b = z - 1;

     do {

         buf[--cp] = DIGITS66[(int)(i & b)];

         i >>>= 6;

     } while (i != 0);

     return new String(buf, cp, (32-cp));

 }


 // 해당 키에 대하여 Duplication여부를 확인한다.

 // 동시간에 5만개 생성시 No Duplication 확인 

 public static void main(String[] args)

 {

 List<String> checkArray = new ArrayList<String>();

 

 boolean contains = false;

 for(int i=0;i<50000;i++)

 {

 String id = BasicIdGenerator.next();

 // String id = UUID.randomUUID().toString();

 // UID uid = new UID();

 // String id = uid.toString();

 contains = checkArray.contains(id);

 

 checkArray.add(id);

 System.out.println(">> " + id + " Check : " + contains);

 }

 

 if(contains)

 {

 System.out.println("Exist Duplicated Keys");

 } else

 {

 System.out.println("Safe! No Dupliucated Key");

 }

 }

}


+ Recent posts