html 生成唯一码,生成唯一32位ID编码代码,以满足对ID编号的唯一性加资源性解决问题...
生成唯一32位ID编码代码,以满足对ID编号的唯一性加资源性解决问题
package com.huayu.common;
/*
* RandomGUID from http://www.javaexchange.com/aboutRandomGUID.html
* @version 1.2.1 11/05/02
* @author Marc A. Mnich
*
* From www.JavaExchange.com, Open Software licensing
*
* 11/05/02 -- Performance enhancement from Mike Dubman.
* Moved InetAddr.getLocal to static block. Mike has measured
* a 10 fold improvement in run time.
* 01/29/02 -- Bug fix: Improper seeding of nonsecure Random object
* caused duplicate GUIDs to be produced. Random object
* is now only created once per JVM.
* 01/19/02 -- Modified random seeding and added new constructor
* to allow secure random feature.
* 01/14/02 -- Added random function seeding with JVM run time
*
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
/*
* In the multitude of java GUID generators, I found none that
* guaranteed randomness. GUIDs are guaranteed to be globally unique
* by using ethernet MACs, IP addresses, time elements, and sequential
* numbers. GUIDs are not expected to be random and most often are
* easy/possible to guess given a sample from a given generator.
* SQL Server, for example generates GUID that are unique but
* sequencial within a given instance.
*
* GUIDs can be used as security devices to hide things such as
* files within a filesystem where listings are unavailable (e.g. files
* that are served up from a Web server with indexing turned off).
* This may be desireable in cases where standard authentication is not
* appropriate. In this scenario, the RandomGUIDs are used as directories.
* Another example is the use of GUIDs for primary keys in a #base
* where you want to ensure that the keys are secret. Random GUIDs can
* then be used in a URL to prevent hackers (or users) from accessing
* records by guessing or simply by incrementing sequential numbers.
*
* There are many other possiblities of using GUIDs in the realm of
* security and encryption where the element of randomness is important.
* This class was written for these purposes but can also be used as a
* general purpose GUID generator as well.
*
* RandomGUID generates truly random GUIDs by using the system's
* IP address (name/IP), system time in milliseconds (as an integer),
* and a very large random number joined together in a single String
* that is passed through an MD5 hash. The IP address and system time
* make the MD5 seed globally unique and the random number guarantees
* that the generated GUIDs will have no discernable pattern and
* cannot be guessed given any number of previously generated GUIDs.
* It is generally not possible to access the seed information (IP, time,
* random number) from the resulting GUIDs as the MD5 hash algorithm
* provides one way encryption.
*
* ----> Security of RandomGUID:
* RandomGUID can be called one of two ways -- with the basic java Random
* number generator or a cryptographically strong random generator
* (SecureRandom). The choice is offered because the secure random
* generator takes about 3.5 times longer to generate its random numbers
* and this performance hit may not be worth the added security
* especially considering the basic generator is seeded with a
* cryptographically strong random seed.
*
* Seeding the basic generator in this way effectively decouples
* the random numbers from the time component making it virtually impossible
* to predict the random number component even if one had absolute knowledge
* of the System time. Thanks to Ashutosh Narhari for the suggestion
* of using the static method to prime the basic random generator.
*
* Using the secure random option, this class compies with the statistical
* random number generator tests specified in FIPS 140-2, Security
* Requirements for Cryptographic Modules, secition 4.9.1.
*
* I converted all the pieces of the seed to a String before handing
* it over to the MD5 hash so that you could print it out to make
* sure it contains the # you expect to see and to give a nice
* warm fuzzy. If you need better performance, you may want to stick
* to byte[] arrays.
*
* I believe that it is important that the algorithm for
* generating random GUIDs be open for inspection and modification.
* This class is free for all uses.
*
*
* - Marc
*/
public class RandomGUID extends Object {
public String valueBeforeMD5 = "";
public String valueAfterMD5 = "";
private static Random myRand;
private static SecureRandom mySecureRand;
private static String s_id;
/*
* 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 RandomGUID() {
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 RandomGUID(boolean secure) {
getRandomGUID(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));
valueBeforeMD5 = sbValueBeforeMD5.toString();
md5.update(valueBeforeMD5.getBytes());
byte[] array = md5.digest();
StringBuffer sb = new StringBuffer();
for (int j = 0; j < array.length; ++j) {
int b = array[j] & 0xFF;
if (b < 0x10) sb.append('0');
sb.append(Integer.toHexString(b));
}
valueAfterMD5 = sb.toString();
} catch (Exception e) {
System.out.println("Error:" + e);
}
}
/**
* 生成一个GUID串
* @return GUID
*/
public static String newGuid(){
RandomGUID rdmGUID = new RandomGUID();
return rdmGUID.toString();
}
/*
* 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();
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< 10000; i++) { //这是生成10000个此32位编码,唯一性的哦 //RandomGUID myGUID = new RandomGUID(); //System.out.println("Seeding String=" + myGUID.valueBeforeMD5); //System.out.println("rawGUID=" + myGUID.valueAfterMD5); //System.out.println("RandomGUID=" + RandomGUID.toString()); System.out.println(RandomGUID.newGuid()); } }}
html 生成唯一码,生成唯一32位ID编码代码,以满足对ID编号的唯一性加资源性解决问题...相关推荐
- 假设我们在对有符号值使用补码运算的32位机器人运行代码。对于有符号值使用的是算术右移,而对于无符号值使用的是逻辑右移
假设我们在对有符号值使用补码运算的32位机器人运行代码.对于有符号值使用的是算术右移,而对于无符号值使用的是逻辑右移.变量的声明和初始化如下: int x = foo(); //任意值 int y = ...
- linux 生成2g文件吗,linux 32位系统 c++写大于2G文件
问题:在centos5.5 32位系统上,开发的c++程序,用vfprintf 输出日志文件,发现当日志大于2G时会报错"File size limit exceeded".开始以 ...
- 【源码】声明32位和64位Access、Excel等VBA兼容的API函数的方法
1.在声明中加上 PtrSafe 关键字 2.加上VBA7 及Win64的判断 Declare 语句 PtrSafe 关键字(可参考VBA帮助) 带有 PtrSafe 关键字的 Declare 语句 ...
- Windows X86(32位系统)为什么可使用最大内存4G,再加内存条,内存也不会增加?
①X86就是指32位系统(位:cpu一次能处理的最大位数....所以64位比32位速度快很多) ②内存条:CPU可通过总线地址,并进行读写操作的电脑部件. 电脑内存(RAM,random sccess ...
- java唯一码_唯一邀请码生成(Java版本)
前言 之前收到一个需求,甲方说,他们想给用户生成一个唯一的邀请码,然后用户量在xxx之类的,例如我这里就随便说个5kw个吧.这个嘛,听起来都觉得挺简单的,毕竟每个用户基本上都有自己的唯一用户id,用那 ...
- html 生成唯一码,生成唯一邀请码.html
Document let sourceString = 'ZDOWGVJ5ASB3IRP9QM41EYFCU2TN76XH0KL';//三十五进制字符串长度不足8,用数字8高位补全 function ...
- C#生成的exe无法在32位的XP系统运行
如图所示错误 解决办法: 1.XP系统最高运行框架.net framework 4.0,要运行得把目标框架降至XP系统最高框架以内: 2.VS软件设置项目目标框架: 3.设置生成事件目标平台为X86: ...
- php md5 32 大写,编写生成32位大写和小写字符的md5的函数
package nicetime.com.practise; import java.security.MessageDigest; /** * MD5加密是JAVA应用中常见的算法,请写出两个MD5 ...
- 高性能(无需判重)批量生成优惠券码方案
UUID方案:将uuid分成等份,转成16进制即可.(代码里有11位和8位数的券码代码参考) 雪花id方案:实现思路很简单,生成雪花id(可根据需求,换成使用uuid的方案,测试代码里有两种方法),将 ...
最新文章
- CentOS7.3下二进制安装Kubernetes1.9集群 开启TLS
- linux下的access()函数判断文件是否存在、打印时间
- python找不到模块文件夹_python – __init__.py在同一目录中找不到模块
- 百位云计算专家齐聚湖畔大学,阿里云MVP全球闭门会聚焦数字化转型
- 如何使用文件的fseek函数对文件指针进行操作
- 学生学籍管理系统页面源代码html_学生管理系统(界面+源代码)
- Jlink按照用zadig升级用于openocd后,还原
- 2022iOS面试题集锦(iOS interview)
- 敢问路在何方?路在脚下!
- 速来了解头条号的推荐机制,让你的自媒体内容收下更多数据量!
- win7查找计算机图片,如何在 win7电脑上查看 HEIC 照片的内容?
- Xpath简介及用法整理
- [LUOGU] P3354 [IOI2005]Riv 河流
- 对n个数进行排序(空间复杂度O(1))
- 解决win10安装之后本地搜索框不能用
- 均匀分布 卡方分布_【Math】概率论常用分布大全
- 《Java 技术体系》之一:Java 技术体系概览
- carbonData使用文档
- PSIM仿真入门之一
- dev sda2 linux lvm,记录linux LVM 扩容硬盘空间的记录
热门文章
- SU命令的功能及基本用法--psmerge
- Linux网络:Virtual Routing and Forwarding (VRF)
- Understanding glibc malloc - ptmalloc
- Cilium提供并透明地保护应用程序工作负载之间的网络连接和负载平衡:什么是eBPF和XDP?
- python画平面直角坐标系_Python 数据可视化:重新认识坐标系
- ue4cmd怎么调用_虚幻引擎UE4-命令行使用的一些详细技巧
- java ssl 加密传输_java线程之四 SSL加密传输
- spring 处理带有特殊字符的请求_程序员笔记|常见的Spring异常分析及处理
- [专栏精选]Unity中动态构建NavMesh
- 对于局部变量_浅谈Shell函数中全局变量和局部变量