See http://stackoverflow.com/questions/645659/how-do-you-htdigest-400-user-accounts
The easiest method, based on one of the suggestions in the top-voted answer, is probably this:
(echo -n "$user:$realm:" && echo -n "$user:$realm:$password" | md5sum | awk '{print $1}' ) >> /etc/apache2/pw/$user
I've used md5sum
from GNU coreutils and awk
rather than just md5
because it's what i have installed on my system and I couldn't be bothered finding out which package has /usr/bin/md5 - you could also use sha512sum or other hashing program.
e.g. if user=foo, realm=bar, and password=baz then the command above will produce:
foo:bar:5bf2a4095f681d1c674655a55af66c5a
htdigest doesn't do anything magical or even unusual - it just outputs the user, realm, and password in the right format...as the command above does.
Deleting the digest for a given user:realm instead of just adding one, can easily be done with sed. Updating/changing the digest for a user:realm can also be done with sed in combination with the method above to generate the digest line. e.g.
DIGESTLINE=$(echo -n "$user:$realm:" && echo -n "$user:$realm:$password" | md5sum | awk '{print $1}')
sed -i -e "/^$user:$realm:/ c$DIGESTLINE" /etc/apache2/pw/$user