bredom
11/4/2017 - 10:26 PM

Hashowanie i sprawdzanie hasła

//Password hashing that can be used instead of crypt. It's simpler and faster.
$password = "haslo1234";
$hashed_pass = password_hash($password, PASSWORD_DEFAULT);
if (password_verify($password, $hashed_pass)) {
	echo "Password is valid"; //returns 1
}
	//1. Hashowanie i sprawdzanie hasła
	function password_encrypt($password) {
	 	$hash_format = "$2y$10$";

	 	$salt_length = 22;

	 	$salt = generate_salt($salt_length);
	 	$format_and_salt = $hash_format . $salt;
	 	$hash = crypt($password, $format_and_salt);
	 	return $hash;
	 }

	function generate_salt($salt_length) { 
	 	
	 	$unique_random_string = md5(uniqid(mt_rand(), true));

	 	$base64_string = base64_encode($unique_random_string);

	 	$modified_base64_string = str_replace('+', '.', $base64_string);

	 	$salt = substr($modified_base64_string, 0, $salt_length);

	 	return $salt;
	 }

	function password_check($password, $existing_hash) {
	 	
	 	$hash = crypt($password, $existing_hash);
	 	if($hash === $existing_hash) {
	 		return true;
	 	} else {
	 		return false;
	 	}
	 }