Download the PHP package drewlabs/crypt without Composer
On this page you can find all versions of the php package drewlabs/crypt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download drewlabs/crypt
More information about drewlabs/crypt
Files in drewlabs/crypt
Package crypt
Short Description Provides files encryption & Decryption, HMAC Hash object and Password Hashing using PHP password_hash, password_verify & hash_equals functions
License MIT
Informations about the package crypt
Crypt
Crypt is PHP composer based package providing hashing and encryption classes, and methods, based on PHP default hashing & encryption methods.
Installation
Simply use composer
PHP dependencies manager to install the package. If there is no composer installation on your operating system, you should be able to install the utility using this guide [https://getcomposer.org/download/].
Once composer is intalled on your operating system, run the comand below in your terminal at the root of your project:
composer require drewlabs/crypt
The above command will add drewlabs/crypt
package and it dependencies to your project.
Usage
Encrypting a raw string
Note
By default Crypt::new()
generate a random key and use AES-128-CBC
as cipher type. To override the defaults:
Note Supported cipher type are:
aes-128-cbc
andaes-128-gcm
-> Key length equals to 8 charactersaes-256-cbc
andaes-256-gcm
-> Key length equals 16 characters
Note If the key is a base64 string, crypt library will try to decode the base64 string before creating the encryption key internally:
- Decrypting a string
To get the plain text from an encrypted string, simply call the decryptString()
method on the encrypted text:
- File encryption
The Crypt
also class provides methods/functions for entrypting an entire file and decrypt the file from back to it original state. Below are the API for encrypting and decrypting files on a disk:
-
encryptBlob(string $from, string $to): void // Encrypt document located at path
$from
and output the encrypted content to path$to
- decryptBlob(string $from. string $to): void // Decrypt document located at path
$from
and output the encrypted content to path$to
HMAC Hashing
HMAC hashing provides methods for creating hash and checking a raw value against a hash using user defined algorightm. To create a hash value:
Note
By default, hashed value are created from strings
. But the Hmac implementation supports PHP arrays, and serializable objects (classes having a toArray()
). If the object does not have a toArray()
method, the hashing implementation call get_object_vars
on the object to convert the object into array, before hashing it.
Note
To create a hash object from existing raw hashed string use the Hash::raw()
method:
- Checking a hashed value
To check a hashed value against a new plain text value, first you create the hash object from raw hashed value and then you call the check()
method against the plain text.
Note
By default the library uses sha256
algorightm when creating hash values. PHP base function hash_algos
returns the list of supported hash algorithm.
Note Below is the api for hashing a value:
- make(?string $alg, ?string $key = null) // Creates a hashed value using user provided algorithm
- hashOptions() // Returns an encoded string composed of has key and algorithm used when hashing a value
- static raw(string $hash, ?string $options = null) // Static method for creating a hash object from a raw hashed value
- check(string $value) // Check the hash object internal hash value against a plain text value
Password encryption
The package also comes with implementation for creating md5
, argon2
, argon2i
and bcrypt
hash from plain text. It internally uses PHP password_hash
function for creating hash. They are recommended for hashing password for user applications. To create a password hash from plain text:
-
Hash manager factory
- Proxy function API