Introduction to JavaScript MD5
JavaScript MD5 is a library implementing the MD5 hashing function to compute checksum of any file. MD5 libraries are being build using JavaScript may it be on client side or on the server side to check the archived data. MD5 also known as Message – Digest Algorithm used as a Hash function produces 128-bit hash value. MD5 was initially designed to be used as a cryptographic hash function which suffered vulnerabilities in long run.
Syntax:
MD5 library Blueimp-md5 is to be installed.
MD is a standard one way function that allows input data to be mapped to fixed output. All MD5 implementations will produce 128 bit hash value from input data string, expressed as 32 digit hexadecimal number.
Example:
sample => 38539843fe90jt289021ut54879230
var sampleHash = md5("sampleString");
Installation
Here we have two types of installations, client side and server side installation need to be done.
1. Client Side Installation
Include minified MD5 JavaScript in HTML code.
Code:
<script src="https://cdn.educba.com/md5.min.js"></script>
Next, need to calculate the Hexadecimal coded MD5 hash string calling MD5 method using string as an argument.
2. Server Side Installation
Install blueimp-md5 package using NPM ( Node Package Manager ).
npm install blueimp-md5
Need to have a .Js file with below mentioned code in it.
Code:
require("http").createServer(function (req, res) {
var md5 = require("blueimp-md5"),
url = require("url"),
query = url.parse(req.url).query;
res.writeHead(200, {"Content-Type": "text/plain"});
// Computation and printing MD5 hash value
res.end(md5(query));
console.log(md("value"));
}).listen(8000, "localhost");
console.log("Server running at http://localhost:8000/");
On hit on the URL in browser, we can see the hash value: http://localhost:8000.
Example of JavaScript MD5
We will be using md5.js file which contains the logic requirements to use the blueimp-md5 library. We are implementing in NodeJs. Here we shall attach a zip folder of the code. Install the required dependencies using npm install and follow the above-mentioned steps.
Please unzip and use Visual Studio Code to run the application.
Code:
In package.json
{
"name": "blueimpmd5",
"version": "2.16.0",
"title": "JavaScript MD5",
"description": "JavaScript MD5 implementation. Compatible with server-side environments like Node.js, module loaders like RequireJS, Browserify or webpack and all web browsers.",
"keywords": [
"javascript",
"md5"
],
"homepage": "https://github.com/blueimp/JavaScript-MD5",
"author": {
"name": "Sebastian Tschan",
"url": "https://blueimp.net"
},
"contributors": [
{
"name": "Paul Johnston",
"url": "http://pajhome.org.uk/crypt/md5"
}
],
"repository": {
"type": "git",
"url": "git://github.com/blueimp/JavaScript-MD5.git"
},
"license": "MIT",
"devDependencies": {
"chai": "4",
"eslint": "7",
"eslint-config-blueimp": "2",
"eslint-config-prettier": "6",
"eslint-plugin-jsdoc": "25",
"eslint-plugin-prettier": "3",
"mocha": "7",
"prettier": "2",
"uglify-js": "3"
},
"eslintConfig": {
"extends": [
"blueimp",
"plugin:jsdoc/recommended",
"plugin:prettier/recommended"
],
"env": {
"browser": true,
"node": true
}
},
"eslintIgnore": [
"js/*.min.js",
"test/vendor"
],
"prettier": {
"arrowParens": "avoid",
"proseWrap": "always",
"semi": false,
"singleQuote": true,
"trailingComma": "none"
},
"scripts": {
"lint": "eslint .",
"unit": "mocha",
"test": "npm run lint && npm run unit",
"build": "cd js && uglifyjs md5.js -c -m -o md5.min.js --source-map url=md5.min.js.map",
"preversion": "npm test",
"version": "npm run build && git add -A js",
"postversion": "git push --tags origin master master:gh-pages && npm publish"
},
"files": [
"js/*.js",
"js/*.js.map"
],
"main": "js/md5.js",
"dependencies": {
"blueimp-md5": "^2.16.0"
}
}
These are all the dependencies required for MD5 JavaScript code to run.
In server.js
require('http')
.createServer(function (req, res) {
//MD5 module is being extracted here
var md5 = require('./js/md5'),
// Use the below line of code if you have installed using NPM
// var md5 = require("blueimp-md5"),
url = require('url'),
query = url.parse(req.url).query
res.writeHead(200, { 'Content-Type': 'text/plain' })
// Calculate and print the MD5 hash of the url query:
// res.end(md5(query))
console.log("The MD5 value of given Input: ", md5('educba'));
})
.listen(8000, 'localhost')
console.log('Server is started at http://localhost:8000/')
Output:
Here you can see that server has started on http://localhost:8000/, hit the browser with the URL, you can get the MD5 value of the input data.
Output:
So the MD5 value of input data ‘educba’ is bef91623c955ae2fa96eb26af3885643.
Lets change the input value to ‘401641’.
So the MD5 value of the given input is 1a86a71542de192c6d1875a0bd7f0cd5.
Output:
Lets change the console.log() line of code to:
Code:
console.log("The MD5 value of given Input: ", md5('Hi, this is JavaScript MD5 application'));
Output:
Importance of MD5 is to improve security for any website, hash values are coded as to not know the exact value to be used in the application backend. Server makes MD5 hash of the random values , it can be a password, which has to must be encoded to get stored in the backend database. Databases get hacked with which users find the passwords. So with MD5 these personal data can be encoded with security.
Conclusion
With the we conclude the article ‘JavaScript MD5’, although MD5 is now considered to be weak, because of less security reasons. More secure functions have come up such as SHA2 or SHA3 instead of MD5. We have seen how MD5 is implemented with MD5 library package installed in package.json as we are using NodeJs. One of the better options for security is to use Crypto.js JavaScript implementation of the hash functions.
Recommended Articles
This is a guide to JavaScript MD5. Here we discuss the introduction, installation and example of JavaScript MD5 respectively. You may also have a look at the following articles to learn more –