SHARE
    TWEET
    Guest User

    PBKDF2.class.php

    a guest
    Jul 13th, 2013
    3,603
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    PHP 6.76 KB | None | 0 0
    1. <?php
    2. /**
    3. * Password hashing with PBKDF2.
    4. * (modified to use the native php function if available)
    5. * Based on the pure PHP implementation of PBKDF2 which can be found on:
    6. * https://defuse.ca/php-pbkdf2.htm
    7. *
    8. * @author havoc AT defuse.ca (www: https://defuse.ca/php-pbkdf2.htm)
    9. * @author TheBlintOne
    10. *
    11. * @license Public Domain (so feel free to use it): http://en.wikipedia.org/wiki/Public_domain
    12. */
    13. /**
    14. * Class to encapsulate the PBKDF2 functions
    15. *
    16. * @author havoc AT defuse.ca (www: https://defuse.ca/php-pbkdf2.htm)
    17. * @author TheBlintOne
    18. */
    19. class PBKDF2
    20. {
    21. // These constants may be changed without breaking existing hashes.
    22. const PBKDF2_HASH_ALGORITHM = "sha256";
    23. const PBKDF2_ITERATIONS = 1000;
    24. const PBKDF2_SALT_BYTES = 24;
    25. const PBKDF2_HASH_BYTES = 24;
    26. const HASH_SECTIONS = 4;
    27. const HASH_ALGORITHM_INDEX = 0;
    28. const HASH_ITERATION_INDEX = 1;
    29. const HASH_SALT_INDEX = 2;
    30. const HASH_PBKDF2_INDEX = 3;
    31. /**
    32. * Creates a hash for the given password
    33. *
    34. * @param string $password the password to hash
    35. * @return string the hashed password in format "algorithm:iterations:salt:hash"
    36. */
    37. public function create_hash( $password )
    38. {
    39. $salt = base64_encode( mcrypt_create_iv( PBKDF2::PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM ) );
    40. return PBKDF2::PBKDF2_HASH_ALGORITHM . ":" . PBKDF2::PBKDF2_ITERATIONS . ":" . $salt . ":" .
    41. base64_encode( $this->hash(
    42. PBKDF2::PBKDF2_HASH_ALGORITHM,
    43. $password,
    44. $salt,
    45. PBKDF2::PBKDF2_ITERATIONS,
    46. PBKDF2::PBKDF2_HASH_BYTES,
    47. true
    48. ) );
    49. }
    50. /**
    51. * Checks if the given password matches the given hash created by PBKDF::create_hash( string )
    52. *
    53. * @param string $password the password to check
    54. * @param string $good_hash the hash which should be match the password
    55. * @return boolean true if $password and $good_hash match, false otherwise
    56. *
    57. * @see PBKDF2::create_hash
    58. */
    59. public function validate_password( $password, $good_hash )
    60. {
    61. $params = explode( ":", $good_hash );
    62. if( count( $params ) < HASH_SECTIONS )
    63. return false;
    64. $pbkdf2 = base64_decode( $params[ PBKDF2::HASH_PBKDF2_INDEX ] );
    65. return slow_equals(
    66. $pbkdf2,
    67. $this->hash(
    68. $params[ PBKDF2::HASH_ALGORITHM_INDEX ],
    69. $password,
    70. $params[ PBKDF2::HASH_SALT_INDEX ],
    71. (int)$params[ PBKDF2::HASH_ITERATION_INDEX ],
    72. strlen( $pbkdf2 ),
    73. true
    74. )
    75. );
    76. }
    77. /**
    78. * Compares two strings $a and $b in length-constant time
    79. *
    80. * @param string $a the first string
    81. * @param string $b the second string
    82. * @return boolean true if they are equal, false otherwise
    83. */
    84. public function slow_equals( $a, $b )
    85. {
    86. $diff = strlen( $a ) ^ strlen( $b );
    87. for( $i = 0; $i < strlen( $a ) && $i < strlen( $b ); $i++ )
    88. {
    89. $diff |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
    90. }
    91. return $diff === 0;
    92. }
    93. /**
    94. * PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
    95. *
    96. * Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
    97. *
    98. * This implementation of PBKDF2 was originally created by https://defuse.ca
    99. * With improvements by http://www.variations-of-shadow.com
    100. * Added support for the native PHP implementation by TheBlintOne
    101. *
    102. * @param string $algorithm the hash algorithm to use. Recommended: SHA256
    103. * @param string $password the Password
    104. * @param string $salt a salt that is unique to the password
    105. * @param int $count iteration count. Higher is better, but slower. Recommended: At least 1000
    106. * @param int $key_length the length of the derived key in bytes
    107. * @param boolean $raw_output [optional] (default false) if true, the key is returned in raw binary format. Hex encoded otherwise
    108. * @return string a $key_length-byte key derived from the password and salt,
    109. * depending on $raw_output this is either Hex encoded or raw binary
    110. * @throws Exception if the hash algorithm are not found or if there are invalid parameters
    111. */
    112. public function hash( $algorithm, $password, $salt, $count, $key_length, $raw_output = false )
    113. {
    114. $algorithm = strtolower( $algorithm );
    115. if( !in_array( $algorithm, hash_algos() , true ) )
    116. throw new Exception( 'PBKDF2 ERROR: Invalid hash algorithm.' );
    117. if( $count <= 0 || $key_length <= 0 )
    118. throw new Exception( 'PBKDF2 ERROR: Invalid parameters.' );
    119. // use the native implementation of the algorithm if available
    120. if( function_exists( "hash_pbkdf2" ) )
    121. {
    122. return hash_pbkdf2( $algorithm, $password, $salt, $count, $key_length, $raw_output );
    123. }
    124. $hash_length = strlen( hash( $algorithm, "", true ) );
    125. $block_count = ceil( $key_length / $hash_length );
    126. $output = "";
    127. for( $i = 1; $i <= $block_count; $i++ )
    128. {
    129. // $i encoded as 4 bytes, big endian.
    130. $last = $salt . pack( "N", $i );
    131. // first iteration
    132. $last = $xorsum = hash_hmac( $algorithm, $last, $password, true );
    133. // perform the other $count - 1 iterations
    134. for( $j = 1; $j < $count; $j++ )
    135. {
    136. $xorsum ^= ( $last = hash_hmac( $algorithm, $last, $password, true ) );
    137. }
    138. $output .= $xorsum;
    139. }
    140. if( $raw_output )
    141. return substr( $output, 0, $key_length );
    142. else
    143. return bin2hex( substr( $output, 0, $key_length ) );
    144. }
    145. }
    146. ?>
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /