`

谁能提供个产生GUID的JAVA类

阅读更多

 

有没有谁能给我个产生GUID的java类呢?

谢谢大家!

分享到:
评论
1 楼 neitnaco 2007-11-21  
GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复。

import java.net.*;
import java.util.*;
import java.security.*;public class GuidCreator extends Object {

private String seedingString = “”;
private String rawGUID = “”;
private boolean bSecure = false;
private static Random myRand;
private static SecureRandom mySecureRand;

private static String s_id;

public static final int BeforeMD5 = 1;
public static final int AfterMD5 = 2;
public static final int FormatString = 3;

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 GuidCreator() { }

/*
* 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 GuidCreator(boolean secure) {
bSecure = secure;
}

/*
* Method to generate the random GUID
*/
private void getRandomGUID(boolean secure) {
MessageDigest md5 = null;
StringBuffer sbValueBeforeMD5 = new StringBuffer();

try {
md5 = MessageDigest.getInstance(”MD5″);
} catch (NoSuchAlgorithmException e) {
System.out.println(”Error: ” + e);
}

try {
long time = System.currentTimeMillis();
long rand = 0;

if (secure) {
rand = mySecureRand.nextLong();
} else {
rand = myRand.nextLong();
}

// This StringBuffer can be a long as you need; the MD5
// hash will always return 128 bits. You can change
// the seed to include anything you want here.
// You could even stream a file through the MD5 making
// the odds of guessing it at least as great as that
// of guessing the contents of the file!
sbValueBeforeMD5.append(s_id);
sbValueBeforeMD5.append(”:”);
sbValueBeforeMD5.append(Long.toString(time));
sbValueBeforeMD5.append(”:”);
sbValueBeforeMD5.append(Long.toString(rand));

seedingString = sbValueBeforeMD5.toString();
md5.update(seedingString.getBytes());

byte[] array = md5.digest();
StringBuffer sb = new StringBuffer();
for (int j = 0; j < array.length; ++j) {
int b = array[j] & 0xFF;
if (b < 0×10) sb.append(’0′);
sb.append(Integer.toHexString(b));
}

rawGUID = sb.toString();

} catch (Exception e) {
System.out.println(”Error:” + e);
}
}

public String createNewGuid(int nFormatType, boolean secure) {
getRandomGUID(secure);
String sGuid = “”;
if (BeforeMD5 == nFormatType) {
sGuid = this.seedingString;
} else if (AfterMD5 == nFormatType) {
sGuid = this.rawGUID;
} else {
sGuid = this.toString();
}
return sGuid;
}

public String createNewGuid(int nFormatType) {
return this.createNewGuid(nFormatType, this.bSecure);
}

/*
* Convert to the standard format for GUID
* (Useful for SQL Server UniqueIdentifiers, etc.)
* Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
public String toString() {
String raw = rawGUID.toUpperCase();
StringBuffer sb = new StringBuffer();
sb.append(raw.substring(0,);
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();
}
}

/**
* Use Exceple
* public static void main(String args[]) {
* for (int i=0; i< 100; i++) {
* GuidCreator myGUID = new GuidCreator();
* System.out.println(”Seeding String=” + myGUID.createNewGuid(GuidCreator.BeforeMD5));
* System.out.println(”rawGUID=” + myGUID.createNewGuid(GuidCreator.AfterMD5));
* System.out.println(”RandomGUID=” + myGUID.createNewGuid(GuidCreator.FormatString));
* }
* }
*/

相关推荐

    JAVA UUID 生成全球唯一ID

    GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复

    基于Java语言的分布式唯一ID生成系统客户端设计源码

    一个分布式唯一ID生成系统能够确保在分布式系统中各个组件产生的标识符不会发生冲突,这对于分布式数据库、服务集群以及需要高并发处理的业务场景是至关重要的。Java作为一门成熟的编程语言,其强大的跨平台特性和...

    sql主键产生器

    "SQL主键产生器"就是这样一个工具,它能帮助我们在创建或更新表时自动为主键字段生成唯一值。 SQL主键产生器通常有以下几种实现方式: 1. **序列(Sequences)**:在某些数据库系统如Oracle中,可以创建序列对象来...

    Java之无序数生成

    为解决这个问题,我们需要一种既能确保唯一性又能隐藏有序性的方法。这里,我们可以利用加密算法,将有序ID转化为看似无规律的序列。加密算法能够将有意义的数据变为看似无意义的形式,比如Caesar密码或AES加密。...

    Integration Services 数据类型

    - 数据转换组件,这是一个专门的SSIS任务,用于在数据流中将一列数据从一种类型转换为另一种类型。 - 派生列转换,创建新的列,其数据类型不同于源列。 优化数据类型选择非常重要,因为它直接影响数据处理的速度和...

    log4j-users-guide.pdf

    1. **简介**:Log4j是Apache软件基金会的一个项目,提供了一套强大的日志API,用于在Java应用程序中记录各种级别的日志信息。最新版2.15.0对安全性和性能进行了优化,以适应现代开发需求。 2. **架构**:Log4j 2的...

    17-数据清洗-清洗电商评论数据1

    而`fastjson-1.2.31`是Java的一个高性能的Json解析库,可以帮助快速解析和生成Json格式的数据。 总结,数据清洗在电商评论数据分析中起着至关重要的作用,它涉及到Json解析、异常值处理、重复数据检测等多个环节。...

    MongoDB ObjectId

    - 紧随的2个字节:进程ID(PID),区分同一服务器上不同进程产生的ObjectId。 - 最后的3个字节:随机数,进一步确保在同一进程和同一毫秒内生成的ObjectId也是唯一的。 2. **_id键**: - MongoDB中的每个文档都...

    实现类似Office助手的小精灵

    ---- Agent一词的中文意思是“代理”,故名思意,这类代理软件的主要作用是提供一种 易于理解和使用的操作界面,接受用户的指令、代替用户完成某些复杂繁琐的工作、或为用户 提供帮助。科学研究表明:从人机工程的...

Global site tag (gtag.js) - Google Analytics