/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.lcudp.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import org.apache.log4j.Logger;
/**
*
* @author zhoupk
*/
public class RandomGUIDUtil {
static Logger logger = Logger.getLogger(RandomGUIDUtil.class.getName());
public String valueBeforeMD5 = "";
public String valueAfterMD5 = "";
private static Random myRand;
private static SecureRandom mySecureRand;
private static String s_id;
private static final int PAD_BELOW = 0x10;
private static final int TWO_BYTES = 0xFF;
/*
* Static block to take care of one time secureRandom seed. It takes a few
* seconds to initialize SecureRandom. You might want to consider removing
* this static block or replacing it with a "time since first loaded" seed
* to reduce this time. This block will run only once per JVM instance.
*/
static {
mySecureRand = new SecureRandom();
long secureInitializer = mySecureRand.nextLong();
myRand = new Random(secureInitializer);
try {
s_id = InetAddress.getLocalHost().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/*
* Default constructor. With no specification of security option, this
* constructor defaults to lower security, high performance.
*/
public RandomGUIDUtil() {
getRandomGUID(false);
}
/*
* Constructor with security option. Setting secure true enables each random
* number generated to be cryptographically strong. Secure false defaults to
* the standard Random function seeded with a single cryptographically
* strong random number.
*/
public RandomGUIDUtil(boolean secure) {
getRandomGUID(secure);
}
/*
* Method to generate the random GUID
*/
private void getRandomGUID(boolean secure) {
MessageDigest md5 = null;
StringBuffer sbValueBeforeMD5 = new StringBuffer(128);
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
logger.error("Error: " + e);
}
try {
long time = System.currentTimeMillis();
long rand = 0;
if (secure) {
rand = mySecureRand.nextLong();
} else {
rand = myRand.nextLong();
}
sbValueBeforeMD5.append(s_id);
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(time));
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(rand));
valueBeforeMD5 = sbValueBeforeMD5.toString();
md5.update(valueBeforeMD5.getBytes());
byte[] array = md5.digest();
StringBuffer sb = new StringBuffer(32);
for (int j = 0; j < array.length; ++j) {
int b = array[j] & TWO_BYTES;
if (b < PAD_BELOW) {
sb.append('0');
}
sb.append(Integer.toHexString(b));
}
valueAfterMD5 = sb.toString();
} catch (Exception e) {
logger.error("Error:" + e);
}
}
/*
* Convert to the standard format for GUID (Useful for SQL Server
* UniqueIdentifiers, etc.) Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
public String toString() {
String raw = valueAfterMD5.toUpperCase();
StringBuffer sb = new StringBuffer(64);
sb.append(raw.substring(0, 8));
sb.append("-");
sb.append(raw.substring(8, 12));
sb.append("-");
sb.append(raw.substring(12, 16));
sb.append("-");
sb.append(raw.substring(16, 20));
sb.append("-");
sb.append(raw.substring(20));
return sb.toString();
}
// Demonstraton and self test of class
public static void main(String args[]) {
for (int i = 0; i < 100; i++) {
RandomGUIDUtil myGUID = new RandomGUIDUtil();
System.out.println("Seeding String=" + myGUID.valueBeforeMD5);
System.out.println("rawGUID=" + myGUID.valueAfterMD5);
System.out.println("RandomGUID=" + myGUID.toString());
}
}
}
分享到:
相关推荐
Java生成UUID使用的第三方包,生成UUID的第三方包,
本文档主要讲述的是java生成UUID通用唯一识别码;UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OSF) 的组织在分布式计算...
java生成UUID通用唯一识别码 中文WORD版,感兴趣的小伙伴们可以下载来用下,代码可以直接使用
java生成UUID通用唯一识别码[参照].pdf
版本 3:UUID 是通过散列(MD5 作为散列算法)名字空间(namespace)标识符和名称生成的; 版本 4 - UUID 使用随机性或伪随机性生成; 版本 5 类似于版本 3(SHA1 作为散列算法)。 为了能兼容过去的 UUID,以及应对...
UUID生成工具类
java 生成8位UUID,解决UUID2太长的问题,欢迎下载。后续代码,陆续放出
java生成唯一索引,Long型,区别uuid
jdk与javauuidgenerator生成uuid
<groupId>com.fasterxml.uuid</groupId> <artifactId>java-uuid-generator <packaging>bundle <name>Java UUID Generator <version>3.1.5</version>
NULL 博文链接:https://jarorwar.iteye.com/blog/553875
java生成16位随机数
uuid生成,可生成16个字符的唯一码。使用方法,见main函数
java UUID16的生成下载 UUID ,解压后生成的.java,可以在编译器中运行main方法
GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复
主要介绍了java实现随机生成UUID的函数代码,有需要的小伙伴可以参考下。
JAVA UUID 生成.txt
通过hibernate的组件生成uuid的代码
JUG是一组用于处理UUID的Java类:使用任何标准方法生成UUID,有效输出,排序等。 它根据生成UUID(有关更多说明,另请参见) JUG由Tatu Saloranta( )最初于2002年编写,并且经过多年的更新。 此外,其他许多人也...