I was looking around for concepts in building a reasonably secure HTML login form without using SSL, and I came across an interesting article (link at end of post). The concept it outlines is fairly simple, and I’m a little annoyed that I didn’t think of this myself earlier.

Essentially, the idea is that the password never actually leaves the client machine. Instead, the client sends a cryptographic hash of the password. For other security reasons, we also don’t want the server to store the password in plain text, so it should only store the hash value of the password.

Of course, this alone isn’t enough, because anyone scanning the wire could simply capture the hash and send that along to the server and authenticate. What we need is a way for the server and the client to agree that they have the same hash value for the password, without actually sending it. To accomplish this, we can set up the server to generate a random string and send that to the client. Then, both server and client append the password’s hash to the random string and perform a hash sum on the combined string. The client then sends that string to the server and if it agrees with the result the server got, we have a valid authentication.

The article referenced above included some sample code to illustrate this functionality, but I believe I can simplify it even further. It’s not a practical, real world example, because we’re not sending a user name or retrieving a password from a stored location on the server. But it should be enough to illustrate the concept and give a developer a head start in however they wish to implement. Personally, I plan to instantiate the code in a class and use XMLHttpRequest instead of traditional POST methods.

Anyway, on to the example code. Note: This example doesn’t actually look up any stored user login information. Instead it simply uses a pre-defined password: ‘password’.

We’ll need two files. The first file generates the server’s shared key and passes along the value to the client as well as the HTML and JavaScript needed to input a password, generate hash values and submit the form to the server.

Create main.php with the content:

// We'll use PHP's session handling to keep track of the server-generated key


// Function to generate a random key.
// Modified from code found at: http://www.totallyphp.co.uk/code/create_a_random_password.htm

function randomString($length) {
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    $str = NULL;
    $i = 0;
    while ($i < $length) {
        $num = rand(0, 61);
        $tmp = substr($chars, $num, 1);
        $str .= $tmp;
    return $str;

// Call the function and set the shared key

$key = $_SESSION['key'] = randomString(20);

    <!-- JavaScript that contains the functions which perform the actual hashing -->
    <script type="text/javascript" src="http://pajhome.org.uk/crypt/md5/sha1.js"></script>

    <!-- The following function creates the hash of the concatenated key and password hash
and submits the content to the server via a form -->
    <script type="text/javascript">
	function login() {
		var p = hex_sha1(document.getElementById('pass').value);
		var k = document.getElementById('key').value;
		var h = hex_sha1(k+p);
		var hash = document.getElementById('hash');
		hash.value = h;
		var f = document.getElementById('finalform');
    <form action="javascript:login()" method="post" >
	<input type="hidden" id="key" value="<?php echo $key; ?>" />
	<input type="password" id="pass" />
	<input type="submit" value="Submit" />
    <form action="login.php" method="post" id="finalform">
	<input type="hidden" name="hash" id="hash" />

Next we need the file to handle the submitted values and compare the results. Create login.php with the following contents:

$hash = $_POST['hash'];

$pass = sha1('password');
$key = $_SESSION['key'];

$server_hash = sha1($key.$pass);

if ($server_hash == $hash) {
	echo "MATCH!";
} else {
	echo "NO MATCH!";

That’s pretty much it. If you want to see a little more fluid example in action, see: http://www.lightcubesolutions.com/~jhuntwork/secure_login/

Referenced article: PHP – Implementing Secure Login with PHP, JavaScript, and Sessions (without SSL)

5 thoughts on “Secure PHP/JavaScript login without SSL

  1. The solution above has a “plaintext password equivalent” stored on the server. So, let’s assume the following threat: someone steals the file with SHA1 sums of the passwords. The information in this file is sufficient to authenticate against the PHP script, using a modified JavaScript (it should be easy to rewrite using GreaseMonkey). So, if we assume this threat model, the first hash in “both server and client append the password’s hash to the random string and perform a hash sum on the combined string” is useless. Of course it does prevent the admins from looking at the passwords, but they can still sell SHA1 sums together with a GreaseMonkey thing. Thus, when discussing such security solutions, it is necessary to specify the threat model explicitly.

    OTOH, there is an authentication protocol that neither has a plaintext password equivalent stored on the server, nor sends the unencrypted password over the wire: SRP (RFC 2945). And it can be implemented with JavaScript and Java.

    • (Replying to Alexander): You’re right, there are still potential weaknesses with this method. However, the threat you pointed out could still exist when using SSL encrypted connections, depending on how the passwords are stored, as the admins could still sell passwords or password equivalents. As you say, defining the model becomes important. In this particular scenario, we assume that the admins can be trusted not to sell password equivalents, but the actual password remains a personal and privately known string. For the stated purpose of sending private password strings over an unencrypted connection, this method does fill the need.

      Even so, thanks for the pointer to SRP, it will be something I will look into more as I have time.

  2. Hello sorry for my english.
    In login.php always returns mismatched not match because the latest hash generated in the.
    I think the problem is in this line “$ pass = sha1 (‘password’);” because I can not find where you enter the password that the user entered.
    I would appreciate guidance.


Leave a reply


<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>