Cryptography Extensions in PHP


Today we will be learning about some cryptography extensions used in PHP in this Cryptography Extensions in PHP tutorial. But before having its introduction let us first understand what is cryptography.

  • What is cryptography?
    • Cryptography is the art and science of achieving security by encoding the plain-text messages into non-readable cipher-text messages.
    • The plain-text messages are the messages understood by everyone who know that particular language.
    • When a plain-text message is codified with a suitable scheme making it non-readable, the resulting message is called the cipher-text.
    • In other words, cryptography is essential to hide the confidential data which is to be kept highly secured from the outside world, by encoding and decoding whenever required.
    • Private keys and public keys are used in the encryption (encode) and decryption (decode) process of the messages.
    • Encrypting and decrypting the messages is done with the use of various security algorithms.
    • PHP has different implementations of the most important cryptographic algorithms.
    • Let us go through the cryptographic extensions that provide the implementation of the cryptographic functions.
  • Cryptography extensions:
  • The cryptography extensions are given below:

    1. Crack:
      • This function allows us to use the CrackLib library to test the strength of a password.
      • The strength of the password with its length, upper and lower characters is checked against the CrackLib library.
      • This CrackLib library is to be installed before using it.
      • Some of the Crack functions are:
        • crack_check: performs an obscure check with the given password.
        • crack_closedict: closes an open CrackLib dictionary.
        • crack_getlastmessage: returns the message from the last obscure check.
        • crack_opendict: opens a new CrackLib dictionary.
    2. Hash:
      • It uses a variety of hashing algorithms to process messages of different lengths.
      • With this extension we can generate hash values or HMAC (Hash-based Message Authentication Code).
      • This extension supports most of the hash algorithms used in strong cryptography.
      • Hash extension does not require external libraries to be installed and is enabled in PHP 5.1.2.
      • Some of the hash functions are:
        • hash_copy: copy hashing context.
        • hash_file: generates a hash value using the contents of a given file.
        • hash_hmac: generate a keyed hash value using HMAC method.
        • hash_init: initialize an incremental hashing context.
        • hash: generate a hash value (message digest).
    3. Mcrypt:
      • The mcrypt extension is the interface to the mcrypt library.
      • The mcrypt library supports a wide range of block algorithms like DES, Triple DES, Blowfish (default), TWOFISH, 3WAY, TEA, SAFER-SK64, SAFER-SK128, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes.
      • Mcrypt extension is mostly used in PHP to encrypt data using symmetric cipher.
      • Some of the Mcrypt functions are:
        • mcrypt_get_key_size: Gets the key size of the specified cipher.
        • mcrypt_list_algorithms: Gets an array of all supported ciphers.
        • mcrypt_encrypt: encrypts plaintext with given parameters.
        • mcrypt_decrypt: decrypts encrypted text with given parameters.
    4. Mhash:
      • This extension is an interface to the mhash library.
      • Mhash supports a wide variety of hash algorithms such as MD5, SHA1, GOST and many others.
      • Mhash can be used to create checksums, message digests, message authentication codes, etc.
      • Some of the mhash functions are:
        • mhash: computes hash.
        • mhash_count: gets the highest available hash ID.
        • mhash_get_hash_name: gets the name of the specified hash.
        • mhash_keygen_s2k: generates a key.
    5. OpenSSL:
      • The OpenSSL extension uses the functions of OpenSSL Project for generation and verification of signatures and for encrypting and decrypting data.
      • OpenSSL can be used to protect data using public key cryptography with the RSA algorithm.
      • Some of the OpenSSL functions are:
        • openssl_digest: computes a digest.
        • openssl_encrypt: encrypts data.
        • openssl_decrypt: decrypts data.
        • openssl_open: open sealed data.
        • openssl_error_string: returns openSSL error message.
    6. Password Hashing:
      • The API of password hashing extension provides an easy to use wrapper around crypt() to make it easy to create and manage passwords in a secure manner.
      • Some of the password hashing functions are:
        • password_hash: creates a password hash.
        • password_get_info: returns information about the given hash.
        • password_needs_rehash: checks if the given hash matches the given options.
        • password_verify: verifies that a password matches a hash.

Thus we had a short review on the cryptography extensions in PHP in this Cryptography Extensions in PHP tutorial.


Please enter your comment!
Please enter your name here