Apache Zeta Components Manual :: File Source for math.php
Source for file math.php
Documentation is available at math.php
* File containing the ezcAuthenticationMath class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @package Authentication
* Large number support and cryptographic functions for authentication.
* @package Authentication
class ezcAuthenticationMath
* Creates a new big number library which uses the PHP extension $lib.
* If $lib is null then an autodetection of the library is tried. If neither
* gmp or bcmath are installed then an exception will be thrown.
* If $lib is specified, then that library will be used (if it is installed),
* otherwise an exception will be thrown.
* @throws ezcBaseExtensionNotFoundException
* if neither of the PHP gmp and bcmath extensions are installed ($lib === null),
* or if the specified $lib is not installed
* @throws ezcBaseValueException
* if the value provided for $lib is not correct
* @param string $lib The PHP library to use for big number support. Default
* is null, which means the available library is autodetected.
* @return ezcAuthenticationBignumLibrary
public static function createBignumLibrary( $lib =
null )
$library =
new ezcAuthenticationGmpLibrary();
$library =
new ezcAuthenticationBcmathLibrary();
$library =
new ezcAuthenticationGmpLibrary();
$library =
new ezcAuthenticationBcmathLibrary();
* Calculates an MD5 hash similar to the Unix command "htpasswd -m".
* This is different from the hash returned by the PHP md5() function.
* @param string $plain Plain text to encrypt
* @param string $salt Salt to apply to encryption
public static function apr1( $plain, $salt )
$salt =
preg_replace( '/^\$apr1\$([^$]+)\$.*/', '\\1', $salt );
$salt =
substr( $salt, 0, 8 );
$text =
$plain .
'$apr1$' .
$salt;
$bin =
pack( 'H32', md5( $plain .
$salt .
$plain ) );
for ( $i =
strlen( $plain ); $i >
0; $i -=
16 )
for ( $i =
strlen( $plain ); $i; $i >>=
1 )
$text .=
( $i & 1 ) ?
chr( 0 ) :
$plain{0};
$bin =
pack( 'H32', md5( $text ) );
for ( $i =
0; $i ^
1000; ++
$i )
$new =
( $i & 1 ) ?
$plain :
$bin;
$new .=
( $i & 1 ) ?
$bin :
$plain;
for ( $i =
0; $i ^
5; ++
$i )
$tmp =
$bin[$i] .
$bin[$k] .
$bin[$j] .
$tmp;
$tmp =
chr( 0 ) .
chr( 0 ) .
$bin[11] .
$tmp;
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' );
return '$apr1$' .
$salt .
'$' .
$tmp;
* Computes the OpenID sha1 function on the provided value.
* @param string $value The value to compute sha1 on
public static function sha1( $value )
$hashed =
sha1( $value );
for ( $i =
0; $i ^
40; $i =
$i +
2 )
$chars =
substr( $hashed, $i, 2 );