--@action Check a download's checksum --@version 1.0.0 --@author TheFile.Ninja --@id check-checksum --@description Compares the file under the cursor with the checksum on the clipboard: MD5, SHA-1, SHA-256 or SHA-512. --@tags files, security --@requires none -- Copy the checksum from the download page, put the cursor on the file and -- run this. The checksum's length says which algorithm it is. With no -- checksum on the clipboard, the file's SHA-256 is shown and copied. local ALGORITHMS = { [32] = { "md5", "MD5" }, [40] = { "sha1", "SHA-1" }, [64] = { "sha256", "SHA-256" }, [128] = { "sha512", "SHA-512" }, } local f = pane.active():focused() if not f or not f.is_file then ui.message("Put the cursor on the file to check.", "Checksum") return end -- The clipboard may hold more than the checksum ("SHA256: 9f86… setup.exe"): -- the first run of hex digits with a checksum's length is the one. local expected, algorithm for hex in (clipboard.get_text() or ""):gmatch("%x+") do if ALGORITHMS[#hex] then expected, algorithm = hex:lower(), ALGORITHMS[#hex] break end end local prog = ui.progress("Checksum of " .. f.name) prog:update(-1) local ok, actual = pcall(hash.file, f, algorithm and algorithm[1] or "sha256") prog:finish() if not ok then ui.message(tostring(actual), "Checksum", "error") return end if not expected then clipboard.set_text(actual) ui.message(f.name .. "\n\nSHA-256 " .. actual .. "\n\nNo checksum on the clipboard to compare with: this one is copied.", "Checksum") elseif actual == expected then ui.message(f.name .. "\n\nThe " .. algorithm[2] .. " matches:\n" .. actual, "Checksum matches") else ui.message(f.name .. "\n\nThe " .. algorithm[2] .. " does NOT match.\n\nExpected " .. expected .. "\nThe file " .. actual, "Checksum differs", "error") end