SQL-based authentication: Difference between revisions

→‎{{header|Tcl}}: Got rid of the external md5 requirement by "borrowing" SQL statements from the Perl version
(→‎Tcl: Added implementation)
(→‎{{header|Tcl}}: Got rid of the external md5 requirement by "borrowing" SQL statements from the Perl version)
Line 91:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
Also requires the TDBC driver for MySQL, and the md5 package from tcllib.
<lang Tcl>package require tdbc
package require
Line 102:
}
 
# A simple helper to keep code shorter
proc r64k {} {
expr int(65536*rand())
}
proc encode {salt pass} {
md5::md5 $salt$pass
}
 
Line 112 ⟶ 110:
set salt [binary format ssssssss \
[r64k] [r64k] [r64k] [r64k] [r64k] [r64k] [r64k] [r64k]]
set# md5Note [encodethat $we are using named parameters below, :user :salt $:pass]
# They are bound automatically to local variables with the same name
$handle allrows {
INSERT IGNORE INTO users (username, pass_salt, pass_md5)
VALUES (:user, :salt, :unhex(md5(concat(:salt, :pass))))
}
return ;# Ignore the result of the allrows method
Line 122 ⟶ 121:
proc authenticate_user {handle user pass} {
$handle foreach row {
SELECT userid, pass_salt, pass_md5 FROM users WHERE
username=:user LIMITAND 1pass_md5=unhex(md5(concat(pass_salt, :pass)))
} {
return [dict withget $row {userid]
if {$pass_md5 eq [encode $pass_salt $pass]} {
return $userid
}
}
}
# Only get here if no rows selected
error "authentication failed for user \"$user\""
}</lang>
Anonymous user