SQL-based authentication: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Phix}}: syntax coloured, marked p2js incompatible) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(One intermediate revision by one other user not shown) | |||
Line 7:
This is the table definition:
<
userid int primary key auto_increment,
username varchar(32) unique key not null,
Line 14:
pass_md5 tinyblob not null
-- binary MD5 hash of pass_salt concatenated with the password
);</
(<tt>pass_salt</tt> and <tt>pass_md5</tt> would be <tt>binary(16)</tt> values, but MySQL versions before 5.0.15 strip trailing spaces when selecting them.)
Line 20:
{{libheader|mysqlclient}} (MySQL client library)
{{libheader|OpenSSL}} (for MD5)
<
#include <stdlib.h>
#include <string.h>
Line 193:
}
return EXIT_SUCCESS;
}</
From the command line, <tt>program add user password</tt> to add users, and <tt>program auth user password</tt> to see if the user with that password is authorized or not.
Line 199:
=={{header|C sharp|C#}}==
Class for hashing and random salt generation.
<
using System.Text;
Line 236:
}
}
}</
Class for creating and authenticating users.
<
namespace rosettaMySQL
Line 302:
}
}
}</
Class with main method and database connection method.
<
using MySql.Data.MySqlClient;
Line 356:
}
}
}</
Output
<pre>Bob has been created: True
Line 364:
=={{header|Go}}==
<
import (
Line 442:
// clear table to run program again
db.Exec(`truncate table users`)
}</
{{out}}
<pre>
Line 455:
=={{header|Java}}==
{{works with|Java|1.7}}
<
import java.sql.Connection;
import java.sql.DriverManager;
Line 596:
}
}
}</
=={{header|Julia}}==
<
using MySQL
using Nettle # for md5
Line 648:
println("""Mary does not authenticate with password of 123: $(authenticate_user(mydb, "Mary", "123")==false)""")
mysql_disconnect(mydb)
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.sql.Connection
Line 752:
}
}
}</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{libheader|DatabaseLink`}}
<
connectDb[dbUser_, dbPass_, dbUrl_] :=
OpenSQLConnection[JDBC["mysql", dbUrl], "Username" -> dbUser,
Line 779:
SQLColumn["username"] == user][[1]]},
Hash[FromCharacterCode[data[[2, 1]]] <> pass, "MD5"] ==
FromDigits[data[[3, 1]], 256]]; CloseSQLConnection[db]; rtn];</
=={{header|Nim}}==
Line 785:
Even if this program is only an example, we have chosen to generate the salt using the random number generator provided by the third-party module “nimcrypto” rather than using the PRNG from the standard module “random” which is totally inadequate for cryptographic usage.
<
proc connectDb(user, password: string): DbConn =
Line 850:
# Clean-up and close.
db.clean()
db.close()</
{{out}}
Line 858:
=={{header|Objeck}}==
<
use Encryption;
Line 961:
return true;
}
}</
=={{header|Perl}}==
Line 967:
{{libheader|DBI}}<br>
{{libheader|DBD::mysql}}
<
# returns a database handle configured to throw an exception on query errors
Line 996:
undef, $user, $pass);
$userid
}</
=={{header|Phix}}==
Uses SQLite, there is a MySQL wrapper in [http://phix.x10.mx/pmwiki/pmwiki.php PCAN] that I have not personally tried yet.<br>
Uses code from [[MD5/Implementation#Phix|MD5]], now modified to be an executable library.
<!--<
<span style="color: #000080;font-style:italic;">-- demo\rosetta\SQL-based_authentication.exw</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
Line 1,076:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 1,085:
=={{header|PHP}}==
To use MySQL in PHP you need the php_mysql module installed
<
function connect_db($database, $db_user, $db_password, $host = 'localhost', $port = NULL, $die = false) {
// Returns a MySQL link identifier (handle) on success
Line 1,155:
return $row['userid'];
}
</syntaxhighlight>
=={{header|Python}}==
{{works with|Python|2.7}}
Uses the [http://dev.mysql.com/downloads/connector/python/ official Python MySQL connector]
<
import hashlib
Line 1,242:
else:
print 'User %s failed' % user
</syntaxhighlight>
=={{header|Racket}}==
<
(require db file/md5)
(define-logger authentication)
Line 1,310:
(check-false (create-user test-DB #"tim" #"tim's password"))
(check-exn exn:fail? (λ () (authenticate-user test-DB #"tim" #"password")))
(check-true (authenticate-user test-DB #"tim" #"shh! it's a secret!")))</
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" line>
use v6;
use DBIish;
Line 1,344:
my $userid = $sth.fetch;
$userid[0] or Any;
}</
=={{header|Raven}}==
MySQL connectivity is available out of the box. Below, we do not ever send the plain text password over the wire.
<
'abcdefghijklmnopqrstuvwxyz0123456789' as $salt_chars
Line 1,379:
'foo' 'bar' authenticate_user !if "could not authenticate user\n" print bye
"user successfully created and authenticated!\n" print</
=={{header|Ruby}}==
Uses the [https://github.com/brianmario/mysql2 mysql2 gem]
<
require 'securerandom'
require 'digest'
Line 1,412:
password_md5 = Digest::MD5.hexdigest(user_record['pass_salt'] + password)
password_md5 == user_record['pass_md5']
end</
=={{header|Sidef}}==
{{trans|Perl}}
<
# returns a database handle configured to throw an exception on query errors
Line 1,442:
nil, user, pass
)
}</
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
Also requires the TDBC driver for MySQL.
<
proc connect_db {handleName dbname host user pass} {
Line 1,482:
# Only get here if no rows selected
error "authentication failed for user \"$user\""
}</
=={{header|Wren}}==
Line 1,491:
Not an exact 'phiximile' but follows the same lines.
<
import "./crypto" for Md5
Line 1,533:
addUser.call(db, "user", "password")
System.print("User with correct password: %(authenticateUser.call(db, "user", "password"))")
System.print("User with incorrect password: %(authenticateUser.call(db, "user", "wrong"))")</
{{out}}
|