<p>Is there a programmatic way to build <em>htpasswd</em> files, without depending on OS specific functions (i.e. <code>exec()</code>, <code>passthru()</code>)?</p>

[{'answer_id': 39935, 'author': 'Jordi Bunster', 'author_id': 4272, 'author_profile': 'https://Stackoverflow.com/users/4272', 'pm_score': -1, 'selected': False, 'text': '<p><a href="http://trac.edgewall.org" rel="nofollow noreferrer">Trac</a> ships with a Python replacement for htpasswd, which I\'m sure you could port to your language of choice: <a href="http://trac.edgewall.org/browser/trunk/contrib/htpasswd.py" rel="nofollow noreferrer">htpasswd.py</a>.</p>\n'}, {'answer_id': 39963, 'author': 'Greg Roberts', 'author_id': 4269, 'author_profile': 'https://Stackoverflow.com/users/4269', 'pm_score': 6, 'selected': True, 'text': '<p>.httpasswd files are just text files with a specific format depending on the hash function specified. If you are using MD5 they look like this:</p>\n\n<pre><code>foo:$apr1$y1cXxW5l$3vapv2yyCXaYz8zGoXj241\n</code></pre>\n\n<p>That\'s the login, a colon, ,$apr1$, the salt and 1000 times md5 encoded as base64. If you select SHA1 they look like this:</p>\n\n<pre><code>foo:{SHA}BW6v589SIg3i3zaEW47RcMZ+I+M=\n</code></pre>\n\n<p>That\'s the login, a colon, the string {SHA} and the SHA1 hash encoded with base64.</p>\n\n<p>If your language has an implementation of either MD5 or SHA1 and base64 you can just create the file like this:</p>\n\n<pre><code>&lt;?php\n\n$login = \'foo\';\n$pass = \'pass\';\n$hash = base64_encode(sha1($pass, true));\n\n$contents = $login . \':{SHA}\' . $hash;\n\nfile_put_contents(\'.htpasswd\', $contents);\n\n?&gt;\n</code></pre>\n\n<p>Here\'s more information on the format:</p>\n\n<p><a href="http://httpd.apache.org/docs/2.2/misc/password_encryptions.html" rel="noreferrer">http://httpd.apache.org/docs/2.2/misc/password_encryptions.html</a></p>\n'}, {'answer_id': 53006, 'author': 'Darryl Hein', 'author_id': 5441, 'author_profile': 'https://Stackoverflow.com/users/5441', 'pm_score': -1, 'selected': False, 'text': '<p>From what it says on the PHP website, you can use crypt() in the following method:</p>\n\n<pre><code>&lt;?php\n\n// Set the password &amp; username\n$username = \'user\';\n$password = \'mypassword\';\n\n// Get the hash, letting the salt be automatically generated\n$hash = crypt($password);\n\n// write to a file\nfile_set_contents(\'.htpasswd\', $username \':\' . $contents);\n\n?&gt;\n</code></pre>\n\n<p>Part of this example can be found: <a href="http://ca3.php.net/crypt" rel="nofollow noreferrer">http://ca3.php.net/crypt</a></p>\n\n<p>This will of course overwrite the entire existing file, so you\'ll want to do some kind of concatination.</p>\n\n<p>I\'m not 100% sure this will work, but I\'m pretty sure.</p>\n'}]
['https://Stackoverflow.com/questions/39916', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']