From 79e23531463d2f7e861d7eea33641fed096f77e4 Mon Sep 17 00:00:00 2001 From: "changhyun1.lee" Date: Tue, 7 Aug 2012 17:58:21 +0900 Subject: [PATCH] [Title] Added AppIdGenerator [Type] Enhancement [Module] [Priority] [CQ#] [Redmine#] 6281 [Problem] [Cause] [Solution] [TestCase] Change-Id: I6eef87fa668f04f282eccf9ceadba67f037d5a0c --- .../src/org/tizen/common/AppIdGenerator.java | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 org.tizen.common/src/org/tizen/common/AppIdGenerator.java diff --git a/org.tizen.common/src/org/tizen/common/AppIdGenerator.java b/org.tizen.common/src/org/tizen/common/AppIdGenerator.java new file mode 100644 index 0000000..28aba98 --- /dev/null +++ b/org.tizen.common/src/org/tizen/common/AppIdGenerator.java @@ -0,0 +1,89 @@ +/* + * Common + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Changhyun Lee + * Hyeongseok Heo + * BonYong Lee + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ +package org.tizen.common; + +import java.math.BigInteger; +import java.security.SecureRandom; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +public class AppIdGenerator { + final char[] PSEUDO_CHARS = { '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' }; + + final BigInteger PSEUDO_CHAR_SIZE = new BigInteger("62"); + + private String convertBytesToAlphaNumeric(byte in[], int size) { + BigInteger randomBigInt = new BigInteger(in).abs(); + StringBuffer out = new StringBuffer(size + 1); + + for (int i = 0; i < size; ++i) { + BigInteger[] dividedAndremainder = randomBigInt + .divideAndRemainder(PSEUDO_CHAR_SIZE); + randomBigInt = dividedAndremainder[0]; + out.append(PSEUDO_CHARS[dividedAndremainder[1].intValue()]); + } + + String ret = new String(out); + return ret; + } + + private byte[] generateRandomBytes(int size) throws Exception { + SecureRandom random = new SecureRandom(); + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); + keyGen.init(128); + + byte[] bytes = new byte[size]; + random.nextBytes(bytes); + + SecretKey secretkey = keyGen.generateKey(); + byte[] key = secretkey.getEncoded(); + SecretKeySpec secretkeySpec = new SecretKeySpec(key, "AES"); + + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.ENCRYPT_MODE, secretkeySpec); + + return cipher.doFinal(bytes); + } + + public String generate(int size) throws Exception { + return convertBytesToAlphaNumeric(generateRandomBytes(size), size); + } + + public String generate() throws Exception { + return generate(10).toLowerCase(); + } + +} \ No newline at end of file -- 2.7.4