Yuri2232

SteamPress v1.1

Nov 27th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. function pressFile(IN)
  2.     file = fs.open("TestFile","r")
  3.     RGW = file.readAll()
  4.     file.close()
  5.     return RGW
  6. end
  7.  
  8. function pressFolder(IN,en)
  9.     PressTable = {}
  10.     PressTable.Name = IN
  11.     PressTable.files = {}
  12.     --Checks
  13.     if IN == nil then
  14.         IN = "PressedFolder"
  15.     end
  16.     if en then
  17.         GenKey(IN..".key")
  18.         PressTable.PRESSKEY = IN..".key"
  19.     else
  20.  
  21.     end
  22.     if fs.exists(IN) and fs.isDir(IN) then
  23.         print("pressing folder")
  24.         local MainDir = fs.list(IN)
  25.         local curDir = IN.."/"
  26.         local curItem = ""
  27.         for i = 1,#MainDir do
  28.             curItem = curDir..MainDir[i]
  29.             print("pressing "..curItem)
  30.             if fs.isDir(curItem) then
  31.                 PressTable.files[MainDir[i]] = {}
  32.                 local TableLoc = PressTable.files[MainDir[i]]
  33.                 pressSubFolder(TableLoc,curItem,en)
  34.             else
  35.                 file = fs.open(curItem,"r")
  36.                 if en then
  37.                     PressTable.files[MainDir[i]] = encrypt(file.readAll(),curItem,IN..".key")
  38.                 else
  39.                     PressTable.files[MainDir[i]] = file.readAll()
  40.                 end
  41.                 file.close()
  42.             end
  43.             sleep(.1)
  44.             term.clear()
  45.             term.setCursorPos(1,1)
  46.         end
  47.         file = fs.open(IN..".pressed","w")
  48.         file.write(textutils.serialize(PressTable))
  49.         file.close()
  50.         print("Pressed to "..IN..".pressed")
  51.     else
  52.         print("Can not press folder")
  53.         return
  54.     end
  55. end
  56.  
  57. function pressSubFolder(TableLoc,folder,en)
  58.     local MainDir = fs.list(folder.."/")
  59.     local curDir = folder.."/"
  60.     local curItem = ""
  61.     print("pressing subfolder "..folder)
  62.     for i = 1,#MainDir do
  63.         curItem = curDir..MainDir[i]
  64.         print("pressing "..curItem)
  65.         if fs.isDir(curItem) then
  66.             TableLoc[MainDir[i]] = {}
  67.             pressSubFolder(TableLoc[MainDir[i]],curItem,en)
  68.         else
  69.             file = fs.open(curItem,"r")
  70.             if en then
  71.                 TableLoc[MainDir[i]] = encrypt(file.readAll(),curItem,PressTable.PRESSKEY)
  72.             else
  73.                 TableLoc[MainDir[i]] = file.readAll()
  74.             end
  75.             file.close()
  76.         end
  77.         sleep(.1)
  78.         term.clear()
  79.         term.setCursorPos(1,1)
  80.     end
  81.     print("subfolder "..folder.." done")
  82.     return
  83. end
  84.  
  85. function GenKey(OUT)
  86.     print("generating key...")
  87.     KeyTable = {}
  88.     for i = 1,127 do
  89.         KeyTable[string.byte(string.char(i))] = math.random(1000,9999)
  90.         print(string.char(i)..":"..KeyTable[string.byte(string.char(i))])
  91.         sleep(.001)
  92.     end
  93.     print("Saving key to "..OUT)
  94.     kf = fs.open(OUT,"w")
  95.     kf.write(textutils.serialize(KeyTable))
  96.     kf.close()
  97.     print("Done.")
  98. end
  99.  
  100. function unpackFolder(TABLE,key)
  101.     if fs.exists(TABLE) and not fs.isDir(TABLE) then
  102.         file = fs.open(TABLE,"r")
  103.         PressTable = textutils.unserialize(file.readAll())
  104.         file.close()
  105.         if PressTable.PRESSKEY ~= nil and key == nil then
  106.             print("Table is locked")
  107.             return
  108.         else
  109.             if key ~= nil then
  110.                 en = true
  111.                 local KeyFile = fs.open(key,"r")
  112.                 key = textutils.unserialize(KeyFile.readAll())
  113.                 KeyFile.close()
  114.             else
  115.                 en = false
  116.             end
  117.             print("unpacking folder")
  118.             fs.makeDir(PressTable.Name)
  119.             print("Created folder "..PressTable.Name)
  120.             local MainDir = PressTable.Name.."/"
  121.             local curDir = MainDir
  122.             local curItem = ""
  123.             for name in pairs(PressTable.files) do
  124.                 curItem = curDir..name
  125.                 print("unpacking "..type(PressTable.files[name]).." "..curItem)
  126.                 if type(PressTable.files[name]) == "table" then
  127.                     fs.makeDir(curItem)
  128.                     unpackSubFolder(PressTable.files[name],curItem,key,en)
  129.                 else
  130.                     file = fs.open(curItem,"w")
  131.                     if en then
  132.                         file.write(decrypt(PressTable.files[name],key))
  133.                     else
  134.                         file.write(PressTable.files[name])
  135.                     end
  136.                     file.close()
  137.                 end
  138.                 sleep(.1)
  139.                 term.clear()
  140.                 term.setCursorPos(1,1)
  141.             end
  142.             print("Done unpacking")
  143.             return
  144.         end
  145.     end
  146. end
  147.  
  148. function unpackSubFolder(TABLE,loc,key,en)
  149.     local MainDir = PressTable.Name.."/"
  150.     local curDir = loc
  151.     local curItem = ""
  152.     for name in pairs(TABLE) do
  153.         curItem = curDir.."/"..name
  154.         print("unpacking subfolder "..type(TABLE[name]).." "..curItem)
  155.         if type(TABLE[name]) == "table" then
  156.             fs.makeDir(curItem)
  157.             unpackSubFolder(TABLE[name],curItem,key,en)
  158.         else
  159.             file = fs.open(curItem,"w")
  160.             if en then
  161.                 file.write(decrypt(TABLE[name],key))
  162.             else
  163.                 file.write(TABLE[name])
  164.             end
  165.             file.close()
  166.         end
  167.         sleep(.1)
  168.         term.clear()
  169.         term.setCursorPos(1,1)
  170.     end
  171.     print("subfolder "..loc.." done")
  172. end
  173.  
  174. function encrypt(S,NAME,key)
  175.     local KeyFile = fs.open(key,"r")
  176.     local key = textutils.unserialize(KeyFile.readAll())
  177.     KeyFile.close()
  178.     local encryptedString = ""
  179.     curPer = 0
  180.     for i = 1,string.len(S) do
  181.         curPer = math.floor((i/string.len(S))*100)
  182.         if key[string.byte(string.sub(S,i,i))] then
  183.             encryptedString = encryptedString..key[string.byte(string.sub(S,i,i))]
  184.         else
  185.             print("unknown code found! "..string.byte(string.sub(S,i,i)))
  186.             sleep(1)
  187.         end
  188.         sleep(.00000000001)
  189.         term.clear()
  190.         term.setCursorPos(1,1)
  191.         write("encrypting "..NAME.." "..curPer.."%")
  192.         W,H = term.getSize()
  193.         term.setCursorPos(1,H)
  194.         if term.isColor then
  195.             term.setBackgroundColor(colors.green)
  196.             term.setTextColor(colors.green)
  197.         end
  198.         for q = 1,(curPer/100)*W do
  199.             write("|")
  200.         end
  201.         term.setBackgroundColor(colors.black)
  202.         term.setTextColor(colors.white)
  203.     end
  204.     return encryptedString
  205. end
  206.  
  207. function decrypt(encryptedString,key)
  208.     local code = ""
  209.     local S = ""
  210.     for i = 1,string.len(encryptedString) do
  211.         code = code..string.sub(encryptedString,i,i)
  212.         if string.len(code) == 4 then
  213.             for keys in pairs(key) do
  214.                 if tostring(key[keys]) == tostring(code) then
  215.                     S = S..string.char(keys)
  216.                 end
  217.             end
  218.             code = ""
  219.             if string.len(encryptedString) > 500 then
  220.                 sleep(.0000000000001)
  221.             end
  222.         end
  223.     end
  224.     return S
  225. end
  226.  
  227. args = {...}
  228. term.clear()
  229. term.setCursorPos(1,1)
  230. if args[1] == "press" then
  231.     pressFolder(args[2],args[3])
  232. elseif args[1] == "unpack" then
  233.     unpackFolder(args[2],args[3])
  234. end
Advertisement
Add Comment
Please, Sign In to add comment