cryptDescriptioncrypt() encrypts a string using the crypt function from the operating system's C library. The function accepts two arguments: the string to encrypt, and the salt to use during encryption. A salt is a string of characters used to increase the number of encrypted strings that can be generated for a given string with a given encryption method. Salts help increase the effort needed to "crack" encrypted data. The function can encrypt strings using DES, Blowfish, and MD5 algorithms. Not all operating systems support some (or even any) of these algorithms. The exact algorithm depends on the format and length of salt passed to the function. Check your system documentation to see what salt length and format are used for each algorithm supported. If the salt argument is not provided, a random salt for the default encryption algorithm is generated. See your system documentation for more details - under UNIX-like operating systems, run the command man crypt. The crypt() function has several associated constants that help make it easier to use:
CautionThe behavior of crypt() is heavily dependent on the operating system implementation of crypt. Some versions of crypt truncate the string passed to eight characters in length before encrypting them. Salt length and format may also vary from one implementation to another. For example, $2$ is commonly used as the initial part of a Blowfish salt - however, on OpenBSD, $12$ is used instead. ExampleExample 1198. Check the availability of each crypt algorithm <?php $string = 'password'; $salt = md5(microtime()); // A format string for printf $format = "%-'.45s..%'.32s\n"; echo "The default salt length is ", CRYPT_SALT_LENGTH, "\n\n"; printf( $format, 'Default encryption', CRYPT_STD_DES ? crypt($string) : 'Not Supported' ); printf( $format, 'DES encryption', CRYPT_STD_DES ? crypt($string, substr($salt, 0, 2)) : 'Not Supported' ); printf( $format, 'Extended DES encryption (9 character salt)', CRYPT_EXT_DES ? crypt($string, substr($salt, 0, 2)) : 'Not Supported' ); printf( $format, 'Blowfish encryption', CRYPT_BLOWFISH ? crypt($string, '$2$'.substr($salt, 0, 13)) : 'Not Supported' ); printf( $format, 'MD5 encryption', CRYPT_MD5 ? crypt($string, '$1$'.substr($salt, 0, 9)) : 'Not Supported' ); ?> Sample Output under Windows 2000: The default salt length is 2. Default encryption................................................ZeNZsFJ14yGqQ DES encryption....................................................e5G0QZvvWg8L2 Extended DES encryption (9 character salt)........................Not Supported Blowfish encryption...............................................Not Supported MD5 encryption....................................................Not Supported
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|