PHP offers a different variety of built-in functions and libraries to perform cryptographic operations such as ensuring data security and integrity.
Unique ID is essential for make something that unique to identify and track such as user, files and so on.
To generate random unique id, There are different methodologies used for different needs such as random integer with specified length, random alphanumeric character and random alphanumeric characters with symbol. Here, random unique id can be generate with built-in functions.
-
Random numbers only:
rand
function generates random integers from provided minimum and maximum range.$random_number = rand();
-
Random unique id with numbers only:
random_int
generates a cryptographically secure random integer within a specified range.$random_int = random_int(1, 10000);
-
Random unique is with alphanumeric characters:
random_bytes
generates a cryptographically secure random string of specified size in bytes. Convert generated bytes of string to hexadecimal characters withbin2hex
function. But in practical, It is recommended minimum 128 bit key to generate 32 characters of length.$random_bytes = random_bytes(16); // 128 bit or 16 byte $random_unique_id = bin2hex($random_bytes); // 32 character length of unique id
-
Random unique is with alphanumeric characters:
uniqid
generates a unique ID based on a prefix and a microsecond timestamp. It is simple to use and it may not be cryptographically secure.$uniqueId = uniqid();
Hashing is a cryptographic technique used to transform readable data into a fixed-size string of characters. It is often used for store passwords securely rather than plain text. Which cannot be readable and unable to crack the actual password. May be later it is used to verify with actual password before any authentication with actual user.
Salting is technique that adds a random value or data to the actual data before hashing, making it more difficult to crack for other. Such a way hashing a password with salting is relatively secure.
Note: Remember or store salting value or data for validate or decode the actual data.
Built-in Hash functions:
md5()
: Generates a 128-bit MD5 hash.sha1()
: Generates a 160-bit SHA-1 hash.sha256()
: Generates a 256-bit SHA-256 hash.hash()
: Generates the hash for given data with various algorithms like MD5, SHA1, SHA256, SHA512, etc.password_hash()
: Generates a strong password hash using various algorithms such as bcrypt, Argon2.
Example:
$hash = md5('secret_password');
$hash = sha1('secret_password');
$hash = sha256('secret_password');
$hash = hash('sha256', 'secret_password');
Example: Hashing a password
function hashPassword($password, $salt) {
return password_hash($password . $salt, PASSWORD_DEFAULT);
}
$salt = bin2hex(random_bytes(16));
$hashedPassword = hashPassword('secret_password', $salt);
Example: Verifying hashed password
When verifying the password with hashed password using password_verify
function, which returns boolean value of true
, if password matches otherwise return false
.
function verifyPassword($password, $hashedPassword, $salt) {
return password_verify($password . $salt, $hashedPassword);
}
It is stands for Hash-Based Message Authentication Code.
It is a cryptographic technique used to verify the integrity and authenticity of a data or message.
It combines different cryptographic hash function like SHA-256 with a secret key.
hash_hmac()
function is used to calculate the HMAC of a data or message. Which is more secure than any other hash function. To make more secure hashes with secret key.
Syntax:
string hash_hmac( string $algorithm, string $data, string $key [, bool $raw_output = false ] )
Parameters:
algorithm
: The hash algorithm to use (e.g.,sha256
,sha512
,md5
).data
: The data to be hashed.key
: The secret key used for HMAC calculation.raw_output
: It is by default false. Iftrue
, returns the raw binary output.
Example:
$message = "Welcome, user!";
$key = "secret_key";
$hmac = hash_hmac('sha256', $message, $key);
echo $hmac;
Key Points to Remember:
- Secret Key: Keep secret key confidential. It is used to verify the integrity of messages.
- Hash Function Strength: Prefer to use a strong hash function like SHA-256 or SHA-3.
- Salt: While not directly related to HMAC, salting passwords enhance security.
- Security Considerations:
- Key Strength: Use a strong and randomly generated key.
- Regular Key Rotation: Periodically rotate the generated key to enhance security.
Real-World Use Cases:
- API Authentication: To verify the authenticity of API requests and usages.
- Secure File Transfer: To ensure that files have not been tampered during transmission.
- Password Storage: To store passwords with strong hashing algorithm and salting.
Encryption is a cryptographic technique, which is used to make actual data to unreadable form.
Decryption is a cryptographic technique, which is used to convert or decrypt the encrypted data into actual data for readable.
Built-in Encryption function:
- OpenSSL: A powerful cryptographic library that supports various encryption and decryption algorithms.
$plaintext = "secret message"; // data to encrypt
$cipher = 'AES-256-CBC'; // cryptographic method
$ivlen = openssl_cipher_iv_length($cipher); // Length of Initialization Vector
$iv = openssl_random_pseudo_bytes($ivlen); // Initialization Vector for secure encryption
// Encryption
$ciphertext = openssl_encrypt($plaintext, $cipher, 'secret_key', 0, $iv);
// Decryption
$decrypted_text = openssl_decrypt($ciphertext, $cipher, 'secret_key', 0, $iv);
Security Considerations:
- Strong Algorithms: Use strong cryptographic algorithms.
- Key Management: Store keys with cipher, options and iv securely and avoid expose in the code.