Java Generate Key From String

Greenhorn
posted 10 years ago
i want to compare objects. from each object i built a string. but this string is too long. so i want to create a key instead of keeping long string.
how can i create this key from the string.
Please help ! !
Marshal

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. Oct 09, 2019 The UUID v4 implementation uses random numbers as the source. The Java implementation is SecureRandom – which uses an unpredictable value as the seed to generate random numbers to reduce the chance of collisions. Let’s generate version 4 UUID: UUID uuid = UUID.randomUUID; Let's generate a unique key using ‘SHA-256' and a random UUID. The Java KeyGenerator class (javax.crypto.KeyGenerator) is used to generate symmetric encryption keys. A symmetric encryption key is a key that is used for both encryption and decryption of data, by a symmetric encryption algorithm. In this Java KeyGenerator tutorial I will show you how to generate symmetric encryption keys.

In this example you will generate a public/private key pair for the Digital Signature Algorithm (DSA). You will generate keys with a 1024-bit length. Generating a key pair requires several steps: Create a Key Pair Generator. The first step is to get a key-pair generator object for generating keys for the DSA signature algorithm. In this example you will generate a public/private key pair for the Digital Signature Algorithm (DSA). You will generate keys with a 1024-bit length. Generating a key pair requires several steps: Create a Key Pair Generator. The first step is to get a key-pair generator object for generating keys for the DSA signature algorithm.

posted 10 years ago
Welcome to JavaRanch
I presume you can't implement the Comparable interface? Can you create a Comparator?
If you put Strings as Keys in a Map, I didn't think there was any limit to its length.
Have I misunderstood your question?
author
posted 10 years ago
Yeah, I agree. It is better to have the objects compare themselves -- or even using a comparator -- than to convert it to a string for comparision. But, to answer your question..
There is no free lunch here. You can shorten a string, but you will lose data, and hence, accuracy in comparison. Generally, shortening a string is useful, if you only care to detect if two strings are *not* equal. The easiest way is to use the hashcode, since if two strings are not equal, then their hashcodes are not equal either. Another way is to use a message digest, such as MD5 or SHA1, but again, like with hashcode, it is possible for two unequal strings to have the same digest.
Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)

Greenhorn
posted 10 years ago
Thanks for the both replies.
inside my main object there are lot of sub objects. this main object is not just a string. so I create a unique string using the data inside this main object. this key would be length of 100 charactors.sometimes more than that. it depend on the data inside the main object.
And i have around 250 main objects to compare each other. so still you propose to keep the string key as the Map key?
I thought to use MD5 and create a hashcode.
also can two unequal objects have same hashcode if i use MD5?
Imesh
author
posted 10 years ago

can two unequal objects have same hashcode if i use MD5?


No free lunch. Yes, they can.
However, MD5 tries its best to have two objects that are very similar to have different digests. Meaning, if two strings are off by only one or two characters, then it is highly unlikely that the two digests are the same. On the other hand, if two strings are completely different, then it has a better chance that the digests are the same.
Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)

Java Cowboy
posted 10 years ago

Imesh Damith wrote:also can two unequal objects have same hashcode if i use MD5?


Yes they can, as Henry already said. Hash codes, in whatever form, are not going to help you here. It's theoretically impossible to make it so that two unequal objects always have different hash codes - if that would be possible, you'd have invented an incredible compression algorithm that violates the laws of information theory.
It's easy to see why this is theoretically impossible. Suppose you have a block of data containing N bits. Then there are 2^N possible ways you could fill this block of data. Now, you're going to calculate an M-bit hash code over the block of data (where M < N). So there are 2^M possible hash codes. Since M < N, 2^M < 2^N, so there are less possible hash codes than blocks of data. This means that there must be different blocks of data that have the same hash code.
Greenhorn
posted 10 years ago
hi, jesper and henry
Thanks for the explenation, so is there anyway that i can solve my problem? do i have to keep lengthy string itself as the key in the map?
Thanks
Imesh
Java Cowboy
posted 10 years ago
If you need to be able to distinguish two objects which are not the same, then you are going to need to compare the objects fully somehow. Whether you do this via this lengthy string that you create out of the content of the objects, or whether you just compare the state of two objects directly, is up to you.
In my answer above I said that hash codes are not going to help you. That's maybe not entirely true - you can use hash codes to make comparisons faster. That's how collections such as HashSet and HashMap in the standard Java API work. But you will still need a way to compare the complete content of objects.
To compare two objects, you could do this:
1. Check if the hash codes of the two objects are different. If they are, then you're done; the two objects are different.
2. If the hash codes are equal, then you need to compare the full content of the two objects to determine if they are equal or not.
If you do it that way, then you only need to do a full comparison if the hash codes of the two objects are equal.
I don't know how performance critical your code is, but on a normal computer strings of 100 characters and 250 objects that you need to compare can be done in a small fraction of a second. Don't waste too much time on performance optimization if you don't know that there's a performance problem.
Marshal
posted 10 years ago
An running it, you get . . .

java StringHashDemo Campbell
text length: 10000008, starts like this: Campbell멓㕑Campbellꀺ剂Campbell஻ꚲCampbell㡗罴Campbell噠떻Campbell꾌뷭Campbell꾱Campbell竵컗Campbell읥?Campbell⶘雦Campbell⛅䕴Campbell䞈躧Campbell਺Campbell炂滕Campbell䕘鷎Campbell푽쩊Campbell줕쬃Campbell苀횂CampbellꞲඐCampbell葻촻Campbell–纇Campbell佺Campbell諚嗝Campbell׬㺭Campbell폖썉Campbell퀿훂Campbell毆饐Campbell暼∙Campbell㩇뤃Campbell齋킎Campbellწ矒Campbell⁡嶔Campbell?婴Campbell㹇끋CampbellᤘࢫCampbell怚捳Campbell偑栛Campbell䦡ᕟCampbell⎀焰Campbellེ倏Campbell睚כCampbell봰癚CampbellCampbell愧驉Campbell羗偎Campbell墘矸Campbell뼛靼Campbell욌Campbell熇놂Campbell旱Campbell꫻풰Campbell涼నCampbellㅰ砬Campbell욪뒂Campbell轗ݣCampbell㦸벱CampbellⲖ㊣Campbell檫ȐCampbell剟ęCampbell䅲炘Campbell▪࿎Campbell閹쮪Campbell㵢䨮Campbell鄱箧Campbell풁㺫Campbell霟崜Campbell薨᪝CampbellЩ㏩Campbell䴅ᢙCampbell힉∊Campbell๾鱧Campbell?Campbell慰⸣Campbell愲?Campbell뢑뻆Campbell຅浓Campbell玶Campbell䙁惢Campbell퉿燀Campbell໐?Campbell橂阛Campbell襰ŒCampbell纫?Campbell빬㪚Campbell믥↻Campbell泯⊒Campbell≐箞Campbellꇤ㊦Campbell윋긙Campbell䮇⻾Campbell㇘霡Campbell安螪CampbellCampbell态莫Campbell࣑Campbell孵멒Campbellΐ认Campbellệ쏦Campbellड़Campbellௌ
. . . and the hashcode for that object follows, taking 50497.219 μs to work out . . . d10dfa69

It varies between 51 and 68 milliseconds. For 10 million characters. Is that really too slow for you?
Greenhorn
posted 10 years ago
Thanks guys. I will use the custom built string as the key. as you guys explained it would not be a performance hit.
Imesh
Ranch Hand
posted 10 years ago
In Java, how can a create a Unique ID from a particular string?
I have gone through the UUID class but could not find anything, also googled this but to find nothing.
Can any one please Help me on this?
Ranch Hand
posted 10 years ago
Try permutation and combination of a string element appended with some Randomized generated string/number.

[LEARNING bLOG] [Freelance Web Designer] [and 'Rohan' is part of my surname]

Ranch Hand
posted 10 years ago

Try permutation and combination of a string element appended with some Randomized generated string/number.


I think this doesn't work, because, the next time when i want to generate the Unique ID for the same string then this randomized generation doesn't work as the same Unique id doesn't get generated.
Here one more thing is the length of the generated Unique ID must be less than the original String.
Author
posted 10 years ago
Sounds like you want a guaranteed hash code for a string, that's guaranteed to be 'shorter' than the string. Not sure how possible that will be--what is the reason for this requirement?
Marshal
posted 10 years ago

Java Generate Key From String Example

Too difficult a question for us beginners. Moving.
Ranch Hand
posted 10 years ago
In Java 1.5, static UUID UUID.fromString(String name);
As mentioned previously, what about the hashcode converted into a String?
Integer.toString( myString.hashcode() );
Ranch Hand
posted 10 years ago

In Java 1.5, static UUID UUID.fromString(String name);


Here fromString creates a UUID for a valid UUID string and not any string.
Ranch Hand
posted 10 years agoKey
The String class may not have a hashCode() method, but Object does, and String is a subclass of Object. The same goes for Integer class and toString().
D'oh, previous post was edited. I'm still leaving my post up though.

SCJA
When I die, I want people to look at me and say 'Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was.'

Marshal

Java Generate Rsa Key From String

posted 10 years ago

Bharadwaj Adepu wrote:Here one more thing is the length of the generated Unique ID must be less than the original String.


So how should this work for the empty string? (There are no strings with length less than zero.)
And how should it work for strings of one character? (All strings of length zero are the same, so you can't have a unique representation.)
Ranch Hand
posted 10 years ago

Integer class doesn't have toString(java.lang.String) Method and String class doesn't have any hashcode() method.


From java.lang.Integer: public static String toString(int i)
A simple method name typo. java.lang.String: public int hashCode()

Java Generate Key Pair From String

But this doesn't meet your later criteria of unique ID length less then String length. It would seem that you need to write your own mapping function.
Ranch Hand
posted 10 years ago

So how should this work for the empty string? (There are no strings with length less than zero.)


Here in my usage/application, i'll not create the Unique id for empty strings.
Here actually what i want to do is, insert the generated unique id in the database, so if length of my string is more than 4000 and if generated unique string will have the length same as the original string(larger than 4000) then i cant insert that.
Ranch Hand
posted 10 years ago @Fred

Integer.toString( myString.hashCode() );


I think this serves my purpose, i have tested this and the hashCode generated is an number of around 10 to 12 digits, some times negative.
Thanks a LOT
But am not sure if this method creates a same hashCode for same string, i have tested this but not sure..
Author
posted 10 years ago

Bharadwaj Adepu wrote:But am not sure if this method creates a same hashCode for same string, i have tested this but not sure.. Mac free antivirus free download.


http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#hashCode()
AFAIK it's not guaranteed to be unique.
Ranch Hand
posted 10 years ago

Bharadwaj Adepu wrote:
But am not sure if this method creates a same hashCode for same string, i have tested this but not sure..


If you read the contract of the hashCode method, it states:

http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode() wrote: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.


This means that it will not necessarily produce the same hash code for the same object between runs of your JVM. Also, you stated you need a Unique ID for each String. Again, reading the hashCode contract, we learn:

http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode() wrote: It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.


So there is no guarantee that you will get a unique ID for two different Strings. Although the odds are low, there is a possibility you might get the same ID for two different Strings. The documentation for the String's overriding of the hashCode method does not give any indication that these warnings do not apply to its implementation. (I have not studied the String class's implementation to know either way.) Common sense would indicate that given that you are dealing with Strings of 4000 characters (8000 bytes) and bigger that you can not uniquely represent all possible permutations in only 4 bytes (the size of an int).
It sounds to me what you want is a compression method/process and not necessarily a 'unique ID'. You want to be able to store Strings over 4000 characters in less space, and always want the same result for the same string. Do you need the ability to reverse for this 'ID' to the original String? Or just get the same ID so you can look up related data in the database? I would suggest you look into compression methods and APIs.
Ranch Hand
posted 10 years ago
p.s. Of course another option might be to change your database field's data type to a CLOB rather than a VARCHAR(4000). It would probably be much easier and more reliable in the long run.
best scout
posted 10 years ago
Hi Bharadwaj,
I think what you need is a hash function. In general any kind of hash is a surjective projection (hope this is the correct mathematical name for it in English) which in your case means you're trying to map a given string consisting of a (large) number of characters to some kind of value which consists of a much smaller number of characters or bytes. This simply can't be done without collisions! Maybe you can reduce the size of the original string to some degree without loss with a compression function. But I guess it won't be possible to compress 4000 characters to 4 bytes without some information loss :-)
So obviously the only way to go is to use a hash function and even if it theoretically may sound like a problem to have collisions it's often not a problem in reality. In fact there may be collisions which will map different strings to the same hash code. The only question is how likely it is for a collision to happen! A good cryptographic hash function should really make it very unlikely to get collisions.
Of course you have to decide what 'good enough' means regarding your requirements!
Marco
Ranch Hand
posted 10 years ago

Bharadwaj Adepu wrote:

Try permutation and combination of a string element appended with some Randomized generated string/number.


I think this doesn't work, because, the next time when i want to generate the Unique ID for the same string then this randomized generation doesn't work as the same Unique id doesn't get generated.

Java Generate Key From String To Pdf

of course, it will work if you append/prepend/insert the random generated integer. And for the same string, I don't think the random generated number is same.

Bharadwaj Adepu wrote:
Here one more thing is the length of the generated Unique ID must be less than the original String.


With permutations you can restrict the length of resulted string.

[LEARNING bLOG] [Freelance Web Designer] [and 'Rohan' is part of my surname]