This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Security and the fly likes Generate Unique Keys/identifier/license Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Generate Unique Keys/identifier/license" Watch "Generate Unique Keys/identifier/license" New topic
Author

Generate Unique Keys/identifier/license

Aparna Sree
Greenhorn

Joined: Jul 21, 2010
Posts: 28
Hi,

I want to generate unique keys. It should be 16-byte (128-bit) number/char.

UUID generates this. But it generates 32 characters(eg:0003a72d-fad6-4bd2-9281-40cab393b733).

I need only 16 char. Is there an api which supports this? I do not want to slice off the UUID generated key and I want to retain the size to 128 bits.
greg stark
Ranch Hand

Joined: Aug 10, 2006
Posts: 220
A byte and a character are not the same thing. First you said you wanted a 16 byte, 128-bit key. That would equate to 32 printable hex characters, the same as the UUID you said you didn't want. So what do you really want?


Nice to meet you.
Aparna Sree
Greenhorn

Joined: Jul 21, 2010
Posts: 28
I guess I messed up little bit.
I have 2 Queries here

1. UUID creates 32 printable hex characters. I need only 16 printable characters. Something like this: xxxx-xxxx-xxxx-xxxx. Is there any api that can directly generate unique 16 printable characters. If I manipulate UUID (retrieve 16 printable hex char out of it) I am not sure if it will retain its uniqueness.

2. I think 32 printable hex char means base-16 right? Is there a java api which provides it in base 64 format?

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Aparna Sree wrote:I guess I messed up little bit.
I have 2 Queries here

1. UUID creates 32 printable hex characters. I need only 16 printable characters. Something like this: xxxx-xxxx-xxxx-xxxx. Is there any api that can directly generate unique 16 printable characters. If I manipulate UUID (retrieve 16 printable hex char out of it) I am not sure if it will retain its uniqueness.


Since the domain of values is reduced going from 32 down to 16 characters it is unlikely that you can ensure that it is now unique.

I bet that you don't really want it unique. I bet that you only want it unique in the context of your licence so unless you are going to sell billions and billions and billions of copies then it should not matter.

One possible approach is to SHA1 digest the UUID, Base64 encode the result (see http://commons.apache.org/codec/) and then select just the first 16 characters of the result. This may be overkill but I'm willing to bet a lot of money that you never ever ever get a real life clash of license key.


2. I think 32 printable hex char means base-16 right? Is there a java api which provides it in base 64 format?


See http://commons.apache.org/codec/ .


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Aparna Sree
Greenhorn

Joined: Jul 21, 2010
Posts: 28
That helped. Thanks !!!
To check uniqueness (in the context of my licence, of course) am putting it in a Hashset. Tried one hundred thousand data.
So far so good

I did a SHA-256 digest and a Base64 encode of randomly generated long and timestamp.

Works fine so far. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate Unique Keys/identifier/license
 
Similar Threads
Help needed to generate token
How to Create a Unique 16 Character String
crc32 and VMID for unique names
hashCode for temporarly id ?
Unique number generation in JdK1.4