Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home SQL Server Encrypt With MD5, SHA Or SHA1 In MS SQL Server

Encrypt With MD5, SHA Or SHA1 In MS SQL Server

This article shows SQL statements that encrypt an input string by using MD5/SHA/SHA1 algorithm. And you should play with these SQL statements in the Microsoft SQL Server Management Studio (New Query) to see the results.

MD5/SHA/SHA1 Hash

SQL statements below returns the MD5, SHA, SHA1 hash of ‘123456’ string.

SELECT HashBytes('MD5','123456') 
SELECT HashBytes('SHA','123456')
SELECT HashBytes('SHA1','123456')

select HashBytes('MD5','123456') select HashBytes('SHA','123456') select HashBytes('SHA1','123456')

SQL statements Encrypt MD5 SHA SHA1

SQL statements Encrypt MD5 SHA SHA1

However, those return VarBinary instead of VarChar values:

0xE10ADC3949BA59ABBE56E057F20F883E
0x7C4A8D09CA3762AF61E59520943DC26494F8941B

0xE10ADC3949BA59ABBE56E057F20F883E 0x7C4A8D09CA3762AF61E59520943DC26494F8941B

Therefore, we need to use the built in function, fn_varbintohexstr from VarBinary to Hex String:

SELECT SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('MD5', '123456')), 3, 32) AS MD5
SELECT SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('SHA', '123456')), 3, 40) AS SHA
SELECT SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('SHA1', '123456')), 3, 40) AS SHA1

SELECT SubString(master.dbo.fn_varbintohexstr(HashBytes('MD5', '123456')), 3, 32) as MD5 SELECT SubString(master.dbo.fn_varbintohexstr(HashBytes('SHA', '123456')), 3, 40) as SHA SELECT SubString(master.dbo.fn_varbintohexstr(HashBytes('SHA1', '123456')), 3, 40) as SHA1

SQL Statement Encrypt MD5 SHA SHA1 From VarBinary To Hex String

SQL Statement Encrypt MD5 SHA SHA1 From VarBinary To Hex String

Note: in order to verify the MD5 result, you can use the Online Tool MD5 Generator.

Apr 18, 2012Hoan Huynh
WordPress Get Recent Posts And Recent CommentsJavaScript Detect Protocol (HTTP/ HTTPS) To Load Or Handle Accordingly
You Might Also Like:
  • PHP MD5 AND SHA1 Examples
  • ASP.NET Store Session In MS SQL Server Database
  • Replace String With NText Or Text Data Type In MS SQL Server
  • Capture And Display Execution Time Of SQL Query In SQL Server
  • SQL Server Get number of working days and business hours between start date and end date
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • PHP Connect To MS SQL Server
  • MSSQL Trim Function
  • SQL Server get first/last day of previous/current/next month
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago SQL Serverfn_varbintohexstr, HashBytes, md5, SHA, SHA1, substring, VarBinary, varchar3,666
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,451 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,832 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,650 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,985 views
C# Read Json From URL And Parse/Deserialize Json
11,712 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development