-
-
Save aloha1003/2c754643c4abea709d3f94ecc991c32f to your computer and use it in GitHub Desktop.
PHP uniqid in JAVA
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 Uniqid { | |
/*** | |
* Copy of uniqid in php http://php.net/manual/fr/function.uniqid.php | |
* @param prefix | |
* @param more_entropy | |
* @return | |
*/ | |
public String uniqid(String prefix,boolean more_entropy) | |
{ | |
long time = System.currentTimeMillis(); | |
//String uniqid = String.format("%fd%05f", Math.floor(time),(time-Math.floor(time))*1000000); | |
//uniqid = uniqid.substring(0, 13); | |
String uniqid = ""; | |
if(!more_entropy) | |
{ | |
uniqid = String.format("%s%08x%05x", prefix, time/1000, time); | |
}else | |
{ | |
SecureRandom sec = new SecureRandom(); | |
byte[] sbuf = sec.generateSeed(8); | |
ByteBuffer bb = ByteBuffer.wrap(sbuf); | |
uniqid = String.format("%s%08x%05x", prefix, time/1000, time); | |
uniqid += "." + String.format("%.8s", ""+bb.getLong()*-1); | |
} | |
return uniqid ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment