Advertisement
dannysmc95

Economy System API

Jul 13th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.07 KB | None | 0 0
  1. eco = {}
  2. eco.__index = eco
  3.  
  4. url = "https://api.dannysmc.com/economy.php"
  5.  
  6. function eco.login(user, pass)
  7.     if user and pass then
  8.         local query = "cmd=user_login&username="..textutils.urlEncode(tostring(user)).."&password="..textutils.urlEncode(tostring(sha256(pass)))
  9.         local req = http.post(url, query)
  10.         req = req.readAll()
  11.         if req == "true" then
  12.             return true
  13.         else
  14.             return req
  15.         end
  16.     else
  17.         printError("Please specify a username and a password")
  18.     end
  19. end
  20.  
  21. function eco.register(user, pass)
  22.     if user and pass then
  23.         local query = "cmd=user_register&username="..textutils.urlEncode(tostring(user)).."&password="..textutils.urlEncode(tostring(sha256(pass)))
  24.         local req = http.post(url, query)
  25.         req = req.readAll()
  26.         if req == "true" then
  27.             return true
  28.         else
  29.             return req
  30.         end
  31.     else
  32.         printError("Please specify a username and a password")
  33.     end
  34. end
  35.  
  36. function eco.pay(user, pass, payee, message, amount)
  37.     local query = "cmd=eco_pay&username="..textutils.urlEncode(tostring(user)).."&password="..textutils.unserialize(tostring(sha256(pass))).."&touser="..textutils.unserialize(tostring(payee)).."msg="..textutils.unserialize(tostring(message)).."amount="..textutils.unserialize(tostring(amount))
  38.     local req = http.post(url, query)
  39.     req = req.readAll()
  40.     if req == "true" then
  41.         return true
  42.     else
  43.         return req
  44.     end
  45. end
  46.  
  47. function eco.book()
  48.     local req = http.post(url, "cmd=eco_book")
  49.     trans = textutils.unserialize(req.readAll())
  50.     assert(type(trans) == "table")
  51.     return trans
  52. end
  53.  
  54. function eco.view_account(username, password)
  55.     if user and pass then
  56.         local query = "cmd=eco_acc&username="..textutils.urlEncode(tostring(user)).."&password="..textutils.urlEncode(tostring(sha256(pass)))
  57.         local req = http.post(url, query)
  58.         trans = textutils.unserialize(req.readAll())
  59.         assert(type(trans) == "table")
  60.         return trans
  61.     else
  62.         printError("Please specify a username and a password")
  63.     end
  64. end
  65.  
  66. function eco.view_trans(username, password, type)
  67.     if type == "outgoing" then
  68.         if user and pass then
  69.             local query = "cmd=eco_trans_sent&username="..textutils.urlEncode(tostring(user)).."&password="..textutils.urlEncode(tostring(sha256(pass)))
  70.             local req = http.post(url, query)
  71.             trans = textutils.unserialize(req.readAll())
  72.             assert(type(trans) == "table")
  73.             return trans
  74.         else
  75.             printError("Please specify a username and a password")
  76.         end
  77.     elseif type == "incoming" then
  78.         if user and pass then
  79.             local query = "cmd=eco_trans_recv&username="..textutils.urlEncode(tostring(user)).."&password="..textutils.urlEncode(tostring(sha256(pass)))
  80.             local req = http.post(url, query)
  81.             trans = textutils.unserialize(req.readAll())
  82.             assert(type(trans) == "table")
  83.             return trans
  84.         else
  85.             printError("Please specify a username and a password")
  86.         end
  87.     end
  88. end
  89.  
  90. --[[ ##############################################################
  91.                     SHA256 Hashing (Lua)
  92. #################################################################]]
  93.  
  94. local MOD = 2^32
  95. local MODM = MOD-1
  96. local function memoize(f)
  97.     local mt = {}
  98.     local t = setmetatable({}, mt)
  99.     function mt:__index(k)
  100.         local v = f(k)
  101.         t[k] = v
  102.         return v
  103.     end
  104.     return t
  105. end
  106. local function make_bitop_uncached(t, m)
  107.     local function bitop(a, b)
  108.         local res,p = 0,1
  109.         while a ~= 0 and b ~= 0 do
  110.             local am, bm = a % m, b % m
  111.             res = res + t[am][bm] * p
  112.             a = (a - am) / m
  113.             b = (b - bm) / m
  114.             p = p*m
  115.         end
  116.         res = res + (a + b) * p
  117.         return res
  118.     end
  119.     return bitop
  120. end
  121. local function make_bitop(t)
  122.     local op1 = make_bitop_uncached(t,2^1)
  123.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  124.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  125. end
  126. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  127. local function bxor(a, b, c, ...)
  128.     local z = nil
  129.     if b then
  130.         a = a % MOD
  131.         b = b % MOD
  132.         z = bxor1(a, b)
  133.         if c then z = bxor(z, c, ...) end
  134.         return z
  135.     elseif a then return a % MOD
  136.     else return 0 end
  137. end
  138. local function band(a, b, c, ...)
  139.     local z
  140.     if b then
  141.         a = a % MOD
  142.         b = b % MOD
  143.         z = ((a + b) - bxor1(a,b)) / 2
  144.         if c then z = bit32_band(z, c, ...) end
  145.         return z
  146.     elseif a then return a % MOD
  147.     else return MODM end
  148. end
  149. local function bnot(x) return (-1 - x) % MOD end
  150. local function rshift1(a, disp)
  151.     if disp < 0 then return lshift(a,-disp) end
  152.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  153. end
  154. local function rshift(x, disp)
  155.     if disp > 31 or disp < -31 then return 0 end
  156.     return rshift1(x % MOD, disp)
  157. end
  158. local function lshift(a, disp)
  159.     if disp < 0 then return rshift(a,-disp) end
  160.     return (a * 2 ^ disp) % 2 ^ 32
  161. end
  162. local function rrotate(x, disp)
  163.     x = x % MOD
  164.     disp = disp % 32
  165.     local low = band(x, 2 ^ disp - 1)
  166.     return rshift(x, disp) + lshift(low, 32 - disp)
  167. end
  168. local k = {
  169.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  170.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  171.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  172.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  173.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  174.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  175.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  176.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  177.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  178.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  179.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  180.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  181.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  182.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  183.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  184.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  185. }
  186. local function str2hexa(s)
  187.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  188. end
  189. local function num2s(l, n)
  190.     local s = ""
  191.     for i = 1, n do
  192.         local rem = l % 256
  193.         s = string.char(rem) .. s
  194.         l = (l - rem) / 256
  195.     end
  196.     return s
  197. end
  198. local function s232num(s, i)
  199.     local n = 0
  200.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  201.     return n
  202. end
  203. local function preproc(msg, len)
  204.     local extra = 64 - ((len + 9) % 64)
  205.     len = num2s(8 * len, 8)
  206.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  207.     assert(#msg % 64 == 0)
  208.     return msg
  209. end
  210. local function initH256(H)
  211.     H[1] = 0x6a09e667
  212.     H[2] = 0xbb67ae85
  213.     H[3] = 0x3c6ef372
  214.     H[4] = 0xa54ff53a
  215.     H[5] = 0x510e527f
  216.     H[6] = 0x9b05688c
  217.     H[7] = 0x1f83d9ab
  218.     H[8] = 0x5be0cd19
  219.     return H
  220. end
  221. local function digestblock(msg, i, H)
  222.     local w = {}
  223.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  224.     for j = 17, 64 do
  225.         local v = w[j - 15]
  226.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  227.         v = w[j - 2]
  228.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  229.     end
  230.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  231.     for i = 1, 64 do
  232.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  233.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  234.         local t2 = s0 + maj
  235.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  236.         local ch = bxor (band(e, f), band(bnot(e), g))
  237.         local t1 = h + s1 + ch + k[i] + w[i]
  238.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  239.     end
  240.     H[1] = band(H[1] + a)
  241.     H[2] = band(H[2] + b)
  242.     H[3] = band(H[3] + c)
  243.     H[4] = band(H[4] + d)
  244.     H[5] = band(H[5] + e)
  245.     H[6] = band(H[6] + f)
  246.     H[7] = band(H[7] + g)
  247.     H[8] = band(H[8] + h)
  248. end
  249. function sha256(msg)
  250.     msg = preproc(msg, #msg)
  251.     local H = initH256({})
  252.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  253.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  254.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  255. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement