generate a unique key from a set of strings

Discussion in 'Javascript' started by Omatase@gmail.com, Nov 25, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. pcx99 Guest

    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
     
    pcx99, Nov 25, 2006
    #2
    1. Advertising

  3. Omatase Guest

    It does help. One question though. If I use an MD5 hash it isn't
    guaranteed to be unique is it?


    pcx99 wrote:
    > 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
     
    Omatase, Nov 25, 2006
    #3
  4. pcx99 Guest

    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
     
    pcx99, Nov 25, 2006
    #4
  5. Omatase Guest

    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
     
    Omatase, Nov 25, 2006
    #5
  6. Omatase Guest

    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
     
    Omatase, Nov 25, 2006
    #6
  7. In comp.lang.javascript message
    <>, Sat, 25 Nov
    2006 09:27:47, 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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Nov 26, 2006
    #7
  8. Omatase Guest

    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
    > <>, Sat, 25 Nov
    > 2006 09:27:47, 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.htm> jscr maths, dates, sources.
    > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Omatase, Nov 26, 2006
    #8
  9. In comp.lang.javascript message
    <>, Sun, 26 Nov 2006
    12:51:02, Omatase <> wrote:

    >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.
     
    Dr J R Stockton, Nov 27, 2006
    #9
  10. Omatase Guest

    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.
     
    Omatase, Nov 28, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Girish Sahani
    Replies:
    17
    Views:
    586
    Boris Borcic
    Jun 9, 2006
  2. ToshiBoy
    Replies:
    6
    Views:
    886
    ToshiBoy
    Aug 12, 2008
  3. Donkey Hottie
    Replies:
    25
    Views:
    927
    Mike Schilling
    Oct 6, 2008
  4. M P
    Replies:
    1
    Views:
    550
  5. Token Type
    Replies:
    9
    Views:
    391
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page