SQL-based authentication: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 197:
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.
=={{header|C sharp|C#}}==
Class for hashing and random salt generation.
<lang csharp>using System.Security.Cryptography;
Line 920:
undef, $user, $pass);
$userid
}</lang>▼
=={{header|Perl 6}}==▼
{{trans|Perl}}▼
<lang perl6>▼
use v6;▼
use DBIish;▼
multi connect_db(:$dbname, :$host, :$user, :$pass) {▼
my $db = DBIish.connect("mysql",host => $host, database =>$dbname, user =>$user, password =>$pass, :RaiseError) ▼
or die "ERROR: {DBIish.errstr}.";▼
$db;▼
}▼
multi create_user(:$db, :$user, :$pass) {▼
#https://stackoverflow.com/questions/53365101/converting-pack-to-perl6▼
my $salt = Buf.new((^256).roll(16));▼
my $sth = $db.prepare(q:to/STATEMENT/);▼
INSERT IGNORE INTO users (username, pass_salt, pass_md5)▼
VALUES (?, ?, unhex(md5(concat(pass_salt, ?))))▼
STATEMENT▼
$sth.execute($user,$salt,$pass);▼
$sth.insert-id or Any;▼
}▼
multi authenticate_user (:$db, :$user, :$pass) {▼
my $sth = $db.prepare(q:to/STATEMENT/);▼
SELECT userid FROM users WHERE▼
username=? AND pass_md5=unhex(md5(concat(pass_salt, ?)))▼
STATEMENT▼
$sth.execute($user,$pass);▼
my $userid = $sth.fetch;▼
$userid[0] or Any;▼
}</lang>
Line 1,265 ⟶ 1,232:
(check-exn exn:fail? (λ () (authenticate-user test-DB #"tim" #"password")))
(check-true (authenticate-user test-DB #"tim" #"shh! it's a secret!")))</lang>
(formerly Perl 6)
▲{{trans|Perl}}
▲<lang perl6>
▲use v6;
▲use DBIish;
▲multi connect_db(:$dbname, :$host, :$user, :$pass) {
▲ my $db = DBIish.connect("mysql",host => $host, database =>$dbname, user =>$user, password =>$pass, :RaiseError)
▲ or die "ERROR: {DBIish.errstr}.";
▲ $db;
▲}
▲multi create_user(:$db, :$user, :$pass) {
▲ #https://stackoverflow.com/questions/53365101/converting-pack-to-perl6
▲ my $salt = Buf.new((^256).roll(16));
▲ my $sth = $db.prepare(q:to/STATEMENT/);
▲ INSERT IGNORE INTO users (username, pass_salt, pass_md5)
▲ VALUES (?, ?, unhex(md5(concat(pass_salt, ?))))
▲ STATEMENT
▲ $sth.execute($user,$salt,$pass);
▲ $sth.insert-id or Any;
▲}
▲multi authenticate_user (:$db, :$user, :$pass) {
▲ my $sth = $db.prepare(q:to/STATEMENT/);
▲ SELECT userid FROM users WHERE
▲ username=? AND pass_md5=unhex(md5(concat(pass_salt, ?)))
▲ STATEMENT
▲ $sth.execute($user,$pass);
▲ my $userid = $sth.fetch;
▲ $userid[0] or Any;
▲}</lang>
=={{header|Raven}}==
|