14.12.2020

Java Code To Generate Unique Key

Java Code To Generate Unique Key Rating: 8,0/10 6950 reviews
  • Learn Java Secure Hashing algorithms in-depth. A secure password hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user-provided password, which are generally very weak and easy to guess. There are many such hashing algorithms in Java which can prove really effective for password security.
  • Unique Key in SQL. A unique key is a set of one or more than one fields/columns of a table that uniquely identify a record in a database table. You can say that it is little like primary key but it can accept only one null value and it cannot have duplicate values.
This class provides the functionality of a secret (symmetric) key generator.

Key generators are constructed using one of the getInstance class methods of this class.

KeyGenerator objects are reusable, i.e., after a key has been generated, the same KeyGenerator object can be re-used to generate further keys.

There are two ways to generate a key: in an algorithm-independent manner, and in an algorithm-specific manner. The only difference between the two is the initialization of the object:

  • Algorithm-Independent Initialization

    All key generators share the concepts of a keysize and a source of randomness. There is an init method in this KeyGenerator class that takes these two universally shared types of arguments. There is also one that takes just a keysize argument, and uses the SecureRandom implementation of the highest-priority installed provider as the source of randomness (or a system-provided source of randomness if none of the installed providers supply a SecureRandom implementation), and one that takes just a source of randomness.

    Since no other parameters are specified when you call the above algorithm-independent init methods, it is up to the provider what to do about the algorithm-specific parameters (if any) to be associated with each of the keys.

  • Algorithm-Specific Initialization

    For situations where a set of algorithm-specific parameters already exists, there are two init methods that have an AlgorithmParameterSpec argument. One also has a SecureRandom argument, while the other uses the SecureRandom implementation of the highest-priority installed provider as the source of randomness (or a system-provided source of randomness if none of the installed providers supply a SecureRandom implementation).

In case the client does not explicitly initialize the KeyGenerator (via a call to an init method), each provider must supply (and document) a default initialization.

Every implementation of the Java platform is required to support the following standard KeyGenerator algorithms with the keysizes in parentheses:

Starting with Java 5, the UUID class provides a simple means for generating unique ids. The identifiers generated by UUID are actually universally unique identifiers. To generate secrete key we can use Java KeyGenerator class which provides the functionality of a secret (symmetric) key generator. Key generators are constructed using one of the getInstance class methods of this class. Generate 16-digit unique code (like product serial). A product serial key. Anyway, I think this code is not satisfied with the third condition (no duplication.

  • AES (128)
  • DES (56)
  • DESede (168)
  • HmacSHA1
  • HmacSHA256
These algorithms are described in the KeyGenerator section of the Java Cryptography Architecture Standard Algorithm Name Documentation. Consult the release documentation for your implementation to see if any other algorithms are supported.
Ranch Hand
posted 14 years ago
Hi,
I have a set of numbers(may be 200- 300 numbers) and i need to generate a 8-10 digit/string key. I need this key to compare another key if that key is also made up of the same set of numbers. Is there a ways to generate a smaller key and also can be compared..
Thx in advance and its urgent
[ July 12, 2005: Message edited by: Mary Cole ]
Bartender
posted 14 years ago
How's about giving us a little more information, like what the nature of the original numbers is and what the purpose of the unique key is. I'm sitting here picturing a set of numbers from 1-200 or 300 and wondering why you need to generate another key to individually identify each one.
Ranch Hand
posted 14 years ago
What you're looking for is a cryptographic hashing function. There are generally two mainstream flavors: MD5 and SHA1.
http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html
http://java.sun.com/developer/technicalArticles/Security/Crypto/
Hashing functions are used to produce a unique key for some input. If the input changes in any way, then the hash produced will be completely different, however if the same input is supplied you will consistently be getting the same hash.
In UNIX, for example, passwords are not stored in cleartext. Instead, the passwords are hashed and the hash is stored. This way if a hacker gets his hands on the hash he/she has no way of getting back the password. This is a property of a one-way hash function - you can only go one way (from password to hash, and not from hash to password). When a user supplies his/her password again to login into the system, the password is hashed once again and compared to what is stored on file. If the hashes match, then you login.
Here I wrote a quick example for you:

[ July 12, 2005: Message edited by: Yevgeniy Treyvus ]
[ July 12, 2005: Message edited by: Yevgeniy Treyvus ]
[ July 12, 2005: Message edited by: Yevgeniy Treyvus ]
Ranch Hand
posted 14 years ago
Joe,
Basically what am doing is generating a token based on the user selection of the values in the fields in GUI. 3 scenarios can exist for these selections --
AN user can select multiple products (am storing only productids.which is numeric..and 4-5 digts in length....)
AN user can select multiple vendors(am storing only vendorids.which is numeric. usially 2-3 digits in lenght)
AN user can select both multiple products and vendors( which is again numberic values productid and vendorid)....
So I have an array whcih contains these numbers. I sort this array and build a plain string by looping thru the array.....so the length of the string might be 200+ characters if more selections are made.
Based on this I decide whehter the key already exists in my cache so that I can avoid the trip to DB for doing some calculations.
If another user also selects the same productid, vendorid and productid+vendorid combination i should get the same key so that I can use the cached data without hitting the DB
lets say userA has selected --
productID = 2456
vendorID = 34
productid,vendorid = 2834 , 18
So my token will be 183424562834 (after sorting and looping the array)
If the userB selects the same combination... his token will also be the same
Suppose an userC has following combination--
productID= 1834, 2456,2834 ( he has selected multiple products)
VendorId NONE selected
ProductId,vendorId = NONE selected
Now also the generated toke will be 183424562834.
So my cache result will always be wrong.
Is there any good token generation process so that i can handle all these conditions.
Thx in advance
U
Bartender
posted 14 years agoCode
I'm still confused. Is there a correlation between product and vendor? If there is, then there should be some correlation between their keys. That is, you can select a product id, a vendor id or some valid combination of vendor and product id's.
You probably want to have some sort of formatted field where you can specify a key for a vendor without product (i.e. xxxx-34), a product without vendor (i.e. 1234-xx) and a vendor-product combo (i.e. 1234-34).
Ranch Hand
posted 14 years ago
Joe,
Its like this... A product can stand on its own...a vendor can stand on its own...but there can be a combination of vendor-produt too...
So for independent product i have the key as '2345-PR'
For independent vendor i have '67-VE'
But for vend and prod combo i have it as '24-6677'
But when I want to build the key ...I will strip off all the characters so that I have plain numbers.
Hope am clear on this
Bartender
posted 14 years ago

Originally posted by Mary Cole:
I will strip off all the characters so that I have plain numbers.


I think that's your problem. You need some context to your key so you aren't confusing product 1234 with vendor 12 and vendor 34.
Ranch Hand
posted 14 years ago
If idon't strip off and hold plain numbers...how will I compare the key...coz an UserA might have product 1234 at first position in the array nd user 2 might have the same product at 5th place in the array....so my comparision will not work ( even thought they have the same combos)
Bartender
posted 14 years agoI'd consider a query like the one below:


productID= 1834, 2456,2834 ( he has selected multiple products)
VendorId NONE selected
ProductId,vendorId = NONE selected


to be 3 seperate queries, one for each product.
Ranch Hand
Generateposted 14 years ago
Even if i do like thats...is there a way to generate a shorter key 10 in lenght
Bartender

Java Create Unique Id

posted 14 years agoStill confused. I don't see any connection between the requirements you've specified so far and the need for a 10-digit key. Even if you wanted to take an entire query like this:


productID= 1834, 2456,2834 ( he has selected multiple products)
VendorId NONE selected
ProductId,vendorId = NONE selected


and shrink it down to 10 digits, you are losing information. You will have name collision, where two queries that are not equal will resolve to the same name and dropouts, where two queries that should be equal do not resolve to the same name.
If it were me, I'd cache each product id seperately, otherwise your cache will duplicate information (i.e. if you had another query productID= 1834, 2456, you don't want to store the results AGAIN, because they duplicate 2/3 of the previous query).

Java Code To Generate Unique Key Definition

Maybe you want to store all the query information in a class and override hashcode and equals to Do The Right Thing.

Java Code To Generate Unique Key Chain


Js Generate Unique Id

I get the feeling that this is a school assignment and since I don't have the objective for this lesson I'm missing the point of the exercise.