⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 635830.xml

📁 论坛精华帖子
💻 XML
📖 第 1 页 / 共 3 页
字号:
 * 01/14/02 -- Added random function seeding with JVM run time
 *
 */
import java.net.*;
import java.util.*;
import java.security.*;


/*
 * 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 database
 * 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 data 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
 */
</Content>
<PostDateTime>2002-4-12 16:31:31</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>jacky</PostUserNickName>
<rank>五级(中级)</rank>
<ranknum>user5</ranknum>
<credit>105</credit>
<ReplyID>4228279</ReplyID>
<TopicID>635830</TopicID>
<PostUserId>30398</PostUserId>
<PostUserName>kkhui</PostUserName>
<Point>0</Point>
<Content>GUID&#32;part1&#32;--License

/*
&#32;*&#32;RandomGUID
&#32;*&#32;@version&#32;1.2&#32;01/29/02
&#32;*&#32;@author&#32;Marc&#32;A.&#32;Mnich
&#32;*
&#32;*&#32;From&#32;www.JavaExchange.com,&#32;Open&#32;Software&#32;licensing
&#32;*
&#32;*&#32;01/29/02&#32;--&#32;Bug&#32;fix:&#32;Improper&#32;seeding&#32;of&#32;nonsecure&#32;Random&#32;object
&#32;*&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;caused&#32;duplicate&#32;GUIDs&#32;to&#32;be&#32;produced.&#32;&#32;Random&#32;object
&#32;*&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;is&#32;now&#32;only&#32;created&#32;once&#32;per&#32;JVM.
&#32;*&#32;01/19/02&#32;--&#32;Modified&#32;random&#32;seeding&#32;and&#32;added&#32;new&#32;constructor
&#32;*&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;to&#32;allow&#32;secure&#32;random&#32;feature.
&#32;*&#32;01/14/02&#32;--&#32;Added&#32;random&#32;function&#32;seeding&#32;with&#32;JVM&#32;run&#32;time
&#32;*
&#32;*/
import&#32;java.net.*;
import&#32;java.util.*;
import&#32;java.security.*;


/*
&#32;*&#32;In&#32;the&#32;multitude&#32;of&#32;java&#32;GUID&#32;generators,&#32;I&#32;found&#32;none&#32;that
&#32;*&#32;guaranteed&#32;randomness.&#32;&#32;GUIDs&#32;are&#32;guaranteed&#32;to&#32;be&#32;globally&#32;unique
&#32;*&#32;by&#32;using&#32;ethernet&#32;MACs,&#32;IP&#32;addresses,&#32;time&#32;elements,&#32;and&#32;sequential
&#32;*&#32;numbers.&#32;&#32;GUIDs&#32;are&#32;not&#32;expected&#32;to&#32;be&#32;random&#32;and&#32;most&#32;often&#32;are&#32;
&#32;*&#32;easy/possible&#32;to&#32;guess&#32;given&#32;a&#32;sample&#32;from&#32;a&#32;given&#32;generator.&#32;&#32;
&#32;*&#32;SQL&#32;Server,&#32;for&#32;example&#32;generates&#32;GUID&#32;that&#32;are&#32;unique&#32;but&#32;
&#32;*&#32;sequencial&#32;within&#32;a&#32;given&#32;instance.
&#32;*
&#32;*&#32;GUIDs&#32;can&#32;be&#32;used&#32;as&#32;security&#32;devices&#32;to&#32;hide&#32;things&#32;such&#32;as&#32;
&#32;*&#32;files&#32;within&#32;a&#32;filesystem&#32;where&#32;listings&#32;are&#32;unavailable&#32;(e.g.&#32;files
&#32;*&#32;that&#32;are&#32;served&#32;up&#32;from&#32;a&#32;Web&#32;server&#32;with&#32;indexing&#32;turned&#32;off).
&#32;*&#32;This&#32;may&#32;be&#32;desireable&#32;in&#32;cases&#32;where&#32;standard&#32;authentication&#32;is&#32;not
&#32;*&#32;appropriate.&#32;In&#32;this&#32;scenario,&#32;the&#32;RandomGUIDs&#32;are&#32;used&#32;as&#32;directories.
&#32;*&#32;Another&#32;example&#32;is&#32;the&#32;use&#32;of&#32;GUIDs&#32;for&#32;primary&#32;keys&#32;in&#32;a&#32;database
&#32;*&#32;where&#32;you&#32;want&#32;to&#32;ensure&#32;that&#32;the&#32;keys&#32;are&#32;secret.&#32;&#32;Random&#32;GUIDs&#32;can
&#32;*&#32;then&#32;be&#32;used&#32;in&#32;a&#32;URL&#32;to&#32;prevent&#32;hackers&#32;(or&#32;users)&#32;from&#32;accessing
&#32;*&#32;records&#32;by&#32;guessing&#32;or&#32;simply&#32;by&#32;incrementing&#32;sequential&#32;numbers.
&#32;*
&#32;*&#32;There&#32;are&#32;many&#32;other&#32;possiblities&#32;of&#32;using&#32;GUIDs&#32;in&#32;the&#32;realm&#32;of
&#32;*&#32;security&#32;and&#32;encryption&#32;where&#32;the&#32;element&#32;of&#32;randomness&#32;is&#32;important.
&#32;*&#32;This&#32;class&#32;was&#32;written&#32;for&#32;these&#32;purposes&#32;but&#32;can&#32;also&#32;be&#32;used&#32;as&#32;a
&#32;*&#32;general&#32;purpose&#32;GUID&#32;generator&#32;as&#32;well.
&#32;*
&#32;*&#32;RandomGUID&#32;generates&#32;truly&#32;random&#32;GUIDs&#32;by&#32;using&#32;the&#32;system's&#32;
&#32;*&#32;IP&#32;address&#32;(name/IP),&#32;system&#32;time&#32;in&#32;milliseconds&#32;(as&#32;an&#32;integer),&#32;
&#32;*&#32;and&#32;a&#32;very&#32;large&#32;random&#32;number&#32;joined&#32;together&#32;in&#32;a&#32;single&#32;String&#32;
&#32;*&#32;that&#32;is&#32;passed&#32;through&#32;an&#32;MD5&#32;hash.&#32;&#32;The&#32;IP&#32;address&#32;and&#32;system&#32;time&#32;
&#32;*&#32;make&#32;the&#32;MD5&#32;seed&#32;globally&#32;unique&#32;and&#32;the&#32;random&#32;number&#32;guarantees&#32;
&#32;*&#32;that&#32;the&#32;generated&#32;GUIDs&#32;will&#32;have&#32;no&#32;discernable&#32;pattern&#32;and&#32;
&#32;*&#32;cannot&#32;be&#32;guessed&#32;given&#32;any&#32;number&#32;of&#32;previously&#32;generated&#32;GUIDs.&#32;&#32;
&#32;*&#32;It&#32;is&#32;generally&#32;not&#32;possible&#32;to&#32;access&#32;the&#32;seed&#32;information&#32;(IP,&#32;time,&#32;
&#32;*&#32;random&#32;number)&#32;from&#32;the&#32;resulting&#32;GUIDs&#32;as&#32;the&#32;MD5&#32;hash&#32;algorithm&#32;
&#32;*&#32;provides&#32;one&#32;way&#32;encryption.
&#32;*
&#32;*&#32;----&gt;&#32;Security&#32;of&#32;RandomGUID:&#32;&lt;-----
&#32;*&#32;RandomGUID&#32;can&#32;be&#32;called&#32;one&#32;of&#32;two&#32;ways&#32;--&#32;with&#32;the&#32;basic&#32;java&#32;Random
&#32;*&#32;number&#32;generator&#32;or&#32;a&#32;cryptographically&#32;strong&#32;random&#32;generator&#32;
&#32;*&#32;(SecureRandom).&#32;&#32;The&#32;choice&#32;is&#32;offered&#32;because&#32;the&#32;secure&#32;random
&#32;*&#32;generator&#32;takes&#32;about&#32;3.5&#32;times&#32;longer&#32;to&#32;generate&#32;its&#32;random&#32;numbers
&#32;*&#32;and&#32;this&#32;performance&#32;hit&#32;may&#32;not&#32;be&#32;worth&#32;the&#32;added&#32;security&#32;
&#32;*&#32;especially&#32;considering&#32;the&#32;basic&#32;generator&#32;is&#32;seeded&#32;with&#32;a&#32;
&#32;*&#32;cryptographically&#32;strong&#32;random&#32;seed.
&#32;*
&#32;*&#32;Seeding&#32;the&#32;basic&#32;generator&#32;in&#32;this&#32;way&#32;effectively&#32;decouples
&#32;*&#32;the&#32;random&#32;numbers&#32;from&#32;the&#32;time&#32;component&#32;making&#32;it&#32;virtually&#32;impossible
&#32;*&#32;to&#32;predict&#32;the&#32;random&#32;number&#32;component&#32;even&#32;if&#32;one&#32;had&#32;absolute&#32;knowledge
&#32;*&#32;of&#32;the&#32;System&#32;time.&#32;&#32;Thanks&#32;to&#32;Ashutosh&#32;Narhari&#32;for&#32;the&#32;suggestion
&#32;*&#32;of&#32;using&#32;the&#32;static&#32;method&#32;to&#32;prime&#32;the&#32;basic&#32;random&#32;generator.
&#32;*
&#32;*&#32;Using&#32;the&#32;secure&#32;random&#32;option,&#32;this&#32;class&#32;compies&#32;with&#32;the&#32;statistical
&#32;*&#32;random&#32;number&#32;generator&#32;tests&#32;specified&#32;in&#32;FIPS&#32;140-2,&#32;Security
&#32;*&#32;Requirements&#32;for&#32;Cryptographic&#32;Modules,&#32;secition&#32;4.9.1.
&#32;*
&#32;*&#32;I&#32;converted&#32;all&#32;the&#32;pieces&#32;of&#32;the&#32;seed&#32;to&#32;a&#32;String&#32;before&#32;handing
&#32;*&#32;it&#32;over&#32;to&#32;the&#32;MD5&#32;hash&#32;so&#32;that&#32;you&#32;could&#32;print&#32;it&#32;out&#32;to&#32;make
&#32;*&#32;sure&#32;it&#32;contains&#32;the&#32;data&#32;you&#32;expect&#32;to&#32;see&#32;and&#32;to&#32;give&#32;a&#32;nice
&#32;*&#32;warm&#32;fuzzy.&#32;&#32;If&#32;you&#32;need&#32;better&#32;performance,&#32;you&#32;may&#32;want&#32;to&#32;stick
&#32;*&#32;to&#32;byte[]&#32;arrays.
&#32;*
&#32;*&#32;I&#32;believe&#32;that&#32;it&#32;is&#32;important&#32;that&#32;the&#32;algorithm&#32;for&#32;
&#32;*&#32;generating&#32;random&#32;GUIDs&#32;be&#32;open&#32;for&#32;inspection&#32;and&#32;modification.
&#32;*&#32;This&#32;class&#32;is&#32;free&#32;for&#32;all&#32;uses.
&#32;*
&#32;*
&#32;*&#32;-&#32;Marc
&#32;*/</Content>
<PostDateTime>2002-4-12 16:33:11</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>jacky</PostUserNickName>
<rank>五级(中级)</rank>
<ranknum>user5</ranknum>
<credit>105</credit>
<ReplyID>4228303</ReplyID>
<TopicID>635830</TopicID>
<PostUserId>30398</PostUserId>
<PostUserName>kkhui</PostUserName>
<Point>0</Point>
<Content>//part2&#32;body

public&#32;class&#32;RandomGUID&#32;extends&#32;Object&#32;{

&#32;&#32;public&#32;String&#32;valueBeforeMD5&#32;=&#32;"";
&#32;&#32;public&#32;String&#32;valueAfterMD5&#32;=&#32;"";
&#32;&#32;private&#32;static&#32;Random&#32;myRand;
&#32;&#32;private&#32;static&#32;SecureRandom&#32;mySecureRand;

&#32;&#32;/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -