Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ui = require "ui"
- require "canvas"
- local crypto = require "crypto"
- local function isdirectory(path)
- return xpcall(function()
- sys.Directory(path)
- return true
- end, function() return false end)
- end
- local function encode(filename,key) -- algo: aes256
- if isdirectory(filename) then
- for entry in each(sys.Directory(filename)) do
- encode(filename.."\\"..entry.name,key)
- end
- return
- end
- local cipher = crypto.Cipher("aes256", key)
- local f = sys.File(filename)
- cipher:encrypt(f:open("read","binary"):read())
- f:close()
- f:open("write","binary")
- f:write(cipher.encrypted)
- f:close()
- end
- local function decode(filename,key)
- if isdirectory(filename) then
- for entry in each(sys.Directory(filename)) do
- decode(filename.."\\"..entry.name,key)
- end
- return
- end
- local cipher = crypto.Cipher("aes256", key)
- local f = sys.File(filename)
- cipher:decrypt(f:open("read","binary"):read())
- f:close()
- f:open("write","binary")
- f:write(cipher.decrypted)
- f:close()
- end
- local win = ui.Window("Decryptor","dialog",600,600)
- local directory_l = {}
- local directory
- for i in each(sys.Directory(".")) do
- if isdirectory(i.fullpath) then
- table.insert(directory_l,i)
- end
- end
- if #directory_l==1 then
- directory = directory_l[1]
- else
- directory = ui.dirdialog("Choose directory to decode:")
- end
- if directory == nil then os.exit() end
- directory = directory.fullpath
- win:status("Press Enter to use the password. Directory: "..directory)
- local canvas = ui.Canvas(win,0,0,win.width,win.height)
- local patterns = {} -- each pattern is a table of unit points
- local h_scale,v_scale = 7,14/math.sqrt(3)
- local hex_r = h_scale
- local scale = 10
- local top_y,left_x = 0,0
- local m_x,m_y = 0,0
- local is_drawing = false
- local draw_color = 0xffffffff
- local hash = nil
- local function round(n)
- return math.floor(n+0.5)
- end
- local function snap_to_grid_unit(x,y) -- return two values in grid units
- local closest_y = round((y-top_y)/v_scale/scale)
- local closest_x = round(((x-left_x+(closest_y&1)*h_scale)/h_scale)/scale) -- garbage
- return closest_x,closest_y
- end
- local function unit_to_grid(x,y) -- change units to pixels
- return (x*h_scale-(y&1)*h_scale/2)*scale+left_x, y*v_scale*scale+top_y
- end
- local function snap_to_grid(x,y) -- return two values in pixels
- ux,uy = snap_to_grid_unit(x,y)
- return unit_to_grid(ux,uy)
- end
- local function getdirection(p1,p2)
- local x1,y1 = unit_to_grid(table.unpack(p1))
- local x2,y2 = unit_to_grid(table.unpack(p2))
- if x1<x2 and y1==y2 then return 1 end
- if x1<x2 and y1<y2 then return 2 end
- if x1>x2 and y1<y2 then return 3 end
- if x1>x2 and y1==y2 then return 4 end
- if x1>x2 and y1>y2 then return 5 end
- if x1<x2 and y1>y2 then return 6 end
- if x1==x2 and y1>y2 then return 7 end
- if x1==x2 and y1<y2 then return 8 end
- end
- local function points_to_hash()
- local bytes = 0
- for i=1,#patterns do
- bytes=bytes+#patterns[i]+1
- end
- local buffer = sys.Buffer(bytes)
- local bufferptr = 1
- for i=1,#patterns do
- for j=1,#patterns[i]-1 do
- n = getdirection(patterns[i][j],patterns[i][j+1])
- buffer[bufferptr] = n
- bufferptr=bufferptr+1
- end
- buffer[bufferptr] = 0xff
- bufferptr=bufferptr+1
- end
- return crypto.hash("sha256",buffer)
- end
- function win:onResize()
- canvas.width,canvas.height = win.width,win.height
- end
- local function points_equal(p1,p2)
- return p1[1]==p2[1] and p1[2]==p2[2]
- end
- function canvas:onHover(x,y)
- m_x,m_y = x/ui.dpi,y/ui.dpi
- if not is_drawing then return end
- local point = {snap_to_grid_unit(m_x,m_y)}
- cl_x,cl_y = unit_to_grid(table.unpack(point))
- local last_pattern = patterns[#patterns]
- if last_pattern==nil then return end
- if math.abs(cl_x-m_x)<h_scale*scale/5 and math.abs(cl_y-m_y)<v_scale*scale/5 then
- if #last_pattern>1 and points_equal(point,last_pattern[#last_pattern-1]) then
- table.remove(last_pattern)
- elseif not points_equal(point,last_pattern[#last_pattern]) then
- last_pattern[#last_pattern+1] = {snap_to_grid_unit(m_x,m_y)}
- end
- end
- end
- function canvas:onMouseDown(x,y)
- patterns[#patterns+1] = {{snap_to_grid_unit(x/ui.dpi,y/ui.dpi)}} -- add a 2d array
- is_drawing = true
- end
- function canvas:onMouseUp(x,y)
- if not is_drawing then return end
- is_drawing = false
- local last = patterns[#patterns]
- if last == nil then return end
- if #last == 1 then patterns[#patterns]=nil end
- end
- function canvas:onContext()
- table.remove(patterns)
- is_drawing = false
- end
- function canvas:onPaint()
- canvas:begin()
- canvas:clear(0)
- for i=1,canvas.width/h_scale/scale do
- for j=1,canvas.height/v_scale/scale do
- canvas:fillcircle(left_x+i*h_scale*scale - h_scale*scale/2*(j&1),top_y+j*v_scale*scale,3,0x337788ff)
- end
- end
- for i=1,#patterns do
- for j=1,#patterns[i]-1 do
- local p1_x,p1_y = unit_to_grid(table.unpack(patterns[i][j]))
- local p2_x,p2_y = unit_to_grid(table.unpack(patterns[i][j+1]))
- canvas:line(p1_x,p1_y,p2_x,p2_y,draw_color,3)
- end
- end
- if is_drawing then
- local last_pattern = patterns[#patterns]
- local p_x,p_y = unit_to_grid(table.unpack(last_pattern[#last_pattern]))
- canvas:line(p_x,p_y,m_x,m_y,draw_color,3)
- end
- canvas:flip()
- end
- function win:onKey(k)
- if k=="W" then top_y=top_y+scale
- elseif k=="S" then top_y=top_y-scale
- elseif k=="A" then left_x=left_x+scale
- elseif k=="D" then left_x=left_x-scale
- elseif k=="VK_RETURN" then
- if #patterns == 0 then return end
- function canvas:onHover() end
- function canvas:onContext() end
- function canvas:onMouseDown() end
- function canvas:onMouseUp() end
- draw_color = 0xff9900ff
- hash = points_to_hash()
- decode(directory,hash)
- end
- end
- function win:onClose()
- if hash == nil then return end
- if not (sys.File(directory).exists or sys.Directory(directory).exists) then
- ui.warn("The file or folder no longer exists. Nothing will be reencoded.")
- return
- end
- encode(directory,hash)
- end
- win:show()
- ui.task:wait()
Advertisement
Add Comment
Please, Sign In to add comment