Oto skrypt, który odczyta na liście nazw użytkowników, wygeneruje losowe hasło dla każdego i wypisze je zarówno do pliku htdigest, jak i zwykłego pliku tekstowego. Został przetestowany na systemie Linux, ale może wymagać modyfikacji w przypadku innych systemów. W szczególności, md5sum
może być md5
, a head
zawsze akceptuje flagę -c
.
#!/bin/bash
# auth realm for digest auth
AUTH_REALM=MyRealm
# file locations
# a file containing a list of user names,
# one name per line, e.g.,
# $ cat users.txt
# joe
# curly
# larry
USER_FILE=users.txt
# htdigest file, needs to exist
HTDIGEST_FILE=passwd.htdigest
# insecure password file
PASSWD_FILE=passwd.txt
# read the names from the user file
while read username
do
# generate a pseudo-random password
rand_pw=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8`
# hash the username, realm, and password
htdigest_hash=`printf $username:$AUTH_REALM:$rand_pw | md5sum -`
# build an htdigest appropriate line, and tack it onto the file
echo "$username:$AUTH_REALM:${htdigest_hash:0:32}" >> $HTDIGEST_FILE
# put the username and password in plain text
# clearly, this is terribly insecure, but good for
# testing and importing
echo "$username:$rand_pw" >> $PASSWD_FILE
done < $USER_FILE
To właśnie wejściowe i wyniki wyglądać, pierwszy plik nazwy użytkowników:
$ cat users.txt
joe
curly
larry
Uruchamianie skryptu:
$ ./load_users.bash
Powstały htdigest file:
$ cat passwd.htdigest
joe:MyRealm:2603a6c581f336f2874dbdd253aee780
curly:MyRealm:fd3f9d87bba654439d5ba1f32c0286a8
larry:MyRealm:c1c3c0dc50a9b97e9f7ee582e3fce892
I zwykły tekst fi le:
$ cat passwd.txt
joe:aLnqnrv0
curly:3xWxHKmv
larry:7v7m6mXY
Na jednym GNU/Linux przydałby (zaadaptowane z poleceniem FreeBSD powyżej): '(echo -n "Użytkownik: królestwo:" && echo -n "user: realm: passwd" | md5sum - | cut -d '' -f1) >> outfile' – blerontin