-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoring-password-in-plain-text-youre-doing-it-wrong.html
221 lines (194 loc) · 18.4 KB
/
storing-password-in-plain-text-youre-doing-it-wrong.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,400italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="http://www.emadmokhtar.com/theme/stylesheet/style.min.css">
<link rel="stylesheet" type="text/css" href="http://www.emadmokhtar.com/theme/stylesheet/pygments.min.css">
<link rel="stylesheet" type="text/css" href="http://www.emadmokhtar.com/theme/stylesheet/font-awesome.min.css">
<link href="http://www.emadmokhtar.com/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Emad Mokhtar's Framework Atom">
<link href="http://www.emadmokhtar.com/feeds/all.rss.xml" type="application/rss+xml" rel="alternate" title="Emad Mokhtar's Framework RSS">
<link rel="shortcut icon" href="http://www.emadmokhtar.com/images/favicon.ico" type="image/x-icon">
<link rel="icon" href="http://www.emadmokhtar.com/images/favicon.ico" type="image/x-icon">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="" />
<meta name="author" content="EmadMokhtar" />
<meta name="description" content="If you’re software developer then there is big chance you’ll implement user login feature, and you must store the username and password in your system. Most of developers stored these precious information in database as plain text, yes we did include myself I was doing it wrong, and if you’re doing it please stop and read this article to know how to do it right and being awesome. Username, Password, or Pin codes these are precious information, and if hacker or even internal employee stole the database he can do whatever he wants with your users information. So How to keep these information safe? The answer is hashing it. Why I was not doing that? I was lazy and every time I googled/binged/searched about this, I found many complex How-To articles, but last week I finally found something simple and it’s the reason I’m writing this article. What is hashing? Hashing is encoding string "message” to hash value and you can’t reproduce the original string “message” from hash value, so it’s one way cryptographic algorithm, it’s different than encryption that it’s two way cryptographic algorithm. How to Hash password …" />
<meta name="keywords" content="security, dotNet, Csharp">
<meta property="og:site_name" content="Emad Mokhtar's Framework"/>
<meta property="og:title" content="Storing password in plain text, you’re doing it wrong"/>
<meta property="og:description" content="If you’re software developer then there is big chance you’ll implement user login feature, and you must store the username and password in your system. Most of developers stored these precious information in database as plain text, yes we did include myself I was doing it wrong, and if you’re doing it please stop and read this article to know how to do it right and being awesome. Username, Password, or Pin codes these are precious information, and if hacker or even internal employee stole the database he can do whatever he wants with your users information. So How to keep these information safe? The answer is hashing it. Why I was not doing that? I was lazy and every time I googled/binged/searched about this, I found many complex How-To articles, but last week I finally found something simple and it’s the reason I’m writing this article. What is hashing? Hashing is encoding string "message” to hash value and you can’t reproduce the original string “message” from hash value, so it’s one way cryptographic algorithm, it’s different than encryption that it’s two way cryptographic algorithm. How to Hash password …"/>
<meta property="og:locale" content="en_US"/>
<meta property="og:url" content="http://www.emadmokhtar.com/storing-password-in-plain-text-youre-doing-it-wrong.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2013-03-06 16:41:00+03:00"/>
<meta property="article:modified_time" content=""/>
<meta property="article:author" content="http://www.emadmokhtar.com/author/emadmokhtar.html">
<meta property="article:section" content="ASP.NET"/>
<meta property="article:tag" content="security"/>
<meta property="article:tag" content="dotNet"/>
<meta property="article:tag" content="Csharp"/>
<meta property="og:image" content="http://www.emadmokhtar.com/images/profile.jpg">
<title>Emad Mokhtar's Framework – Storing password in plain text, you’re doing it wrong</title>
</head>
<body>
<aside>
<div>
<a href="http://www.emadmokhtar.com">
<img src="http://www.emadmokhtar.com/images/profile.jpg" alt="" title="">
</a>
<h1><a href="http://www.emadmokhtar.com"></a></h1>
<p>Geek developer who's in search of code perfection.</p>
<nav>
<ul class="list">
<li><a href="http://www.emadmokhtar.com">Blog</a></li>
<li><a href="http://www.emadmokhtar.com/pages/podcasts.html#podcasts">Podcasts</a></li>
<li><a href="http://www.emadmokhtar.com/pages/projects.html#projects">Projects</a></li>
<li><a href="https://emadmokhtar.github.io/resume" target="_blank">Resume</a></li>
</ul>
</nav>
<ul class="social">
<li><a class="sc-envelope-o" href="mailto:emad@emadmokhtar.com" target="_blank"><i class="fa fa-envelope-o"></i></a></li>
<li><a class="sc-github" href="https://www.github.com/EmadMokhtar" target="_blank"><i class="fa fa-github"></i></a></li>
<li><a class="sc-linkedin" href="https://www.linkedin.com/in/emadmokhtar/" target="_blank"><i class="fa fa-linkedin"></i></a></li>
<li><a class="sc-stack-overflow" href="http://stackoverflow.com/users/373051/emad-mokhtar" target="_blank"><i class="fa fa-stack-overflow"></i></a></li>
<li><a class="sc-facebook" href="https://www.facebook.com/emadmokhtarframework/" target="_blank"><i class="fa fa-facebook"></i></a></li>
<li><a class="sc-twitter" href="https://twitter.com/emadmokhtar" target="_blank"><i class="fa fa-twitter"></i></a></li>
</ul>
</div>
</aside>
<main>
<nav>
<a href="http://www.emadmokhtar.com">Home</a>
<a href="https://emadmokhtar.github.io/resume">Resume</a>
<a href="http://www.emadmokhtar.com/feeds/all.atom.xml">Atom</a>
<a href="http://www.emadmokhtar.com/feeds/all.rss.xml">RSS</a>
</nav>
<article>
<header>
<h1 id="storing-password-in-plain-text-youre-doing-it-wrong">Storing password in plain text, you’re doing it wrong</h1>
<p>Posted on Wed 06 March 2013 in <a href="http://www.emadmokhtar.com/category/aspnet.html">ASP.NET</a> <b>Read in 4 min.</b></p>
</header>
<div>
<p><img alt="Hacking for password" src="http://www.emadmokhtar.com/images/114166encryption.jpg"></p>
<p>If you’re software developer then there is big chance you’ll implement user login feature, and you must store the username and password in your system. Most of developers stored these precious information in database as plain text, yes we did include myself I was doing it wrong, and if you’re doing it please stop and read this article to know how to do it right and being awesome.</p>
<p>Username, Password, or Pin codes these are precious information, and if hacker or even internal employee stole the database he can do whatever he wants with your users information. So How to keep these information safe? The answer is hashing it.</p>
<p>Why I was not doing that? I was lazy and every time I googled/binged/searched about this, I found many complex How-To articles, but last week I finally found something simple and it’s the reason I’m writing this article.</p>
<h1>What is hashing?</h1>
<p>Hashing is encoding string "message” to hash value and you can’t reproduce the original string “message” from hash value, so it’s one way cryptographic algorithm, <a href="http://stackoverflow.com/questions/326699/difference-between-hashing-a-password-and-encrypting-it">it’s different than encryption</a> that it’s two way cryptographic algorithm.</p>
<h1>How to Hash password?</h1>
<p>As I mentioned above I found many articles about how-to hash password but most of them are very complex and I believe in work smart not hard, I found <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers(v=vs.99).aspx"><code>System.Web.Helpers</code></a> Namespace that has <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers.crypto(v=vs.99).aspx"><code>Crypto</code></a> class that has many hashing helpers. I play with it and try to learn how to hash a password.</p>
<h1>Flow:</h1>
<p>Now let’s understand the flow and then check how to implement it in code. First to save password in database you must hash it using one of hashing algorithm and store the hashes password into database.</p>
<p><img alt="StoreHashedPassword" src="http://www.emadmokhtar.com/images/StoreHashedPassword.png"></p>
<p>Second to check the user password in login, you should hash the password user input and compare the two hashed passwords (user input & stored in database).</p>
<p><img alt="RetreiveHashedPassword" src="http://www.emadmokhtar.com/images/RetreiveHashedPassword.png"></p>
<h1>Code:</h1>
<p>I’ll show you how to hash user’s password and check the password he’ll enter when login to your system. Like I said above I’ll user <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers.crypto(v=vs.99).aspx"><code>System.Web.Helpers.Crypto</code></a> class , this class has 2 methods <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers.crypto.hashpassword(vvs.99).aspx"><code>HashPassword</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers.crypto.verifyhashedpassword(v=vs.99).aspx"><code>VerifyHashedPassword</code></a> we’ll use them<em>.</em> we’ll write the code in Unit Tests so no need for Console Application and wait for Command Prompt to run, you’ll just write your code test it and you’ll get results in test results quickly, so let’s get our hand dirty:</p>
<h2>Hashing:</h2>
<p>What you need to do is to pass the user input password to <code>HashPassword()</code> method and you’ll get the hashed password.</p>
<div class="highlight"><pre><span></span><span class="n">plainPassword</span> <span class="p">=</span> <span class="s">"P@12345566"</span><span class="p">;</span>
<span class="n">hashedPassword</span> <span class="p">=</span> <span class="n">Crypto</span><span class="p">.</span><span class="n">HashPassword</span><span class="p">(</span><span class="n">plainPassword</span><span class="p">);</span>
</pre></div>
<h2>Comparing:</h2>
<p>We can verify the password by passing the hashed password and the plain password and <code>VerifyHashedPassword()</code> method will return true if it’s verified or false if it’s not.</p>
<div class="highlight"><pre><span></span>Assert.AreEqual(true, Crypto.VerifyHashedPassword(hashedPassword,plainPassword));
</pre></div>
<h2>Complete Code:</h2>
<div class="highlight"><pre><span></span><span class="k">[Test]</span>
<span class="na">public void test_hashing_password()</span>
<span class="na">{</span>
<span class="na">string plainPassword;</span>
<span class="na">string hashedPassword;</span>
<span class="na">plainPassword</span> <span class="o">=</span> <span class="s">"P@12345566";</span>
<span class="s"> hashedPassword = Crypto.HashPassword(plainPassword);</span>
<span class="s"> Assert.AreEqual(true, Crypto.VerifyHashedPassword(hashedPassword,plainPassword));</span>
<span class="na">}</span>
</pre></div>
<p>Note: I’m using <a href="http://www.nunit.org/">NUnit</a> as my Unit Test Framework and <a href="http://www.jetbrains.com/resharper/">Resharper</a> as my Test Runner.*</p>
<h1>Yet better Hashing:</h1>
<p>Even after hashing passwords you aren’t safe from hacking and hackers, hackers can use <a href="http://en.wikipedia.org/wiki/Rainbow_table">rainbow table</a> to attack your system. The good news is you can increase the security of your hashing by adding salt, let’s see how we can do this:</p>
<h1>Flow</h1>
<p>Save the password looks like the same flow but this time we’ll use <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers.crypto.generatesalt(v=vs.99).aspx"><code>GenerateSalt</code></a> method in <a href="http://msdn.microsoft.com/en-us/library/system.web.helpers.crypto(v=vs.99).aspx"><code>System.Web.Helpers</code></a> class to generate random salt that will be added to the password and store it to database with the hashed password.</p>
<p><img alt="YetBetterStoreHashingPassword" src="http://www.emadmokhtar.com/images/YetBetterStoreHashingPassword.png"></p>
<p>Retrieving and comparing user input password and the one stored in database is as before but you need to retrieve both the hashed password and salt from database, add salt to plain password, hash the produced string, then compare it with the hashed password that retrieved from database.</p>
<p><img alt="YetBetterRetreivePassword" src="http://www.emadmokhtar.com/images/YetBetterRetreivePassword.png"></p>
<h1>Code:</h1>
<h2>Hashing:</h2>
<div class="highlight"><pre><span></span><span class="n">plainPassword</span> <span class="p">=</span> <span class="s">"P@12345566"</span><span class="p">;</span>
<span class="n">salt</span> <span class="p">=</span> <span class="n">Crypto</span><span class="p">.</span><span class="n">GenerateSalt</span><span class="p">();</span>
<span class="n">passwordSaltCombination</span> <span class="p">=</span> <span class="n">plainPassword</span> <span class="p">+</span> <span class="n">salt</span><span class="p">;</span>
<span class="n">hashedPassword</span> <span class="p">=</span> <span class="n">Crypto</span><span class="p">.</span><span class="n">HashPassword</span><span class="p">(</span><span class="n">passwordSaltCombination</span><span class="p">);</span>
</pre></div>
<h2>Comparing:</h2>
<div class="highlight"><pre><span></span>Assert.AreEqual(true, Crypto.VerifyHashedPassword(hashedPassword, passwordSaltCombination));
</pre></div>
<h2>Complete Code:</h2>
<div class="highlight"><pre><span></span><span class="k">[Test]</span>
<span class="na">public void test_hasing_password_with_salt()</span>
<span class="na">{</span>
<span class="na">string plainPassword;</span>
<span class="na">string hashedPassword;</span>
<span class="na">string salt;</span>
<span class="na">string passwordSaltCombination;</span>
<span class="na">plainPassword</span> <span class="o">=</span> <span class="s">"P@12345566";</span>
<span class="s"> salt = Crypto.GenerateSalt();</span>
<span class="s"> passwordSaltCombination = plainPassword + salt;</span>
<span class="s"> hashedPassword = Crypto.HashPassword(passwordSaltCombination);</span>
<span class="s"> Assert.AreEqual(true, Crypto.VerifyHashedPassword(hashedPassword, passwordSaltCombination));</span>
<span class="na">}</span>
</pre></div>
<p>As you can see it’s simple procedure so please use it and stop saving plain password in your database.</p>
<p>I encourage you to read Jeff Atwood’s article <a href="http://www.codinghorror.com/blog/2007/09/youre-probably-storing-passwords-incorrectly.html">You're Probably Storing Passwords Incorrectly</a></p>
</div>
<div class="tag-cloud">
<p>
<a href="http://www.emadmokhtar.com/tag/security.html">security</a>
<a href="http://www.emadmokhtar.com/tag/dotnet.html">dotNet</a>
<a href="http://www.emadmokhtar.com/tag/csharp.html">Csharp</a>
</p>
</div>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'emadmokhtarsframework';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript>
</article>
<footer>
<p>© Emad Mokhtar </p>
<p>Built using <a href="http://getpelican.com" target="_blank">Pelican</a> - <a href="https://github.com/alexandrevicenzi/flex" target="_blank">Flex</a> theme by <a href="http://alexandrevicenzi.com" target="_blank">Alexandre Vicenzi</a></p> </footer>
</main>
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11401860-2', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"name": "Storing password in plain text, you’re doing it wrong",
"headline": "Storing password in plain text, you’re doing it wrong",
"datePublished": "2013-03-06 16:41:00+03:00",
"dateModified": "",
"author": {
"@type": "Person",
"name": "EmadMokhtar",
"url": "http://www.emadmokhtar.com/author/emadmokhtar.html"
},
"image": "http://www.emadmokhtar.com/images/profile.jpg",
"url": "http://www.emadmokhtar.com/storing-password-in-plain-text-youre-doing-it-wrong.html",
"description": "If you’re software developer then there is big chance you’ll implement user login feature, and you must store the username and password in your system. Most of developers stored these precious information in database as plain text, yes we did include myself I was doing it wrong, and if you’re doing it please stop and read this article to know how to do it right and being awesome. Username, Password, or Pin codes these are precious information, and if hacker or even internal employee stole the database he can do whatever he wants with your users information. So How to keep these information safe? The answer is hashing it. Why I was not doing that? I was lazy and every time I googled/binged/searched about this, I found many complex How-To articles, but last week I finally found something simple and it’s the reason I’m writing this article. What is hashing? Hashing is encoding string "message” to hash value and you can’t reproduce the original string “message” from hash value, so it’s one way cryptographic algorithm, it’s different than encryption that it’s two way cryptographic algorithm. How to Hash password …"
}
</script></body>
</html>