Generate a Unique Key

Discussion in 'PHP' started by FishSword, May 8, 2011.

  1. #1
    Hiya,

    What’s the best way of generating a unique key, that can’t be guessed easily?

    I would like to create a unique key for both account activation and referral purposes, that includes a check-sum to help prevent users from easily guessing other users activation or referral keys.

    Any help is greatly appreciated.

    Many Thanks,

    FishSword
     
    FishSword, May 8, 2011 IP
  2. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Im interested in the same thing, but passing a password that would generate the key.

    Example, I pass the word "beta" to the PHP and it would generate a unique key of lets say 100 characters.

    If you pass something else it generates other random garbage, but if you pass the same word "beta" again it generates exactly the same key. I saw this before but just cannot remember where.

    Basically I need to create a unique key on disc.
     
    @nibb, May 8, 2011 IP
  3. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    I think this can be done with the hash key function correct? Is that the best way?

    Thanks
     
    @nibb, May 8, 2011 IP
  4. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #4
    use md5 hash key guess that would be the best
     
    Bohra, May 8, 2011 IP
  5. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    What would you turn into a hash though? - I need the key to be unique.
     
    FishSword, May 8, 2011 IP
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    maybe the username+email or username with some random number
     
    Bohra, May 8, 2011 IP
  7. redslazers

    redslazers Peon

    Messages:
    242
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    to create a unique key you can combine a number of things:

    
    //Salt can be anything the more often it changes the better
    $salt = "some-string";
    
    //Generates a random string using the clock and add the salt as a prefix to the random sting
    $randomstring = uniqueid($salt);
    
    //Hash the random string using sha1
    $uniquekey = sha1($randomstring);
    
    PHP:
     
    redslazers, May 9, 2011 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    md5(implode($_SERVER));
    Code (markup):
     
    joebert, May 11, 2011 IP
  9. K.Meier

    K.Meier Well-Known Member

    Messages:
    281
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Use SHA1 incase the key is based on a username or so, since MD5 can easily be reverse engineered.
     
    K.Meier, May 11, 2011 IP