Monday, 22 February 2016

SHA1 PHP encryption function

SHA1 accept a string as a key and return hash string which is encrypted. For decryption key is required, which is used in the encryption.

Syntax

string sha1 ( string $str [, bool $raw_output = false ] )

If the optional raw_output is set to TRUE, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number.

Note: Secure password hashing


It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See here for details.

example : 


<?php 
$msg="";
if(isset($_POST['submit']))
{
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("Could not connect to database! ".mysql_error($conn));
}
mysql_select_db("ajeetsin_ajeetsingh",$con);
$user_name=sha1($_POST['user']);
$password=sha1($_POST['pass']);
$quary="insert into users values(NULL,'$user_name','$password')" or die("error in making sql");
mysql_query($quary) or die("unable to run sql quary");
$msg="User name password saved";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SHA1 Function Demo : Girfa</title>
</head>

<body>
<h1>Create Hash Code : Girfa</h1><hr /><br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
User Name : <input type="text" name="user" /><br /><br />
Password : <input type="text" name="pass"  /><br /><br />
<input type="submit" name="submit"  value="Save" /><br />
<h3><?php echo $msg;?></h3>
</form>
</body>
</html>