Introduction
This article shows how to convert a plain password to a hashed password using the MD5 algorithm on the client side and save it to the database.
For the demonstration, I will do,
- Create a table in the database to store the login credentials of the user.
- Create a website and add an MD5 conversion file of JavaScript.
- Add a page on the website with 2 textboxes for User ID and Password and a save button.
- Add a reference to the MD5 conversion file on the page and create a JavaScript function to convert the plain password to the hashed password.
- Add the code on the page load for adding JavaScript function via an attribute add of the save button and on the button click event save the data.
Step 1. Create a table named "LoginTable" in the database to store the login credentials of the user.
![SQL Query]()
Step 2. Create a website and add an MD5 conversion file of JavaScript.
- Create an empty website named "LoginCredentials".
![LoginCredentials]()
- Add a new folder in the root and name it "Scripts". Add the "md5.js" into the "Scripts" folder.
Note. You can find the "md5.js" in the attached file.
![Attached file]()
Step 3. Add a page on the website with 2 textboxes for User ID and Password and a save button.
- Add a page named "SaveData.aspx".
![Adding WebForm]()
- Add some controls on the page like:
- A text box for user id is named "txtUserID".
- A text box for password named "txtpwd" with TextMode="Password".
- Button for saving the data named "btn_save" with "onclick" event.
![Design View of WebForm]()
Step 4. Add a reference to the MD5 conversion file on the page and create a JavaScript function to convert the plain password to the hashed password.
- Add the reference of the MD5 conversion file on the page.
- Create a JavaScript function to convert the plain password to the hashed password in the "head" section of the page.
Note. The "hex_md5" function exists in the "md5.js" file.
![Function exists]()
Step 5. Add the code on the page load for adding JavaScript function via an attribute add of the save button and on the button click event to save the data.
- Add the JavaScript function via an attribute add of the save button.
- Save the data into the database on the save button click event.
At Run Time
After running the page, type the user ID and password.
![Run time]()
Note. Here User ID is "Admin" and the password is "abcd1234".
Result
Now the output in the database is as you can see.
![SQL View]()