我有个小问题。
我创建了一个 MySQL 表,其中的密码列使用 SHA1 加密。
mysql> CREATE TABLE IF NOT EXISTS user_encrypted (
username varchar(50) COLLATE utf8_unicode_ci NOT NULL PRIMARY KEY DEFAULT 'username',
hashed_password varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'password',
user_mail varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL DEFAULT 'your@email.com',
user_phone varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL DEFAULT '+33 1 23 45 67 89',
user_online tinyint(1) NOT NULL DEFAULT '0',
user_enable tinyint(1) NOT NULL DEFAULT '1',
user_max_connection tinyint(1) NOT NULL DEFAULT '2',
user_start_date date NOT NULL DEFAULT '2015-05-01',
user_end_date date NOT NULL DEFAULT '0000-00-00',
KEY hashed_password (hashed_password)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
结果
Query OK, 0 rows affected (0.01 sec)
我有一个脚本,它调用 MySQL 表:
#!/bin/bash
. /etc/openvpn/script/config.sh
#New Encrypted_password
#username=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "select username from user_encrypted where username = '$username' AND hashed_password = '$password' AND user_enable=1 AND user_max_connection=2 AND user_start_date != user_end_date AND TO_DAYS(now()) >= TO_DAYS(user_start_date) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")
##Check user
[ "$username" != '' ] && [ "$username" = "$username" ] && echo "user : $username" && echo 'authentication ok.' && exit 0 || echo 'authentication failed.'; exit 1
问题是,当我使用脚本时,OpenVPN 要求我输入用户名/密码,我使用 Test 和 Test1234,但系统拒绝输入密码。 似乎“未加密”密码无法转换为加密密码,以检查表的密码是否与我在客户端 OpenVPN 中输入的密码相同。
我不知道该怎么做,在我的脚本 bash 中,我输入的密码被“转换”为加密密码,以便与数据库核对...
谢谢你
最佳答案
您的 bash 脚本不会散列密码。因此,您将密码与密码的散列进行比较。
你说它是一个 SHA1 散列,它很方便地以 a built-in function in MySQL 的形式存在。 .改变
where username = '$username' AND hashed_password = '$password' ...
进入
where username = '$username' AND hashed_password = SHA1('$password') ...
应该可以解决您眼前的问题。
我想提出您的代码的另外两个问题——不幸的是,这些都是非常常见的错误:
关于MySQL哈希列密码+脚本bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289986/