Mercurial Hosting > luan
comparison src/luan/host/Util.luan @ 1517:04615093b19d
use digest_message
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 09 Jun 2020 12:36:16 -0600 |
parents | 60f6741f000a |
children | f67f972bd648 |
comparison
equal
deleted
inserted
replaced
1516:70a55f49b98e | 1517:04615093b19d |
---|---|
5 local stringify = Luan.stringify or error() | 5 local stringify = Luan.stringify or error() |
6 local Io = require "luan:Io.luan" | 6 local Io = require "luan:Io.luan" |
7 local String = require "luan:String.luan" | 7 local String = require "luan:String.luan" |
8 local lower = String.lower or error() | 8 local lower = String.lower or error() |
9 local format = String.format or error() | 9 local format = String.format or error() |
10 local to_binary = String.to_binary or error() | |
10 local Binary = require "luan:Binary.luan" | 11 local Binary = require "luan:Binary.luan" |
11 local bytes = Binary.byte or error() | 12 local bytes = Binary.byte or error() |
12 local base64_encode = Binary.base64_encode or error() | 13 local base64_encode = Binary.base64_encode or error() |
14 local digest_message = Binary.digest_message or error() | |
13 local Hosted = require "luan:host/Hosted.luan" | 15 local Hosted = require "luan:host/Hosted.luan" |
14 local Sql = require "luan:sql/Sql.luan" | 16 local Sql = require "luan:sql/Sql.luan" |
15 local database = Sql.database or error() | 17 local database = Sql.database or error() |
16 local Logging = require "luan:logging/Logging.luan" | 18 local Logging = require "luan:logging/Logging.luan" |
17 local logger = Logging.logger "Util" | 19 local logger = Logging.logger "Util" |
18 | |
19 require "java" | |
20 local MessageDigest = require "java:java.security.MessageDigest" | |
21 | 20 |
22 | 21 |
23 local Util = {} | 22 local Util = {} |
24 | 23 |
25 function Util.read_password(domain) | 24 function Util.read_password(domain) |
26 domain = lower(domain) | 25 domain = lower(domain) |
27 return do_file(Hosted.sites_dir..domain.."/info.luan").password or error() | 26 return do_file(Hosted.sites_dir..domain.."/info.luan").password or error() |
28 end | 27 end |
29 | 28 |
30 local function basic_authentication(dir,password) | 29 local function basic_authentication(dir,password) |
31 local sha1 = MessageDigest.getInstance("SHA1").digest(password.getBytes()) | 30 local sha1 = digest_message("SHA1",to_binary(password)) |
32 local encoded = base64_encode(sha1) | 31 local encoded = base64_encode(sha1) |
33 local file = Io.schemes.file(dir.."/password.nginx") | 32 local file = Io.schemes.file(dir.."/password.nginx") |
34 file.delete() | 33 file.delete() |
35 file.write_text("admin:{SHA}"..encoded.."\n") | 34 file.write_text("admin:{SHA}"..encoded.."\n") |
36 end | 35 end |
37 | 36 |
38 local function digest_authentication(dir,password) | 37 local function digest_authentication(dir,password) |
39 local s = "admin:Restricted:"..password | 38 local s = "admin:Restricted:"..password |
40 local md5 = MessageDigest.getInstance("MD5").digest(s.getBytes()) | 39 local md5 = digest_message("MD5",to_binary(s)) |
41 md5 = {bytes(md5,1,#md5)} | 40 md5 = {bytes(md5,1,#md5)} |
42 local encoded = "" | 41 local encoded = "" |
43 for _, n in ipairs(md5) do | 42 for _, n in ipairs(md5) do |
44 encoded = encoded..format("%02x",n) | 43 encoded = encoded..format("%02x",n) |
45 end | 44 end |