SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Best use of hash_hmac for passwords

  1. #1
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    768
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Best use of hash_hmac for passwords

    I want to hash passwordS for db storage using hash hmac algorythm and there are two ways I can think of, which one is better? In both cases I generate a random salt having 16 characters.

    First one - the salt is appended to the password and I use some random key. The salt will be different for each user while the key is constant for all. The key is stored in the php code:
    PHP Code:
    $db_password hash_hmac('sha256'$password.$saltCONSTANT_KEY); 
    Second - I use the salt as the key:
    PHP Code:
    $db_password hash_hmac('sha256'$password$salt); 
    Which method will be more secure?

  2. #2
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first method is more secure; the constant value acts as a pepper, an additional unknown for a malicious user; You can also increase the length of the salt

  3. #3
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    8,994
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What I do:
    PHP Code:
    function hmac $password$salt$algo 'sha512' ) {
      return 
    hash$algostrrev$salt ) . hash$algo$salt $passwordtrue ), true );
    }

    $ck '...long long super long constant salt...';
    $uk '...long long super long user salt...';

    $c hmachmac$password$uk ), $ck );
    var_dumpbase64_encode$c ) ); 
    I have a site specific salt which is the same for all users. But I also have a user specific salt that is randomly generated using a cryptographically secure pseudo-random number generator (CSPRNG) that is quite long, way more then 16 characters.

    Now...what I actually do is a little more complicated then what it shown here.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •