1<?xml version="1.0" encoding="ISO-8859-1"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5              This file is generated from xml source: DO NOT EDIT
6        XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7      -->
8<title>Password Formats - Apache HTTP Server</title>
9<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" />
12<script src="/style/scripts/prettify.min.js" type="text/javascript">
13</script>
14
15<link href="/images/favicon.ico" rel="shortcut icon" /></head>
16<body id="manual-page"><div id="page-header">
17<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
18<p class="apache">Apache HTTP Server Version 2.4</p>
19<img alt="" src="/images/feather.gif" /></div>
20<div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="/images/left.gif" /></a></div>
21<div id="path">
22<a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.4</a> &gt; <a href="./">Miscellaneous Documentation</a></div><div id="page-content"><div id="preamble"><h1>Password Formats</h1>
23<div class="toplang">
24<p><span>Available Languages: </span><a href="/en/misc/password_encryptions.html" title="English">&nbsp;en&nbsp;</a> |
25<a href="/fr/misc/password_encryptions.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a></p>
26</div>
27
28    <p>Notes about the password encryption formats generated and understood by
29    Apache.</p>
30  </div>
31<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#basic">Basic Authentication</a></li>
32<li><img alt="" src="/images/down.gif" /> <a href="#digest">Digest Authentication</a></li>
33</ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
34<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
35<div class="section">
36<h2><a name="basic" id="basic">Basic Authentication</a></h2>
37
38    <p>There are five formats that Apache recognizes for basic-authentication
39    passwords. Note that not all formats work on every platform:</p>
40
41    <dl>
42       <dt>bcrypt</dt>
43       <dd>"$2y$" + the result of the crypt_blowfish algorithm.
44       See the APR source file
45       <a href="http://svn.apache.org/viewvc/apr/apr/trunk/crypto/crypt_blowfish.c?view=markup">crypt_blowfish.c</a>
46       for the details of the algorithm.</dd>
47
48       <dt>MD5</dt>
49       <dd>"$apr1$" + the result of an Apache-specific algorithm using an
50       iterated (1,000 times) MD5 digest of various combinations of a
51       random 32-bit salt and the password. See the APR source file
52       <a href="http://svn.apache.org/viewvc/apr/apr/trunk/crypto/apr_md5.c?view=markup">apr_md5.c</a>
53       for the details of the algorithm.</dd>
54
55       <dt>SHA1</dt>
56       <dd>"{SHA}" + Base64-encoded SHA-1 digest of the password. Insecure.</dd>
57
58       <dt>CRYPT</dt>
59       <dd>Unix only. Uses the traditional Unix <code>crypt(3)</code> function
60       with a randomly-generated 32-bit salt (only 12 bits used) and the first 8
61       characters of the password. Insecure.</dd>
62
63       <dt>PLAIN TEXT (i.e. <em>unencrypted</em>)</dt>
64       <dd>Windows &amp; Netware only. Insecure.</dd>
65    </dl>
66
67    <h3>Generating values with htpasswd</h3>
68
69      <div class="example"><h3>bcrypt</h3><p><code>
70      $ htpasswd -nbB myName myPassword<br />
71      myName:$2y$05$c4WoMPo3SXsafkva.HHa6uXQZWr7oboPiC2bT/r7q1BB8I2s0BRqC
72      </code></p></div>
73
74      <div class="example"><h3>MD5</h3><p><code>
75      $ htpasswd -nbm myName myPassword<br />
76      myName:$apr1$r31.....$HqJZimcKQFAMYayBlzkrA/
77      </code></p></div>
78
79      <div class="example"><h3>SHA1</h3><p><code>
80      $ htpasswd -nbs myName myPassword<br />
81      myName:{SHA}VBPuJHI7uixaa6LQGWx4s+5GKNE=
82      </code></p></div>
83
84      <div class="example"><h3>CRYPT</h3><p><code>
85      $ htpasswd -nbd myName myPassword<br />
86      myName:rqXexS6ZhobKA
87      </code></p></div>
88
89    
90
91    <h3>Generating CRYPT and MD5 values with the OpenSSL
92             command-line program</h3>
93      
94
95      <p>OpenSSL knows the Apache-specific MD5 algorithm.</p>
96
97      <div class="example"><h3>MD5</h3><p><code>
98      $ openssl passwd -apr1 myPassword<br />
99      $apr1$qHDFfhPC$nITSVHgYbDAK1Y0acGRnY0
100      </code></p></div>
101
102      <div class="example"><h3>CRYPT</h3><p><code>
103      openssl passwd -crypt myPassword<br />
104      qQ5vTYO3c8dsU
105      </code></p></div>
106    
107
108    <h3>Validating CRYPT or MD5 passwords with the OpenSSL command
109             line program</h3>
110      
111      <p>The salt for a CRYPT password is the first two characters (converted to
112      a binary value). To validate <code>myPassword</code> against
113      <code>rqXexS6ZhobKA</code></p>
114
115      <div class="example"><h3>CRYPT</h3><p><code>
116      $ openssl passwd -crypt -salt rq myPassword<br />
117      Warning: truncating password to 8 characters<br />
118      rqXexS6ZhobKA
119      </code></p></div>
120
121      <p>Note that using <code>myPasswo</code> instead of
122      <code>myPassword</code> will produce the same result because only the
123      first 8 characters of CRYPT passwords are considered.</p>
124
125      <p>The salt for an MD5 password is between <code>$apr1$</code> and the
126      following <code>$</code> (as a Base64-encoded binary value - max 8 chars).
127      To validate <code>myPassword</code> against
128      <code>$apr1$r31.....$HqJZimcKQFAMYayBlzkrA/</code></p>
129
130      <div class="example"><h3>MD5</h3><p><code>
131      $ openssl passwd -apr1 -salt r31..... myPassword<br />
132      $apr1$r31.....$HqJZimcKQFAMYayBlzkrA/
133      </code></p></div>
134    
135
136    <h3>Database password fields for mod_dbd</h3>
137      <p>The SHA1 variant is probably the most useful format for DBD
138      authentication. Since the SHA1 and Base64 functions are commonly
139      available, other software can populate a database with encrypted passwords
140      that are usable by Apache basic authentication.</p>
141
142      <p>To create Apache SHA1-variant basic-authentication passwords in various
143      languages:</p>
144
145      <div class="example"><h3>PHP</h3><p><code>
146      '{SHA}' . base64_encode(sha1($password, TRUE))
147      </code></p></div>
148
149      <div class="example"><h3>Java</h3><p><code>
150      "{SHA}" + new sun.misc.BASE64Encoder().encode(java.security.MessageDigest.getInstance("SHA1").digest(password.getBytes()))
151      </code></p></div>
152
153      <div class="example"><h3>ColdFusion</h3><p><code>
154      "{SHA}" &amp; ToBase64(BinaryDecode(Hash(password, "SHA1"), "Hex"))
155      </code></p></div>
156
157      <div class="example"><h3>Ruby</h3><p><code>
158      require 'digest/sha1'<br />
159      require 'base64'<br />
160      '{SHA}' + Base64.encode64(Digest::SHA1.digest(password))
161      </code></p></div>
162
163      <div class="example"><h3>C or C++</h3><p><code>
164      Use the APR function: apr_sha1_base64
165      </code></p></div>
166
167      <div class="example"><h3>PostgreSQL (with the contrib/pgcrypto functions
168               installed)</h3><p><code>
169        
170        '{SHA}'||encode(digest(password,'sha1'),'base64')
171      </code></p></div>
172    
173
174  </div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
175<div class="section">
176<h2><a name="digest" id="digest">Digest Authentication</a></h2>
177    <p>Apache recognizes one format for
178    digest-authentication passwords - the MD5 hash of the string
179    <code>user:realm:password</code> as a 32-character string of hexadecimal
180    digits. <code>realm</code> is the Authorization Realm argument to the
181    <code class="directive"><a href="/mod/mod_authn_core.html#authname">AuthName</a></code> directive in
182    httpd.conf.</p>
183
184    <h3>Database password fields for mod_dbd</h3>
185
186      <p>Since the MD5 function is commonly available, other software can
187      populate a database with encrypted passwords that are usable by Apache
188      digest authentication.</p>
189
190      <p>To create Apache digest-authentication passwords in various
191      languages:</p>
192
193      <div class="example"><h3>PHP</h3><p><code>
194      md5($user . ':' . $realm . ':' .$password)
195      </code></p></div>
196
197      <div class="example"><h3>Java</h3><p><code>
198      byte b[] = java.security.MessageDigest.getInstance("MD5").digest( (user + ":" + realm + ":" + password ).getBytes());<br />
199      java.math.BigInteger bi = new java.math.BigInteger(1, b);<br />
200      String s = bi.toString(16);<br />
201      while (s.length() &lt; 32)<br />
202      <span class="indent">
203        s = "0" + s;
204      </span>
205      // String s is the encrypted password
206      </code></p></div>
207
208      <div class="example"><h3>ColdFusion</h3><p><code>
209      LCase(Hash( (user &amp; ":" &amp; realm &amp; ":" &amp; password) , "MD5"))
210      </code></p></div>
211
212      <div class="example"><h3>Ruby</h3><p><code>
213      require 'digest/md5'<br />
214      Digest::MD5.hexdigest(user + ':' + realm + ':' + password)
215      </code></p></div>
216
217      <div class="example"><h3>PostgreSQL (with the contrib/pgcrypto functions installed)</h3><p><code>
218        
219        encode(digest( user || ':' || realm || ':' || password , 'md5'), 'hex')
220      </code></p></div>
221
222    
223  </div></div>
224<div class="bottomlang">
225<p><span>Available Languages: </span><a href="/en/misc/password_encryptions.html" title="English">&nbsp;en&nbsp;</a> |
226<a href="/fr/misc/password_encryptions.html" hreflang="fr" rel="alternate" title="Fran�ais">&nbsp;fr&nbsp;</a></p>
227</div><div class="top"><a href="#page-header"><img src="/images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
228<script type="text/javascript"><!--//--><![CDATA[//><!--
229var comments_shortname = 'httpd';
230var comments_identifier = 'http://httpd.apache.org/docs/2.4/misc/password_encryptions.html';
231(function(w, d) {
232    if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
233        d.write('<div id="comments_thread"><\/div>');
234        var s = d.createElement('script');
235        s.type = 'text/javascript';
236        s.async = true;
237        s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
238        (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
239    }
240    else { 
241        d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
242    }
243})(window, document);
244//--><!]]></script></div><div id="footer">
245<p class="apache">Copyright 2014 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
246<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
247if (typeof(prettyPrint) !== 'undefined') {
248    prettyPrint();
249}
250//--><!]]></script>
251</body></html>