• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generate Unique key

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'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.
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if i do like thats...is there a way to generate a shorter key 10 in lenght
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still 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).
Maybe you want to store all the query information in a class and override hashcode and equals to Do The Right Thing.
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic