Crypt
in package
Tags
Table of Contents
Properties
- $bcrypt_cost : int
- The cost factor for bcrypt password hashing.
Methods
- __construct() : mixed
- create_instance() : mixed
- Creates an instance of the Crypt class if it does not already exist.
- create_password_hash() : string
- Creates a password hash using bcrypt.
- decrypt() : mixed
- Decrypts a string using a specified key and an optional random key.
- encrypt() : string
- Encrypts a string using a specified key and an optional random key.
- generate_password() : string
- Generates a random password of a specified length.
- json_decode_decrypt() : array<string|int, mixed>
- Decrypts a JSON string and decodes it to an array.
- json_encode_encrypt() : string
- Encodes an array to a JSON string and encrypts it.
- redefine_bcrypt_cost() : int
- Redefines the bcrypt cost factor based on a target time for hashing.
- verify_password_hash() : mixed
Properties
$bcrypt_cost
The cost factor for bcrypt password hashing.
public
int
$bcrypt_cost
= 8
This determines the computational cost of hashing passwords.
Methods
__construct()
public
__construct([mixed $crypt_key = '' ]) : mixed
Parameters
- $crypt_key : mixed = ''
create_instance()
Creates an instance of the Crypt class if it does not already exist.
public
static create_instance() : mixed
This method initializes the global $CRYPT variable with a new Crypt instance. It uses the cryptographic key defined in the Config class of the instance .
create_password_hash()
Creates a password hash using bcrypt.
public
create_password_hash(string $password) : string
This method hashes the provided password using the bcrypt algorithm with the defined cost factor. With PASSWORD_BCRYPT algo, the password length is limited to 72 characters (should be enough for most cases).
Parameters
- $password : string
-
The password to be hashed.
Return values
string —The hashed password.
decrypt()
Decrypts a string using a specified key and an optional random key.
public
decrypt([string $str = '' ][, string $key = '' ][, string $randomkey = '' ]) : mixed
This method reverses the encryption process, decompresses the string, and returns the original data. If no key is provided, it uses the default crypt_key defined in the class combined with a randomkey.
Parameters
- $str : string = ''
-
The encrypted string to be decrypted.
- $key : string = ''
-
The key used for decryption. If empty, the default crypt_key is used.
- $randomkey : string = ''
-
An optional random key to modify the decryption process.
Return values
mixed —The decrypted data, which can be a string or an unserialized array.
encrypt()
Encrypts a string using a specified key and an optional random key.
public
encrypt(string $str[, string $key = '' ][, string $randomkey = '' ]) : string
This method compresses the string, applies XOR encryption with the key, and returns the encrypted result. If no key is provided, it uses the default crypt_key defined in the class combined with a randomkey. It's better to specify a key and a randomkey and change them regularly to ensure security.
Parameters
- $str : string
-
The string to be encrypted.
- $key : string = ''
-
The key used for encryption. If empty, the default crypt_key is used.
- $randomkey : string = ''
-
An optional random key to modify the encryption process.
Return values
string —The encrypted string.
generate_password()
Generates a random password of a specified length.
public
generate_password([int $length = 8 ]) : string
This method creates a password consisting of letters and numbers, with a mix of uppercase and lowercase characters. can be used as an alternative to bcrypt password hashing for temporary passwords or other purposes.
Parameters
- $length : int = 8
-
The length of the generated password. Default is 8 characters.
Return values
string —The generated password.
json_decode_decrypt()
Decrypts a JSON string and decodes it to an array.
public
json_decode_decrypt(string $str) : array<string|int, mixed>
This method decrypts the input string using the Crypt class and then decodes the resulting JSON string to an array.
Parameters
- $str : string
-
The encrypted JSON string to be decrypted and decoded.
Return values
array<string|int, mixed> —The decoded array from the decrypted JSON string.
json_encode_encrypt()
Encodes an array to a JSON string and encrypts it.
public
json_encode_encrypt(array<string|int, mixed> $arr) : string
This method converts the input array to a JSON string and then encrypts it using the Crypt class.
Parameters
- $arr : array<string|int, mixed>
-
The array to be encoded and encrypted.
Return values
string —The encrypted JSON string.
redefine_bcrypt_cost()
Redefines the bcrypt cost factor based on a target time for hashing.
public
redefine_bcrypt_cost([int $timeTarget = 50 ]) : int
This method adjusts the cost factor until the hashing operation takes at least the specified time.
Parameters
- $timeTarget : int = 50
-
The target time in milliseconds for the password hashing operation.
Return values
int —The new bcrypt cost factor.
verify_password_hash()
public
verify_password_hash(mixed $password, mixed $hash) : mixed
Parameters
- $password : mixed
- $hash : mixed