Girls Clothing

Car Radio

Affordable stylish trousers and shirts

Adams Childrenswear

Mountain Warehouse - Affordable outdoor clothing specialist

Jane Shilton

RyanAir Buy as you fly

JavaScript MD5

Introduction

The MD4, MD5 and SHA-1 algorithms are secure hash functions. They take a string input, and produce a fixed size number - 128 bits for MD4 and MD5; 160 bits for SHA-1. This number is a hash of the input - a small change in the input results in a substantial change in the output. The functions are thought to be secure, in the sense that it would require an enormous amount of computing power to find a string which hashes to a chosen value. In others words, there's no way to decrypt a secure hash. The uses of secure hashes include digital signatures and challenge hash authentication. This document is a good introduction to hashes

News: An alpha release is available for the next version of the MD5 and SHA1 scripts. They now support utf-8 input encoding and output in any arbitrary encoding. Also, the information about writing a login system has been much expanded, although the online example is not currently working.

Demonstration

Input
Calculate
Result
hex_md4("test hash") = "549089516e75bd13c41ff098fbb58d5e"
hex_md5("message digest") = "f96b697d7cb7938d525a2f31aaf161d0"
hex_sha1("160-bit hash") = "90d925d853c3d35cd54070bb75280fefad9de9e7"

The Scripts

MD4download v2.1view sourceRFC 1320old v1.1(originally by Jerrad Pierce)
MD5download v2.1view sourceRFC 1321old v1.1
SHA-1download v2.1view sourceFIPS PUB 180-1old v1.1
  • The code works with most JavaScript implementations; Andrew Kepert has written a browser compatibility test with on-line results.
  • It also works with other ECMA-script compatible languages, such as ActionScript.
  • MD4 is not considered as secure as the alternatives.

Quick instructions

First download the appropriate files from the links above. Save them in the same directory as your html file and insert a tag like:

<script type="text/javascript" src="md5.js"></script>

When you want to calculate a hash, use:

<script type="text/javascript">
    hash = hex_md5("input string");
</script>

or md4/sha1 appropriately. These functions return the hash in hexadecimal. The library can also generate HMACs for all three algorithms.

Also, Alejandro Gervasio has written a great set of articles walking you through creating a login system. See parts one, two and three.

Recently Discovered Weaknesses

Some weaknesses have recently been discovered in the MD5 and SHA-1 algorithms (more information). The hashes are designed so it is very difficult to find two messages that produce the same hash, this is called "collision resistance". Because MD5 is 128-bit, by random chance you will find a collision by producing 264 hashes. The weakness in MD5 is that a way has been found to produce such collisions with only 242 hashes. This makes producing collisions practical and I have seen an example of 100 different collisons.

The use of MD5 or SHA-1 for most JavaScript purposes (e.g. challenge-response login) does not rely on the collision resistance property. These weaknesses do not create any vulnerability in such web sites and there is no need to panic. If these weaknesses do concern you, there are alternative algorithms available:

Uses of hashes

  • Challenge hash authentication - a simple way to protect passwords during login.
  • One time passwords - a neat way to use a different password on every website, without having to remember them all.
  • Generating unique id numbers, e.g. from an email address
  • Harvesting entropy, e.g. squashing a passphrase down to a 128-bit cipher key
  • Data integrity and message authentication codes
  • Bit commitment, e.g. this logic game by Thomas Lussnig. Here the server sends a hash to the client, which commits it to a particular choice of secret colours, but without revealing what the colours are.
  • One-way encryption of passwords
  • To make a pseudo symmetric-encryption algorithm

Limitations of JavaScript Cryptography

Over the web, JS cryptography can only protect against passive eavesdropping, as the JavaScript itself is downloaded over an insecure link. If an attacker can modify network traffic, they can make malicious changes to the JavaScript code.

In any case, JS interpreters are not designed for secure programming. They may leave sensitive information lying about in memory. They're too slow for some algorithms, e.g. BSD-style MD5 passwords, or RSA with full-size keys. Bitwise operations are buggy in several implementations.

Here is some information on benchmarking the hash functions.

Users of my Script

Hash code in other languages

The ones marked * are based on my code.

More JavaScript Cryptography

There is a lot of low-grade JS crypto about, but these links are all to relatively high-grade algorithms:

© 1998 - 2008 Paul Johnston, distributed under the BSD License   Updated:02 Jan 2008