คำตอบ:
คุณสามารถใช้ (ab) htpasswdจากแพ็คเกจapache-utilsได้หากคุณมีเวอร์ชั่น 2.4 หรือสูงกว่า
htpasswd -bnBC 10 "" password | tr -d ':\n'
-bใช้เวลารหัสผ่านจากการโต้แย้งคำสั่งที่สอง
-nพิมพ์กัญชาเพื่อ stdout แทนการเขียนไปยังแฟ้ม
-Bแนะใช้ bcrypt
-C 10ชุด bcrypt ค่าใช้จ่ายถึง 10
คำสั่ง bare htpasswd เอาต์พุตในรูปแบบ<name>: <hash>ตามด้วยบรรทัดใหม่สองบรรทัด ดังนั้นสตริงว่างสำหรับชื่อและtrการตัดเครื่องหมายโคลอนและการขึ้นบรรทัดใหม่
คำสั่งผล bcrypt กับ$2y$คำนำหน้าซึ่งอาจจะเป็นปัญหาสำหรับการใช้งานบางอย่าง แต่สามารถแก้ไขอีกsedตั้งแต่ตัวแปร OpenBSD ใช้$2a$เข้ากันได้กับตัวแปร crypt_blowfish $2y$คงใช้
htpasswd -bnBC 10 "" password | tr -d ':\n' | sed 's/$2y/$2a/'
ลิงก์ไปยังหน้า htpasswd man: https://httpd.apache.org/docs/2.4/programs/htpasswd.html
รายละเอียดเกี่ยวกับตัวแปร bcrypt: /programming//a/36225192/6732096
คุณสามารถใช้ไลบรารี Python ในระบบ Fedora ของฉันฉันทำ:
sudo dnf search bcrypt
(sudo เป็นเพียงเพื่อหลีกเลี่ยงการสูญเสียพื้นที่สำหรับแคชของผู้ใช้ dnf) และจากผลลัพธ์สามารถดูได้ว่ามีแพ็คเกจ Python2 และ Python3:
py-bcrypt.x86_64 : Python bindings for OpenBSD's Blowfish password hashing code
python3-py-bcrypt.x86_64 : Python 3 bindings for OpenBSD's Blowfish password hashing code
ติดตั้งเวอร์ชั่น Python2 และแสดงรายการไฟล์ในแพ็คเกจ:
sudo dnf install py-bcrypt.x86_64
rpm -ql py-bcrypt.x86_64
นี่แสดงให้เห็นว่ามีไฟล์อยู่ด้วย/usr/lib64/python2.7/site-packages/bcrypt/__init__.pyดังนั้นฉันสามารถรับเอกสารได้
pydoc bcrypt
นี่แสดงให้ฉันมากพอที่จะเขียนคำสั่งต่อไปนี้ซึ่งจะแฮชสตริง"password":
$ python -c 'import bcrypt; print(bcrypt.hashpw("password", bcrypt.gensalt(log_rounds=10)))'
$2a$10$vWFRZgbOx6RKOKYxCTtyWuMJM60E90Vdm/.0nj.X/o3dYUxvQ/2Dm
สำหรับรุ่นที่ใหม่กว่าbcryptใช้แทนrounds=log_rounds=
sudoเรียกใช้dnf searchมันทำงานได้ดีในฐานะผู้ใช้มาตรฐาน
log_roundsดูเหมือนว่าจะมีการเปลี่ยนแปลงที่จะทำให้มันเป็นrounds python -c 'import bcrypt; print(bcrypt.hashpw("password", bcrypt.gensalt(rounds=10)))'
@Disassemblerคำตอบเพิ่มเติมของ:
ps)15 เป็นความสมดุลที่ดีสำหรับความซับซ้อน / ความเร็วในการสร้างรหัสผ่านสคริปต์ตัวตัดสำหรับhtpasswd& bcrypt:
#!/bin/sh
## bcrypt passwd generator ##
#############################
CMD=$(which htpasswd 2>/dev/null)
OPTS="-nBC 15"
USERNAME=$1
usage() {
local script=$(basename $0)
cat <<EOF
$script: Generate Bcrypt Hashed Passwords using htpasswd
Usage: $script username
EOF
exit 1
}
check_config() {
if [ -z $CMD ]; then
printf "Exiting: htpasswd is missing.\n"
exit 1
fi
if [ -z "$USERNAME" ]; then
usage
fi
}
check_config $USERNAME
printf "Generating Bcrypt hash for username: $USERNAME\n\n"
$CMD $OPTS $USERNAME
exit $?
root /dev/null