ProgramCrafter

th_inform-v0.0.1

Mar 17th, 2021 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. -- The program that creates the MUID of the current computer.
  2. -- (c) ProgramCrafter
  3.  
  4. local function BitStream()
  5.   local t = {}
  6.  
  7.   local readv = function(start, len)
  8.     local v = 0
  9.     for i = start, start + len - 1 do
  10.       v = v * 2 + (t[i] or 0)
  11.     end
  12.     return v
  13.   end
  14.  
  15.   local bitstream = {
  16.     extend = function(len, value)
  17.       local st = #t
  18.       for i = 1, len do
  19.         t[st + i] = value or 0
  20.       end
  21.     end,
  22.     put = function(...)
  23.       local st = #t
  24.       for i = 1, table.pack(...).n do
  25.         t[st + i] = select(i, ...) or 0
  26.       end
  27.     end,
  28.     hexdigest = function()
  29.       local s = '0123456789ABCDEF'
  30.       local k = {}
  31.       for i = 1, #t, 4 do
  32.         local v = readv(i, 4) + 1
  33.         k[#k + 1] = s:sub(v, v)
  34.       end
  35.       return table.concat(k, '')
  36.     end,
  37.     b64digest = function()
  38.       local s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  39.       local k = {}
  40.       for i = 1, #t, 6 do
  41.         local v = readv(i, 6) + 1
  42.         k[#k + 1] = s:sub(v, v)
  43.       end
  44.       return table.concat(k, '')
  45.     end
  46.   }
  47.  
  48.   return bitstream
  49. end
  50.  
  51. local hex = {
  52.   ['0'] = {0, 0, 0, 0},
  53.   ['1'] = {0, 0, 0, 1},
  54.   ['2'] = {0, 0, 1, 0},
  55.   ['3'] = {0, 0, 1, 1},
  56.   ['4'] = {0, 1, 0, 0},
  57.   ['5'] = {0, 1, 0, 1},
  58.   ['6'] = {0, 1, 1, 0},
  59.   ['7'] = {0, 1, 1, 1},
  60.   ['8'] = {1, 0, 0, 0},
  61.   ['9'] = {1, 0, 0, 1},
  62.   ['A'] = {1, 0, 1, 0},
  63.   ['B'] = {1, 0, 1, 1},
  64.   ['C'] = {1, 1, 0, 0},
  65.   ['D'] = {1, 1, 0, 1},
  66.   ['E'] = {1, 1, 1, 0},
  67.   ['F'] = {1, 1, 1, 1},
  68. }
  69.  
  70. local function getTimeDelta(bitstream)
  71.   error('NotImplementedError')
  72. end
  73. local function getIP(bitstream)
  74.   error('NotImplementedError')
  75. end
  76. local function getOCVersion(bitstream)
  77.   local v = _OSVERSION:sub(10, 10)
  78.   if hex[v] then bitstream.put(table.unpack(hex[v])) end
  79. end
  80. local function getLuVersion(bitstream)
  81.   local v = _VERSION:sub(7, 7)
  82.   if v == '2' then bitstream.put(0)
  83.   else             bitstream.put(1) end
  84. end
  85. local function compAddress(bitstream)
  86.   local com = require 'component'
  87.   local s = com.computer.address:upper()
  88.   for i = 1, #s do
  89.     if hex[s:sub(i, i)] then bitstream.put(table.unpack(hex[s:sub(i, i)])) end
  90.   end
  91. end
  92. local function eeprAddress(bitstream)
  93.   local com = require 'component'
  94.   local s = com.eeprom.address:upper()
  95.   for i = 1, #s do
  96.     if hex[s:sub(i, i)] then bitstream.put(table.unpack(hex[s:sub(i, i)])) end
  97.   end
  98. end
  99. local function eeprData(bitstream)
  100.   local com = require 'component'
  101.   local s = com.eeprom.getData():upper()
  102.   local j = 0
  103.   for i = 1, #s do
  104.     if hex[s:sub(i, i)] then
  105.       bitstream.put(table.unpack(hex[s:sub(i, i)]))
  106.       j = j + 1
  107.       if j == 32 then return end
  108.     end
  109.   end
  110. end
  111. local function getConnection(bitstream)
  112.   bitstream.put(0, 0, 0, 0)
  113. end
  114. local function getAveragePing(bitstream)
  115.   bitstream.extend(7)
  116. end
  117. local function sessionRandom(bitstream)
  118.   for i = 1, 128 do
  119.     bitstream.put((math.random() > 0.5) and 1 or 0)
  120.   end
  121. end
  122. local function sessionTemp(bitstream)
  123.   local fs = require 'filesystem'
  124.   local s = fs.findNode('/tmp').fs.address:upper()
  125.   for i = 1, #s do
  126.     if hex[s:sub(i, i)] then bitstream.put(table.unpack(hex[s:sub(i, i)])) end
  127.   end
  128. end
  129.  
  130. local function safeGetInfo(fnn, fn, bitstream, alt_length)
  131.   local success, reason = pcall(fn, bitstream)
  132.   if success then return end
  133.  
  134.   bitstream.extend(alt_length, 0)
  135. end
  136.  
  137.  
  138. local bs = BitStream()
  139.  
  140. safeGetInfo('TimeDelta', getTimeDelta, bs, 14)
  141. safeGetInfo('IP',        getIP,        bs, 32)
  142. safeGetInfo('OCVersion', getOCVersion, bs, 4)
  143. safeGetInfo('LuVersion', getLuVersion, bs, 1)
  144. safeGetInfo('CompAddress',  compAddress,  bs, 128)
  145. safeGetInfo('EeprAddress',  eeprAddress,  bs, 128)
  146. safeGetInfo('BootAddress',  eeprData,     bs, 128)
  147. safeGetInfo('Connection', getConnection,  bs, 4)
  148. safeGetInfo('ConnPING',   getAveragePing, bs, 7)
  149. safeGetInfo('SessRAND',   sessionRandom,  bs, 128)
  150. safeGetInfo('SessTEMP',     sessionTemp,  bs, 128)
  151.  
  152. _G.digest = bs.b64digest()
Add Comment
Please, Sign In to add comment