login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Snippets > Hash wachtwoorden

Hash wachtwoorden

Auteur: Stijn - 10 juni 2010 - 23:07 - Gekeurd door: Stijn - Hits: 4094 - Aantal punten: (0 stemmen)





Deze simpele klasse hashed je wachtwoord met een gekozen hashalgoritme. De algoritmes kan je terugvinden op deze pagina. Er wordt gebruik gemaakt van een salt. Verander deze waarde dan ook in de klasse naar een zelfgekozen random waarde. Je kan ook de positie veranderen waar de salt moet komen. Dus je bent niet gebonden aan vooraan of achteraan.

Voorbeeld (MD5 hash met een andere salt op positie 0):
  1. <?php
  2. //eerst de klasse instellen naar de gewenste parameters.
  3. /**
  4.  * The chosen hash algoritm.
  5.  */
  6. public static $HASH_ALGORITM = self::HASH_ALGORITM_MD5;
  7.  
  8. /**
  9.  * The salt string.
  10.  */
  11. public static $SALT = 'sitemasters';
  12.  
  13. /**
  14.  * The position in the password where the salt should be inserted.
  15.  */
  16. public static $SALT_INDEX_START = 0;

  1. <?php
  2. include_once 'Password.php';
  3. $password = new Password('mijn_wachtwoord');
  4. echo $password;
  5. ?>

Code:
  1. <?php
  2. /**
  3.  * Generate passwords based on a hash algoritm.
  4.  *
  5.  * @author Stijn Leenknegt <stijnleenknegt@gmail.com>
  6.  * @version 1.0
  7.  */
  8. class Password
  9. {
  10.  
  11.  
  12. /**
  13.   * The list of hash algoritms.
  14.   */
  15. const HASH_ALGORITM_MD4 = 'md4';
  16. const HASH_ALGORITM_MD5 = 'md5';
  17. const HASH_ALGORITM_SHA1 = 'sha1';
  18. const HASH_ALGORITM_SHA256 = 'sha256';
  19. const HASH_ALGORITM_SHA384 = 'sha384';
  20. const HASH_ALGORITM_SHA512 = 'sha512';
  21. const HASH_ALGORITM_RIPEMD128 = 'ripemd128';
  22. const HASH_ALOGIRTM_RIPEMD160 = 'ripemd160';
  23. const HASH_ALGORITM_WHIRLPOOL = 'whirlpool';
  24. const HASH_ALGORITM_TIGER128_3 = 'tiger128,3';
  25. const HASH_ALGORITM_TIGER160_3 = 'tiger160,3';
  26. const HASH_ALGORITM_TIGER192_3 = 'tiger192,3';
  27. const HASH_ALGORITM_SNEFRU = 'snefru';
  28. const HASH_ALGORITM_GOST = 'gost';
  29. const HASH_ALGORITM_ADLER32 = 'adler32';
  30. const HASH_ALGORITM_CRC32 = 'crc32':
  31. const HASH_ALGORITM_CRC32B = 'crc32b';
  32. const HASH_ALGORITM_HAVAL128_3 = 'haval128,3';
  33. const HASH_ALGORITM_HAVAL160_3 = 'haval160,3';
  34. const HASH_ALGORITM_HAVAL192_3 = 'haval192,3';
  35. const HASH_ALGORITM_HAVAL224_3 = 'haval224,3';
  36. const HASH_ALGORITM_HAVAL256_3 = 'haval256,3';
  37. const HASH_ALGORITM_HAVAL128_4 = 'haval128,4';
  38. const HASH_ALGORITM_HAVAL160_4 = 'haval160,4';
  39. const HASH_ALGORITM_HAVAL192_4 = 'haval192,4';
  40. const HASH_ALGORITM_HAVAL224_4 = 'haval224,4';
  41. const HASH_ALGORITM_HAVAL256_4 = 'haval256,4';
  42. const HASH_ALGORITM_HAVAL128_5 = 'haval128,5';
  43. const HASH_ALGORITM_HAVAL160_5 = 'haval160,5';
  44. const HASH_ALGORITM_HAVAL192_5 = 'haval192,5';
  45. const HASH_ALGORITM_HAVAL224_5 = 'haval224,5';
  46. const HASH_ALGORITM_HAVAL256_5 = 'haval256,5';
  47.  
  48.  
  49. /**
  50.   * The chosen hash algoritm.
  51.   */
  52. public static $HASH_ALGORITM = self::HASH_ALGORITM_WHIRLPOOL;
  53.  
  54. /**
  55.   * The salt string.
  56.   */
  57. public static $SALT = 'salt_:)';
  58.  
  59. /**
  60.   * The position in the password where the salt should be inserted.
  61.   */
  62. public static $SALT_INDEX_START = 4;
  63.  
  64. /**
  65.   * The password.
  66.   */
  67. private $_password = '';
  68.  
  69.  
  70. /**
  71.   * Create a password object.
  72.   * The password is hashed before it's setted in this class.
  73.   *
  74.   * @param string the password that should be hashed.
  75.   */
  76. public function __construct($password)
  77. {
  78. $this->_password = $this->_hash($password);
  79. }
  80.  
  81.  
  82. /**
  83.   * @return string the password is returned.
  84.   */
  85. public function getPassword()
  86. {
  87. return $this->_password;
  88. }
  89.  
  90.  
  91. /**
  92.   * Execute the hash algoritm.
  93.   *
  94.   * @param string the password.
  95.   * @return string the password hashed.
  96.   */
  97. protected function _hash($password)
  98. {
  99. $password = substr($password, 0, self::$SALT_INDEX_START) . self::$SALT . substr($password, self::$SALT_INDEX_START);
  100.  
  101. return hash(self::$HASH_ALGORITM, $password);
  102. }
  103.  
  104.  
  105. /**
  106.   * @return string the string representation of this password object. This gives the hashed password.
  107.   */
  108. public function __toString()
  109. {
  110. return $this->getPassword();
  111. }
  112.  
  113.  
  114. }
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (6)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.051s