Generating 8-Character Only Uuids

Generating 8-character only UUIDs

It is not possible since a UUID is a 16-byte number per definition. But of course, you can generate 8-character long unique strings (see the other answers).

Also be careful with generating longer UUIDs and substring-ing them, since some parts of the ID may contain fixed bytes (e.g. this is the case with MAC, DCE and MD5 UUIDs).

8 character length UUID

A UUID is 128 bits, commonly displayed as a 36-byte hexadecimal string. If you throw away some (in your case, most) of the bits, it is no longer a UUID.

If you just want 8 random bytes, use your language's native function to generate that.

safe enough 8-character short unique random string

Is there a reason you can't use tempfile to generate the names?

Functions like mkstemp and NamedTemporaryFile are absolutely guaranteed to give you unique names; nothing based on random bytes is going to give you that.

If for some reason you don't actually want the file created yet (e.g., you're generating filenames to be used on some remote server or something), you can't be perfectly safe, but mktemp is still safer than random names.

Or just keep a 48-bit counter stored in some "global enough" location, so you guarantee going through the full cycle of names before a collision, and you also guarantee knowing when a collision is going to happen.

They're all safer, and simpler, and much more efficient than reading urandom and doing an md5.

If you really do want to generate random names, ''.join(random.choice(my_charset) for _ in range(8)) is also going to be simpler than what you're doing, and more efficient. Even urlsafe_b64encode(os.urandom(6)) is just as random as the MD5 hash, and simpler and more efficient.

The only benefit of the cryptographic randomness and/or cryptographic hash function is in avoiding predictability. If that's not an issue for you, why pay for it? And if you do need to avoid predictability, you almost certainly need to avoid races and other much simpler attacks, so avoiding mkstemp or NamedTemporaryFile is a very bad idea.

Not to mention that, as Root points out in a comment, if you need security, MD5 doesn't actually provide it.

how to reduce length of UUID generated using randomUUID( )

If you don't need it to be unique, you can use any length you like.

For example, you can do this.

Random rand = new Random();
char[] chars = new char[16];
for(int i=0;i<chars.length;i++) {
chars[i] = (char) rand.nextInt(65536);
if (!Character.isValidCodePoint(chars[i]))
i--;
}
String s = new String(chars);

This will give you almost the same degree of randomness but will use every possible character between \u0000 and \ufffd

If you need printable ASCII characters you can make it as short as you like but the likelihood of uniqueness drops significantly. What can do is use base 36 instead of base 16

UUID uuid = UUID.randomUUID();
String s = Long.toString(uuid.getMostSignificantBits(), 36) + '-' + Long.toString(uuid.getLeastSignificantBits(), 36);

This will 26 characters on average, at most 27 character.

You can use base64 encoding and reduce it to 22 characters.

If you use base94 you can get it does to 20 characters.

If you use the whole range of valid chars fro \u0000 to \ufffd you can reduce it to just 9 characters or 17 bytes.

If you don't care about Strings you can use 16, 8-bit bytes.

how to generate unique numbers less than 8 characters long

Does it need to be unique or random? If it only needs to be unique, you could load the highest value from the datastore when the application is launched (assuming only one app is writing to the data file). One you have your highest value:

public class IdGenerator{
private String value;

public IdGenerator( String initial value ){
this.value = value;
}

public synchronized String nextValue(){
value = incrementValue( value );
return value;
}

private static String nextValue( String currentValue ){
// Somehow increment the value.
return incrementedValue;
}

}

Depending on what characters you allow in you uuid this can be done various ways. I would read the last character of the current String, check if it's the last allowed character. Is so, increment the 7th character and so on. Else, increment the last character.



Related Topics



Leave a reply



Submit