2009-06-30 09:22:30 +00:00
|
|
|
<?php
|
2009-06-30 11:52:55 +00:00
|
|
|
// $Header: /cvsroot/phpldapadmin/phpldapadmin/lib/emuhash_functions.php,v 1.6.10.2 2008/12/12 12:20:23 wurley Exp $
|
2009-06-30 09:22:30 +00:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* emuhash - partly emulates the php mhash functions
|
|
|
|
* version: 2004040701
|
|
|
|
*
|
|
|
|
* (c) 2004 - Simon Matter <simon.matter@invoca.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Do we have builtin mhash support in this PHP version ? */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
if( ! function_exists( 'mhash' ) && ! function_exists( 'mhash_keygen_s2k' ) ) {
|
|
|
|
if( ! isset( $emuhash_openssl ) )
|
|
|
|
$emuhash_openssl = '/usr/bin/openssl';
|
|
|
|
|
|
|
|
// don't create mhash functions if we don't have a working openssl
|
2009-06-30 09:29:51 +00:00
|
|
|
if( ! file_exists( $emuhash_openssl ) )
|
2009-06-30 09:22:30 +00:00
|
|
|
unset( $emuhash_openssl );
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
elseif ( function_exists( 'is_executable' ) && ! is_executable( $emuhash_openssl ) )
|
|
|
|
unset( $emuhash_openssl );
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
if( ! isset( $emuhash_temp_dir ) )
|
|
|
|
$emuhash_temp_dir = '/tmp';
|
2009-06-30 09:22:30 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Define constants used in the mhash emulation code. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
define('MHASH_MD5', 'md5');
|
|
|
|
define('MHASH_SHA1', 'sha1');
|
|
|
|
define('MHASH_RIPEMD160', 'rmd160');
|
2009-06-30 09:22:30 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Functions to emulate parts of php-mash. */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
function openssl_hash( $openssl_hash_id, $password_clear ) {
|
|
|
|
global $emuhash_openssl, $emuhash_temp_dir;
|
2009-06-30 09:22:30 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
$current_magic_quotes = get_magic_quotes_runtime();
|
|
|
|
set_magic_quotes_runtime( 0 );
|
|
|
|
$tmpfile = tempnam( $emuhash_temp_dir, "emuhash" );
|
|
|
|
$pwhandle = fopen( $tmpfile, "w" );
|
2009-06-30 09:22:30 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
if( ! $pwhandle )
|
2009-06-30 11:52:55 +00:00
|
|
|
error(sprintf('Unable to create a temporary file %s to create hashed password',$tmpfile) ,'error','index.php');
|
2009-06-30 09:22:30 +00:00
|
|
|
|
2009-06-30 09:29:51 +00:00
|
|
|
fwrite( $pwhandle, $password_clear );
|
|
|
|
fclose( $pwhandle );
|
|
|
|
$cmd = $emuhash_openssl . ' ' . $openssl_hash_id . ' -binary < ' . $tmpfile;
|
|
|
|
$prog = popen( $cmd, "r" );
|
|
|
|
$pass = fread( $prog, 1024 );
|
|
|
|
pclose( $prog );
|
|
|
|
unlink( $tmpfile );
|
|
|
|
set_magic_quotes_runtime( $current_magic_quotes );
|
|
|
|
|
|
|
|
return $pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mhash( $hash_id, $password_clear ) {
|
|
|
|
switch( $hash_id ) {
|
|
|
|
case MHASH_MD5:
|
|
|
|
$emuhash = openssl_hash( MHASH_MD5, $password_clear );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MHASH_SHA1:
|
|
|
|
$emuhash = openssl_hash( MHASH_SHA1, $password_clear );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MHASH_RIPEMD160:
|
|
|
|
$emuhash = openssl_hash( MHASH_RIPEMD160, $password_clear );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$emuhash = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $emuhash;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mhash_keygen_s2k( $hash_id, $password_clear, $salt, $bytes ) {
|
|
|
|
return substr(pack("H*", bin2hex(mhash($hash_id, ($salt . $password_clear)))), 0, $bytes);
|
|
|
|
}
|
2009-06-30 09:22:30 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|