Scripte

PHP Script: md5crypt-1.0.lib.php

Nach obenNach unten

Zurück zur Übersicht - Datei downloaden

  1. <?php
  2.  
  3. /*
  4. md5crypt.php5
  5. --------------
  6. Written by
  7.    - Dennis Riehle <selfhtml@riehle-web.com>
  8. Based on
  9.    - perl's Crypt::PasswdMD5 by Luis Munoz (lem@cantv.net)
  10.    - phyton's md5crypt.py by Michal Wallace http://www.sabren.com/
  11.    - /usr/src/libcrypt/crypt.c from FreeBSD 2.2.5-RELEASE
  12. Many thanks to
  13.    - Fabian Steiner <info@fabis-site.net>
  14.      without him this script would not work!!
  15. Version:   1.0 stable
  16. Last edit: Tue, 13 September 2005 13:49:28 GMT
  17. USAGE
  18.   $cryptedpassword = md5crypt_unix   ($password [, $salt [, $magicstring ]);
  19.   $apachepassword  = md5crypt_apache ($password [, $salt]);
  20. DESCRIPTION
  21.   unix_md5_crypt() provides a crypt()-compatible interface to the
  22.   rather new MD5-based crypt() function found in modern operating systems.
  23.   It's based on the implementation found on FreeBSD 2.2.[56]-RELEASE and
  24.   contains the following license in it:
  25.    "THE BEER-WARE LICENSE" (Revision 42):
  26.    <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
  27.    can do whatever you want with this stuff. If we meet some day, and you think
  28.    this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
  29.   apache_md5_crypt() provides a function compatible with Apache's
  30.   .htpasswd files. This was contributed by Bryan Hart <bryan@eai.com>.
  31. */
  32.  
  33.  
  34. $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  35.           // [a-zA-Z0-9./]
  36.  
  37. function md5crypt_to64($v, $n) {
  38.     global $itoa64;
  39.     $ret = '';
  40.    
  41.     while(--$n >= 0) {
  42.         $ret .= $itoa64{$v & 0x3f};   
  43.         $v = $v >> 6;
  44.     }
  45.     return $ret;
  46. }
  47.  
  48. function md5crypt_apache($pw, $salt = NULL) {
  49.     $Magic = '$apr1$';
  50.    
  51.     return md5crypt_unix($pw, $salt, $Magic);
  52. }
  53.  
  54. function md5crypt_unix($pw, $salt = NULL, $Magic = '$1$') {
  55.     global $itoa64;
  56.    
  57.     if($salt !== NULL) {
  58.         // Take care of the magic string if present
  59.         if(substr($salt, 0, strlen($Magic)) == $Magic) {
  60.             $salt = substr($salt, strlen($Magic), strlen($salt));
  61.         }
  62.         // Salt can have up to 8 characters
  63.         $parts = explode('$', $salt, 1);
  64.         $salt = substr($parts[0], 0, 8);
  65.     } else {
  66.         $salt = '';
  67.         mt_srand((double)(microtime() * 10000000));
  68.        
  69.         while(strlen($salt) < 8) {
  70.             $salt .= $itoa64{mt_rand(0, strlen($itoa64))};
  71.         }
  72.     }
  73.    
  74.     $ctx = $pw . $Magic . $salt;
  75.    
  76.     $final = pack('H*', md5($pw . $salt . $pw));
  77.    
  78.     for ($pl = strlen($pw); $pl > 0; $pl -= 16) {
  79.        $ctx .= substr($final, 0, ($pl > 16) ? 16 : $pl);
  80.     }
  81.        
  82.     // Now the 'weird' xform
  83.     for($i = strlen($pw); $i; $i >>= 1) {   
  84.         if($i & 1) {                // This comes from the original version,
  85.             $ctx .= pack("C", 0);   // where a memset() is done to $final                       
  86.         } else {                    // before this loop
  87.             $ctx .= $pw{0};
  88.         }
  89.     }
  90.    
  91.     $final = pack('H*', md5($ctx)); // The following is supposed to make
  92.                                     // things run slower
  93.  
  94.     for($i = 0; $i < 1000; $i++) {
  95.         $ctx1 = '';
  96.         if($i & 1) {
  97.             $ctx1 .= $pw;
  98.         } else {
  99.             $ctx1 .= substr($final, 0, 16);
  100.         }
  101.         if($i % 3) {
  102.             $ctx1 .= $salt;
  103.         }
  104.         if($i % 7) {
  105.             $ctx1 .= $pw;
  106.         }
  107.         if($i & 1) {
  108.             $ctx1 .= substr($final, 0, 16);
  109.         } else {
  110.             $ctx1 .= $pw;
  111.         }
  112.         $final = pack('H*', md5($ctx1));
  113.     }
  114.  
  115.     // Final xform
  116.     $passwd = '';
  117.     $passwd .= md5crypt_to64((intval(ord($final{0})) << 16)
  118.                                |(intval(ord($final{6})) << 8)
  119.                                |(intval(ord($final{12}))),4);
  120.     $passwd .= md5crypt_to64((intval(ord($final{1})) << 16)
  121.                                |(intval(ord($final{7})) << 8)
  122.                                |(intval(ord($final{13}))), 4);
  123.     $passwd .= md5crypt_to64((intval(ord($final{2})) << 16)
  124.                                |(intval(ord($final{8})) << 8)
  125.                                |(intval(ord($final{14}))), 4);
  126.     $passwd .= md5crypt_to64((intval(ord($final{3})) << 16)
  127.                                |(intval(ord($final{9})) << 8)
  128.                                |(intval(ord($final{15}))), 4);
  129.     $passwd .= md5crypt_to64((intval(ord($final{4}) << 16)
  130.                                |(intval(ord($final{10})) << 8)                       
  131.                                |(intval(ord($final{5})))), 4);
  132.     $passwd .= md5crypt_to64((intval(ord($final{11}))), 2);
  133.    
  134.     // Return the final string
  135.     return $Magic . $salt . '$' . $passwd;
  136. }
  137.  
  138. // EOF
  139.  
  140. ?>
  141.  

Zurück zur Übersicht - Datei downloaden

Nach obenNach unten

Valid XHTML 1.0! Valid CSS!

Letzte Änderungen: Sonntag, der 03. Februar 2008
© 2005, Dennis Riehle.