osmarks

dcopy

Jun 8th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local privkey_path = ".potatos_dsk"
  2. if not fs.exists(privkey_path) then
  3.     error("Please save the potatOS disk signing key (or alternate table-format ECC signing key) to " .. privkey_path .. " to use this program.")
  4. end
  5.  
  6. local ecc = require "./ecc"
  7. local t = ecc "ecc"
  8.  
  9. local thing, UUID_override = ...
  10. local function fread(thing)
  11.     local f = fs.open(thing, "r")
  12.     local text = f.readAll()
  13.     f.close()
  14.     return text
  15. end
  16.  
  17. local function hexize(key)
  18.     local out = ""
  19.     for _, v in pairs(key) do
  20.         out = out .. string.format("%.2x", v)
  21.     end
  22.     return out
  23. end
  24.  
  25. local function unhexize(key)
  26.     local out = {}
  27.     for i = 1, #key, 2 do
  28.         local pair = key:sub(i, i + 1)
  29.         table.insert(out, tonumber(pair, 16))
  30.     end
  31.     return out
  32. end
  33.  
  34. local function fwrite(fname, text)
  35.     local f = fs.open(fname, "w")
  36.     f.write(text)
  37.     f.close()
  38. end
  39.  
  40. local pkey = unhexize(fread(privkey_path))
  41.  
  42. local _, side = os.pullEvent "disk"
  43. local mp = disk.getMountPath(side)
  44. local path = fs.combine(mp, "startup")
  45. local sig_path = fs.combine(mp, "signature")
  46.  
  47. local UUID_path = fs.combine(mp, "UUID")
  48. local UUID = ""
  49.  
  50. if UUID_override then UUID = UUID_override
  51. else
  52.     if fs.exists(UUID_path) then UUID = fread(UUID_path)
  53.     else
  54.         for i = 1, 10 do
  55.             UUID = UUID .. string.char(math.random(97, 122))
  56.         end
  57.     end
  58. end
  59.  
  60. print("UUID:", UUID)
  61.  
  62. disk.setLabel(side, thing)
  63. local text = fread(thing):gsub("@UUID@", UUID)
  64.  
  65. fwrite(path, text)
  66. print "Written data."
  67. fwrite(sig_path, hexize(t.sign(
  68.     pkey,
  69.     text
  70. )))
  71. fwrite(UUID_path, tostring(UUID))
  72. print "Written signature."
  73. print("Disk ID:", disk.getID(side))
Add Comment
Please, Sign In to add comment