Created
November 14, 2014 00:05
-
-
Save dilnei/4620c6ebffa661e233b3 to your computer and use it in GitHub Desktop.
Números radomicos MD5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RandomGUID extends Object { | |
private static Random random; | |
private static SecureRandom secureRandom; | |
private static String secureId; | |
/* | |
* <pre></b>Bloco estatico para cuidar do tempo de uma parte secureRandom. Este bloco | |
* sera executado apenas uma vez por instancia de JVM.</b></pre> | |
*/ | |
static { | |
secureRandom = new SecureRandom(); | |
long secureInitializer = secureRandom.nextLong(); | |
random = new Random(secureInitializer); | |
try { | |
secureId = InetAddress.getLocalHost().toString(); | |
} catch (UnknownHostException e) { | |
e.getMessage(); | |
} | |
} | |
/* | |
* <pre><b>Metodo para gerar o GUID aleatorio para converter para o formato padrao | |
* para GUID (Util para UniqueIdentifiers de bancos de dados, etc...) | |
* Exemplo: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6. Definir secure = true: | |
* verdadeiro permite que cada numero aleatorio gerado seja criptografado. | |
* Definir secure = false(Default) permite o numero aleatorio padrao seja | |
* cripatografado.</b></pre> | |
*/ | |
private static String getRandomGUID(boolean secure) throws NoSuchAlgorithmException { | |
MessageDigest md5 = null; | |
StringBuilder sbValueBeforeMD5 = new StringBuilder(); | |
md5 = MessageDigest.getInstance("MD5"); | |
long time = System.currentTimeMillis(); | |
long rand = 0; | |
if (secure) { | |
rand = secureRandom.nextLong(); | |
} else { | |
rand = random.nextLong(); | |
} | |
sbValueBeforeMD5.append(secureId); | |
sbValueBeforeMD5.append(":"); | |
sbValueBeforeMD5.append(Long.toString(time)); | |
sbValueBeforeMD5.append(":"); | |
sbValueBeforeMD5.append(Long.toString(rand)); | |
String valueBeforeMD5 = sbValueBeforeMD5.toString(); | |
md5.update(valueBeforeMD5.getBytes()); | |
byte[] array = md5.digest(); | |
StringBuilder sb = new StringBuilder(); | |
for (int j = 0; j < array.length; ++j) { | |
int b = array[j] & 0xFF; | |
if (b < 0x10) { | |
sb.append('0'); | |
} | |
sb.append(Integer.toHexString(b)); | |
} | |
return sb.toString(); | |
} | |
/** | |
* <b>Este método retorna um GUID com 32 caracteres formatados.</b> | |
* | |
* @return String | |
* @throws java.security.NoSuchAlgorithmException | |
*/ | |
public static String get() throws NoSuchAlgorithmException { | |
String randomGuid = getRandomGUID(false).toUpperCase(); | |
StringBuilder sb = new StringBuilder(); | |
sb.append(randomGuid.substring(0, 8)); | |
sb.append("-"); | |
sb.append(randomGuid.substring(8, 12)); | |
sb.append("-"); | |
sb.append(randomGuid.substring(12, 16)); | |
sb.append("-"); | |
sb.append(randomGuid.substring(16, 20)); | |
sb.append("-"); | |
sb.append(randomGuid.substring(20)); | |
return sb.toString(); | |
} | |
public static void main(String args[]) throws NoSuchAlgorithmException { | |
System.out.println(get()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment