Advertisement
Sirshark10

BigAPI

Dec 4th, 2017
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.69 KB | None | 0 0
  1. --[[
  2. +---------------------------------+
  3. | |
  4. | CLOUD UTIL |
  5. | Various Useful |
  6. | Snippets |
  7. | |
  8. +---------------------------------+
  9.  
  10. This potentially massive API file will contain
  11. a variety of different API's that I have created
  12. over the few years that I've worked with CC and Lua.
  13.  
  14.  
  15.  
  16. --]]
  17.  
  18.  
  19. --[[
  20.  
  21. Networking
  22. |Includes:
  23. |Libspoofer : Spoofs ID's for Rednet and formats the message properly over modem
  24. |Libpcap : Captures all Rednet traffic
  25. --]]
  26.  
  27. _G.libspoofer = {}
  28. _G.libpcap = {}
  29.  
  30. -------------------------------------------
  31. local modem = peripheral.find("modem")
  32.  
  33. libspoofer.send = function(msg, to, from)
  34. id = math.random(1, 800000)
  35. local message = {
  36. nMessageID = id,
  37. nRecipient = to,
  38. message = msg,
  39. sProtocol = nil,
  40. }
  41. modem.transmit(to, from, message)
  42. end
  43. -------------------------------------------
  44. -------------------------------------------
  45.  
  46. local event, side, sChannel, rChannel, data, dist
  47.  
  48. local modem = peripheral.find("modem")
  49.  
  50.  
  51. _G.libpcap.cap = function()
  52.  
  53. modem.close(os.getComputerID())
  54. modem.close(65535)
  55. modem.open(65533)
  56. modem.open(65534)
  57.  
  58. event, side, sChannel, rChannel, data, dist = os.pullEvent("modem_message")
  59. local table = {
  60. event = event,
  61. side = side,
  62. recipient = sChannel,
  63. sender = rChannel,
  64. data = data,
  65. distance = dist}
  66. modem.close(65533)
  67. modem.close(65534)
  68. modem.open(65535)
  69. modem.open(os.getComputerID())
  70. return table
  71. end
  72.  
  73. -------------------------------------------
  74. -------------------------------------------
  75.  
  76.  
  77. --[[
  78.  
  79. Miscellaneous
  80. |Includes:
  81. |Explode/Uexplode : Splits strings on characters (returns table of all split points) and then allows for recombining with specified character(s)
  82. |klua : API to communicate with the krist server (also has a built in sha256 function for hashing) | WARNING: BIG API INCOMING
  83. --]]
  84.  
  85. _G.string.explode = function(str,char)
  86. local tbl = {}
  87. if char == "" then
  88. for i=1, str:len() do
  89. table.insert(tbl,str:sub(i,i))
  90. end
  91. return tbl
  92. elseif not str:find(char) then
  93. return {str}
  94. else
  95. local _,amt = str:gsub(char," ")
  96. for i=1, amt+1 do
  97. if str:find(char) then
  98. local x,y = str:find(char)
  99. local rep = str:sub(1,x-1)
  100. table.insert(tbl,rep)
  101. str = str:sub(y+1, str:len())
  102. else
  103. table.insert(tbl,str)
  104. end
  105. end
  106. end
  107. return tbl
  108. end
  109.  
  110.  
  111. _G.string.uexplode = function(tbl,char)
  112. local ret = ""
  113. for k,v in pairs(tbl) do
  114. if k == #tbl then
  115. ret = ret..v
  116. else
  117. ret = ret..v..char
  118. end
  119. end
  120. return ret
  121. end
  122.  
  123.  
  124.  
  125.  
  126.  
  127. _G.klua = {}
  128. --Sha256 stuff here
  129. local MOD = 2^32
  130. local MODM = MOD-1
  131.  
  132. local function memoize(f)
  133. local mt = {}
  134. local t = setmetatable({}, mt)
  135. function mt:__index(k)
  136. local v = f(k)
  137. t[k] = v
  138. return v
  139. end
  140. return t
  141. end
  142.  
  143. local function make_bitop_uncached(t, m)
  144. local function bitop(a, b)
  145. local res,p = 0,1
  146. while a ~= 0 and b ~= 0 do
  147. local am, bm = a % m, b % m
  148. res = res + t[am][bm] * p
  149. a = (a - am) / m
  150. b = (b - bm) / m
  151. p = p*m
  152. end
  153. res = res + (a + b) * p
  154. return res
  155. end
  156. return bitop
  157. end
  158.  
  159. local function make_bitop(t)
  160. local op1 = make_bitop_uncached(t,2^1)
  161. local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  162. return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  163. end
  164.  
  165. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  166.  
  167. local function bxor(a, b, c, ...)
  168. local z = nil
  169. if b then
  170. a = a % MOD
  171. b = b % MOD
  172. z = bxor1(a, b)
  173. if c then z = bxor(z, c, ...) end
  174. return z
  175. elseif a then return a % MOD
  176. else return 0 end
  177. end
  178.  
  179. local function band(a, b, c, ...)
  180. local z
  181. if b then
  182. a = a % MOD
  183. b = b % MOD
  184. z = ((a + b) - bxor1(a,b)) / 2
  185. if c then z = bit32_band(z, c, ...) end
  186. return z
  187. elseif a then return a % MOD
  188. else return MODM end
  189. end
  190.  
  191. local function bnot(x) return (-1 - x) % MOD end
  192.  
  193. local function rshift1(a, disp)
  194. if disp < 0 then return lshift(a,-disp) end
  195. return math.floor(a % 2 ^ 32 / 2 ^ disp)
  196. end
  197.  
  198. local function rshift(x, disp)
  199. if disp > 31 or disp < -31 then return 0 end
  200. return rshift1(x % MOD, disp)
  201. end
  202.  
  203. local function lshift(a, disp)
  204. if disp < 0 then return rshift(a,-disp) end
  205. return (a * 2 ^ disp) % 2 ^ 32
  206. end
  207.  
  208. local function rrotate(x, disp)
  209. x = x % MOD
  210. disp = disp % 32
  211. local low = band(x, 2 ^ disp - 1)
  212. return rshift(x, disp) + lshift(low, 32 - disp)
  213. end
  214.  
  215. local k = {
  216. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  217. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  218. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  219. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  220. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  221. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  222. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  223. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  224. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  225. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  226. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  227. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  228. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  229. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  230. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  231. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  232. }
  233.  
  234. local function str2hexa(s)
  235. return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  236. end
  237.  
  238. local function num2s(l, n)
  239. local s = ""
  240. for i = 1, n do
  241. local rem = l % 256
  242. s = string.char(rem) .. s
  243. l = (l - rem) / 256
  244. end
  245. return s
  246. end
  247.  
  248. local function s232num(s, i)
  249. local n = 0
  250. for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  251. return n
  252. end
  253.  
  254. local function preproc(msg, len)
  255. local extra = 64 - ((len + 9) % 64)
  256. len = num2s(8 * len, 8)
  257. msg = msg .. "\128" .. string.rep("\0", extra) .. len
  258. assert(#msg % 64 == 0)
  259. return msg
  260. end
  261.  
  262. local function initH256(H)
  263. H[1] = 0x6a09e667
  264. H[2] = 0xbb67ae85
  265. H[3] = 0x3c6ef372
  266. H[4] = 0xa54ff53a
  267. H[5] = 0x510e527f
  268. H[6] = 0x9b05688c
  269. H[7] = 0x1f83d9ab
  270. H[8] = 0x5be0cd19
  271. return H
  272. end
  273.  
  274. local function digestblock(msg, i, H)
  275. local w = {}
  276. for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  277. for j = 17, 64 do
  278. local v = w[j - 15]
  279. local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  280. v = w[j - 2]
  281. w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  282. end
  283.  
  284. 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]
  285. for i = 1, 64 do
  286. local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  287. local maj = bxor(band(a, b), band(a, c), band(b, c))
  288. local t2 = s0 + maj
  289. local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  290. local ch = bxor (band(e, f), band(bnot(e), g))
  291. local t1 = h + s1 + ch + k[i] + w[i]
  292. h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  293. end
  294.  
  295. H[1] = band(H[1] + a)
  296. H[2] = band(H[2] + b)
  297. H[3] = band(H[3] + c)
  298. H[4] = band(H[4] + d)
  299. H[5] = band(H[5] + e)
  300. H[6] = band(H[6] + f)
  301. H[7] = band(H[7] + g)
  302. H[8] = band(H[8] + h)
  303. end
  304.  
  305. _G.klua.sha256 = function(msg)
  306. msg = preproc(msg, #msg)
  307. local H = initH256({})
  308. for i = 1, #msg, 64 do digestblock(msg, i, H) end
  309. return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  310. num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  311. end
  312.  
  313. local function makeaddressbyte(j)
  314. if j <= 6 then return "0"
  315. elseif j <= 13 then return "1"
  316. elseif j <= 20 then return "2"
  317. elseif j <= 27 then return "3"
  318. elseif j <= 34 then return "4"
  319. elseif j <= 41 then return "5"
  320. elseif j <= 48 then return "6"
  321. elseif j <= 55 then return "7"
  322. elseif j <= 62 then return "8"
  323. elseif j <= 69 then return "9"
  324. elseif j <= 76 then return "a"
  325. elseif j <= 83 then return "b"
  326. elseif j <= 90 then return "c"
  327. elseif j <= 97 then return "d"
  328. elseif j <= 104 then return "e"
  329. elseif j <= 111 then return "f"
  330. elseif j <= 118 then return "g"
  331. elseif j <= 125 then return "h"
  332. elseif j <= 132 then return "i"
  333. elseif j <= 139 then return "j"
  334. elseif j <= 146 then return "k"
  335. elseif j <= 153 then return "l"
  336. elseif j <= 160 then return "m"
  337. elseif j <= 167 then return "n"
  338. elseif j <= 174 then return "o"
  339. elseif j <= 181 then return "p"
  340. elseif j <= 188 then return "q"
  341. elseif j <= 195 then return "r"
  342. elseif j <= 202 then return "s"
  343. elseif j <= 209 then return "t"
  344. elseif j <= 216 then return "u"
  345. elseif j <= 223 then return "v"
  346. elseif j <= 230 then return "w"
  347. elseif j <= 237 then return "x"
  348. elseif j <= 244 then return "y"
  349. elseif j <= 251 then return "z"
  350. else return "e"
  351. end
  352. end
  353.  
  354. _G.klua.makev2address = function(key)
  355. local protein = {}
  356. local stick = klua.sha256(klua.sha256(key))
  357. local n = 0
  358. local link = 0
  359. local v2 = "k"
  360. repeat
  361. if n < 9 then protein[n] = string.sub(stick,0,2)
  362. stick = sha256(sha256(stick)) end
  363. n = n + 1
  364. until n == 9
  365. n = 0
  366. repeat
  367. link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
  368. if string.len(protein[link]) ~= 0 then
  369. v2 = v2 .. makeaddressbyte(tonumber(protein[link],16))
  370. protein[link] = ''
  371. n = n + 1
  372. else
  373. stick = sha256(stick)
  374. end
  375. until n == 9
  376. return v2
  377. end
  378.  
  379.  
  380.  
  381. --Rest of the code
  382.  
  383. --Creates a privatekey out of the password
  384. _G.klua.ToPriv = function(input)
  385. return sha256("KRISTWALLET"..input).."-000"
  386. end
  387.  
  388. site = "http://krist.ceriat.net/" --The site for all requests
  389.  
  390. --Get info on an address
  391. _G.klua.getInfo = function(addr)
  392. return http.get(site.."addresses/"..addr).readAll()
  393. end
  394.  
  395. --Send Funds
  396. _G.klua.sendFunds = function(InP,to,amount)
  397. return http.post(site.."transactions/","privatekey="..ToPriv(InP).."&to="..to.."&amount="..amount).readAll()
  398. end
  399.  
  400. --Lookup a NAME.kst
  401. _G.klua.nameLook = function(NAME)
  402. return http.get(site.."names/"..NAME).readAll()
  403. end
  404.  
  405. --Register NAME.kst to given password's private key address.
  406. _G.klua.nameReg = function(NAME,InP)
  407. return http.post(site.."names/"..NAME.."/","privatekey="..ToPriv(InP)).readAll()
  408. end
  409.  
  410. --List latest transactions
  411. _G.klua.lateTrans = function()
  412. return http.get(site.."transactions/latest").readAll()
  413. end
  414.  
  415. --Gets the Krist in circulation
  416. _G.klua.getCirc = function()
  417. return http.get(site.."supply").readAll()
  418. end
  419.  
  420. --Gets the Message of the day
  421. _G.klua.MOTD = function()
  422. return http.get(site.."motd").readAll()
  423. end
  424.  
  425. --Get the current work
  426. _G.klua.work = function()
  427. return http.get(site.."work").readAll()
  428. end
  429.  
  430. --Authenticate an address with the server via the privatekey
  431. _G.klua.authAddr = function(privateKey)
  432. return http.post(site.."login","privatekey="..privateKey).readAll()
  433. end
  434.  
  435. --List all names
  436. _G.klua.lNames = function()
  437. return http.get(site.."names").readAll()
  438. end
  439.  
  440. _G.klua.myanmar = function()
  441. return "Marisa stole the entire country of Myanmar"
  442. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement