/*
* CapturePlugin by Andreas Hessel (Vidrec@gmx.de), Bodo Tasche
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* CVS information:
* $RCSfile$
* $Source$
* $Date: 2006-10-28 18:27:24 +0200 (Sa, 28 Okt 2006) $
* $Author: ds10 $
* $Revision: 2800 $
*/
package captureplugin.drivers.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
/**
* This Helper-Class creates an unique ID
*
* See http://www.javapractices.com/Topic56.cjp
*/
public class IDGenerator {
/**
* Generate unique ID
* @return unique ID
*/
public static String generateUniqueId() {
SecureRandom prng = null;
try {
prng = SecureRandom.getInstance("SHA1PRNG");
String randomNum = Integer.toString(prng.nextInt());
MessageDigest sha = MessageDigest.getInstance("SHA-1");
byte[] result = sha.digest(randomNum.getBytes());
return hexEncode(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* The byte[] returned by MessageDigest does not have a nice
* textual representation, so some form of encoding is usually performed.
* <p/>
* This implementation follows the example of David Flanagan's book
* "Java In A Nutshell", and converts a byte array into a String
* of hex characters.
* <p/>
* Another popular alternative is to use a "Base64" encoding.
*
* @param aInput Byte-Array
* @return String-Representation of the Byte-Array
*/
static private String hexEncode(byte[] aInput) {
StringBuilder result = new StringBuilder();
char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
for (int idx = 0; idx < aInput.length; ++idx) {
byte b = aInput[idx];
result.append(digits[(b & 0xf0) >> 4]);
result.append(digits[b & 0x0f]);
}
return result.toString();
}
}