By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,034 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,034 IT Pros & Developers. It's quick & easy.

generate a unique key from a set of strings

P: n/a
I have a set of about 6 or so strings that I need to use to generate a
unique hash. This hash will become the unique key in a database so the
hash has to be the same each time I gen it for any 1 set of strings.

Is there something out there that already does this written in
javascript? I didn't find anything doing a google search.

Nov 25 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Om*****@gmail.com wrote:
I have a set of about 6 or so strings that I need to use to generate a
unique hash. This hash will become the unique key in a database so the
hash has to be the same each time I gen it for any 1 set of strings.

Is there something out there that already does this written in
javascript? I didn't find anything doing a google search.
If you're using php to connect to the database you can generate a key
using the md5 function of php ( http://www.php.net/md5 ). Javascript
has no such built in feature But there are open source projects that
allow you to generate a valid md5 key in javascript (
http://www.pajhome.org.uk/crypt/md5/ ).

Hope this helps.

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Nov 25 '06 #2

P: n/a
It does help. One question though. If I use an MD5 hash it isn't
guaranteed to be unique is it?
pcx99 wrote:
Om*****@gmail.com wrote:
I have a set of about 6 or so strings that I need to use to generate a
unique hash. This hash will become the unique key in a database so the
hash has to be the same each time I gen it for any 1 set of strings.

Is there something out there that already does this written in
javascript? I didn't find anything doing a google search.

If you're using php to connect to the database you can generate a key
using the md5 function of php ( http://www.php.net/md5 ). Javascript
has no such built in feature But there are open source projects that
allow you to generate a valid md5 key in javascript (
http://www.pajhome.org.uk/crypt/md5/ ).

Hope this helps.

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Nov 25 '06 #3

P: n/a
Omatase wrote:
It does help. One question though. If I use an MD5 hash it isn't
guaranteed to be unique is it?
No. MD5 has a low "collision" rate (2^50) but it's not impossible. The
js-md5 site I linked to earlier discusses this problem and recommends
using js-sha-256 ( http://anmar.eu.org/projects/jssha2/ ) if 2^50 odds
aren't enough for you. md5 is a 128 bit key, sha256 is a 256 bit key.

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Nov 25 '06 #4

P: n/a
with a 256 bit key that makes the collision rate 2^100? does 256 bit
mean that's the size of the key produced, or the size of the sipher
used to create the key?
pcx99 wrote:
Omatase wrote:
It does help. One question though. If I use an MD5 hash it isn't
guaranteed to be unique is it?

No. MD5 has a low "collision" rate (2^50) but it's not impossible. The
js-md5 site I linked to earlier discusses this problem and recommends
using js-sha-256 ( http://anmar.eu.org/projects/jssha2/ ) if 2^50 odds
aren't enough for you. md5 is a 128 bit key, sha256 is a 256 bit key.

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Nov 25 '06 #5

P: n/a
with a 256 bit key that makes the collision rate 2^100? does 256 bit
mean that's the size of the key produced, or the size of the sipher
used to create the key?
pcx99 wrote:
Omatase wrote:
It does help. One question though. If I use an MD5 hash it isn't
guaranteed to be unique is it?

No. MD5 has a low "collision" rate (2^50) but it's not impossible. The
js-md5 site I linked to earlier discusses this problem and recommends
using js-sha-256 ( http://anmar.eu.org/projects/jssha2/ ) if 2^50 odds
aren't enough for you. md5 is a 128 bit key, sha256 is a 256 bit key.

---------------------------------------------------------------------------
http://www.hunlock.com -- Permanently under construction (And proud of it!)
$FA
Nov 25 '06 #6

P: n/a
In comp.lang.javascript message
<11**********************@j44g2000cwa.googlegroups .com>, Sat, 25 Nov
2006 09:27:47, Om*****@gmail.com wrote:
>I have a set of about 6 or so strings that I need to use to generate a
unique hash. This hash will become the unique key in a database so the
hash has to be the same each time I gen it for any 1 set of strings.
If the membership of the set of strings is unconstrained, the
requirement for a guaranteed-unique hash is that it contain at least as
much information as the strings do. It will be possible to recover the
set of strings uniquely from the hash.

Then, if at least one of the strings is unbounded in length, the unique
hash must be unbounded in length too.

Then, you might as well use the set of strings itself, perhaps
compressed by ZIP or similar.

If you want to make more progress, you must either change your question
of accept the answer to a different question.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 26 '06 #7

P: n/a
Certainly what you are saying is true. What I gather from the comments
I have received thus far is that if I want the key to be guaranteed
unique then I need to use all of each string in a way that reversing
the hash would yield the original strings. That, although of course
works, isn't highly usable as an index for a table in a database.

I am certainly ok if it isn't 'guaranteed' unique, but I need to
understand how close each particular solution gets me to that goal so
that I can make a calculated decision to balance uniqueness and
usability.

The first response I got said the 128 bit md5 has has a 'low' collision
rate of 2^50. Does that effectively mean that I would only run the risk
of duplicating a key if I have more than 1125899906842624 rows in my
table? And how large is the result of hashing some strings using a 128
bit md5 hash? Is the result itself 16 bytes? These are all simple
little things that would help me a lot when making my decision.

Appreciate all of your comments

Dr J R Stockton wrote:
In comp.lang.javascript message
<11**********************@j44g2000cwa.googlegroups .com>, Sat, 25 Nov
2006 09:27:47, Om*****@gmail.com wrote:
I have a set of about 6 or so strings that I need to use to generate a
unique hash. This hash will become the unique key in a database so the
hash has to be the same each time I gen it for any 1 set of strings.

If the membership of the set of strings is unconstrained, the
requirement for a guaranteed-unique hash is that it contain at least as
much information as the strings do. It will be possible to recover the
set of strings uniquely from the hash.

Then, if at least one of the strings is unbounded in length, the unique
hash must be unbounded in length too.

Then, you might as well use the set of strings itself, perhaps
compressed by ZIP or similar.

If you want to make more progress, you must either change your question
of accept the answer to a different question.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 26 '06 #8

P: n/a
In comp.lang.javascript message
<11*********************@j44g2000cwa.googlegroups. com>, Sun, 26 Nov 2006
12:51:02, Omatase <Om*****@gmail.comwrote:
>Appreciate all of your comments

Dr J R Stockton wrote:
>It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c)
...
So why did you not heed that one properly?

--
(c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Nov 27 '06 #9

P: n/a
I'm just looking for help, if you don't want to help, but would rather
give antagonizing responses I don't think groups is a good place for
you.

Don't bother posting anything else antagonistic, I won't be checking
this article anymore.

Nov 28 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.