September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,29 @@
|
|||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
require 'mysql2'
|
||||
require 'securerandom'
|
||||
require 'digest'
|
||||
|
||||
def connect_db(host, port = nil, username, password, db)
|
||||
Mysql2::Client.new(
|
||||
host: host,
|
||||
port: port,
|
||||
username: username,
|
||||
password: password,
|
||||
database: db
|
||||
)
|
||||
end
|
||||
|
||||
def create_user(client, username, password)
|
||||
salt = SecureRandom.random_bytes(16)
|
||||
password_md5 = Digest::MD5.hexdigest(salt + password)
|
||||
|
||||
statement = client.prepare('INSERT INTO users (username, pass_salt, pass_md5) VALUES (?, ?, ?)')
|
||||
statement.execute(username, salt, password_md5)
|
||||
statement.last_id
|
||||
end
|
||||
|
||||
def authenticate_user(client, username, password)
|
||||
user_record = client.prepare("SELECT SELECT pass_salt, pass_md5 FROM users WHERE username = '#{client.escape(username)}'").first
|
||||
return false unless user_record
|
||||
|
||||
password_md5 = Digest::MD5.hexdigest(user_record['pass_salt'] + password)
|
||||
password_md5 == user_record['pass_md5']
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue