I used below function to create random token, and also a salt from the token. I used it in my application to prevent CSRF attack.
<?php
function RandomToken($length = 32){
if(!isset($length) || intval($length) <= 8 ){
$length = 32;
}
if (function_exists('random_bytes')) {
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
}
function Salt(){
return substr(strtr(base64_encode(hex2bin(RandomToken(32))), '+', '.'), 0, 44);
}
echo (RandomToken());
echo 'n';
echo Salt();
echo 'n';
/*
This function is same as above but its only used for debugging
*/
function RandomTokenDebug($length = 32){
if(!isset($length) || intval($length) <= 8 ){
$length = 32;
}
$randoms = array();
if (function_exists('random_bytes')) {
$randoms['random_bytes'] = bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
$randoms['mcrypt_create_iv'] = bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
$randoms['openssl_random_pseudo_bytes'] = bin2hex(openssl_random_pseudo_bytes($length));
}
return $randoms;
}
echo 'n';
print_r (RandomTokenDebug());
?>
Php Generate Random Alphanumeric String
Java generate ec key pair key. A random integer between min (or 0) and max (or getrandmax inclusive) Return Type: Integer: PHP Version: 4+ PHP Changelog: PHP 7.1: The rand function is an alias of mtrand. PHP 4.2.0: The random number generator is seeded automatically. Sep 30, 2018 In this article, we will explain how to Generate Random Alphanumeric String with PHP. This is a simple function that allows you to generate random strings containing Letters and Numbers (alphanumeric) using PHP. Key generation in aes.
Generate Random Key In Php Code
![Random Random](/uploads/1/2/6/0/126097471/836250189.jpeg)
Generate Random Api Key Php
Nov 21, 2018 As you can see, generating random and unique hexadecimal strings up to 40 characters long is very easy in PHP. https://skyeydraw.weebly.com/ssl-generate-csr-and-key.html. Generate Cryptographically Secure Random Strings. The three functions to generate random alphanumeric strings that we have discussed so far are not cryptographically secure. Generate ethereum private key javascript. Safely generating random integers in PHP isn't a trivial task. You should always check with your resident StackExchange cryptography experts before you deploy a home-grown algorithm in production. With a secure integer generator in place, generating a random string with a CSPRNG is a walk in the park. Creating a Secure, Random String.