我三流

いろいろ自分なりにやってみたことを書いています

htpasswd を openssl に置き換える

Apacheを使っていないけれどhtpasswdが必要になり、はてどうしたものか?と探していたら

「htpasswd を openssl に置き換える」を発見

htpasswd replacement with openssl

https://gist.github.com/samba/789122387e7f7330890b

 

 

#!/bin/sh

# Writes an APR1-format password hash to the provided <htpasswd-file> for a provided <username>
# This is useful where an alternative web server (e.g. nginx) supports APR1 but no `htpasswd` is installed.
# The APR1 format provides signifcantly stronger password validation, and is described here: 
#     http://httpd.apache.org/docs/current/misc/password_encryptions.html

help (){
cat <<EOF
  Usage: $0 <htpasswd-file> <username>
  Prompts for password (twice) via openssl.
EOF
}

[ $# -lt 2 ] && help;
[ $# -eq 2 ] && printf "${2}:`openssl passwd -apr1`\n" >> ${1}

 

htpasswd-ssl.shとして適当な場所に保存

ユーザーhogeがパスワード格納ファイル.htpasswdを作る場合

# htpasswd-ssl.sh .htpasswd hoge

するとパスワード入力を要求されるので設定したいパスワードを入力

再度、パスワードを聞かれるので入力すると.htpasswdにハッシュ化されたパスワードが入力されました