Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce hashing without salt #8

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 63 additions & 12 deletions lua/hacker-helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,44 +233,56 @@ end, { noremap = true, silent = true, desc = "Octal Decode" })

-- MD5 Hash
vim.keymap.set("v", M.config.prefix .. M.config.keys.hash_prefix .. M.config.keys.hash_md5, function()
selection_util.transform_selection(function(text)

selection_util.hash_selection(function(text)
return M.hash_text(text, "md5")
end, "hash", "md5")
end) -- Passing "hash" as the mode and "md5" as the encoding type

end, { noremap = true, silent = true, desc = "MD5 Hash" })

-- SHA-1 Hash
vim.keymap.set("v", M.config.prefix .. M.config.keys.hash_prefix .. M.config.keys.hash_sha1, function()
selection_util.transform_selection(function(text)

selection_util.hash_selection(function(text)
return M.hash_text(text, "sha1")
end, "hash", "sha1")
end) -- Mode: "hash", Encoding: "sha1"

end, { noremap = true, silent = true, desc = "SHA-1 Hash" })

-- SHA-256 Hash
vim.keymap.set("v", M.config.prefix .. M.config.keys.hash_prefix .. M.config.keys.hash_sha256, function()
selection_util.transform_selection(function(text)

selection_util.hash_selection(function(text)
return M.hash_text(text, "sha256")
end, "hash", "sha256")
end) -- Mode: "hash", Encoding: "sha256"

end, { noremap = true, silent = true, desc = "SHA-256 Hash" })

-- CRC32 Hash
vim.keymap.set("v", M.config.prefix .. M.config.keys.hash_prefix .. M.config.keys.hash_crc32, function()
selection_util.transform_selection(function(text)

selection_util.hash_selection(function(text)
return M.hash_text(text, "crc32")
end, "hash", "crc32")
end) -- Mode: "hash", Encoding: "crc32"

end, { noremap = true, silent = true, desc = "CRC32 Hash" })

-- Bcrypt Hash
vim.keymap.set("v", M.config.prefix .. M.config.keys.hash_prefix .. M.config.keys.hash_bcrypt, function()
selection_util.transform_selection(function(text)

selection_util.hash_selection(function(text)
return M.hash_text(text, "bcrypt")
end, "hash", "bcrypt")
end) -- Mode: "hash", Encoding: "bcrypt"

end, { noremap = true, silent = true, desc = "Bcrypt Hash" })

-- Scrypt Hash
vim.keymap.set("v", M.config.prefix .. M.config.keys.hash_prefix .. M.config.keys.hash_scrypt, function()
selection_util.transform_selection(function(text)

selection_util.hash_selection(function(text)
return M.hash_text(text, "scrypt")
end, "hash", "scrypt")
end) -- Mode: "hash", Encoding: "scrypt"

end, { noremap = true, silent = true, desc = "Scrypt Hash" })

-- Function to handle encoding/decoding based on selection
Expand Down Expand Up @@ -477,6 +489,45 @@ M.hash_text = function(text, algorithm)
end
end

M.hash_text = function(text, algorithm)
local python_cmd = ""

-- Define Python commands for each hashing algorithm
if algorithm == "md5" then
python_cmd = string.format("python3 -c 'import hashlib; print(hashlib.md5(\"%s\".encode()).hexdigest())'", text)
elseif algorithm == "sha1" then
python_cmd = string.format("python3 -c 'import hashlib; print(hashlib.sha1(\"%s\".encode()).hexdigest())'", text)
elseif algorithm == "sha256" then
python_cmd = string.format("python3 -c 'import hashlib; print(hashlib.sha256(\"%s\".encode()).hexdigest())'", text)
elseif algorithm == "bcrypt" then
python_cmd = string.format(
"python3 -c 'import bcrypt; print(bcrypt.hashpw(\"%s\".encode(), bcrypt.gensalt()).decode())'",
text
)
elseif algorithm == "crc32" then
python_cmd =
string.format('python3 -c \'import binascii; print(format(binascii.crc32(b"%s") & 0xffffffff, "08x"))\'', text)
elseif algorithm == "scrypt" then
python_cmd = string.format(
'python3 -c \'import hashlib; print(hashlib.scrypt("%s".encode(), salt=b"", n=16384, r=8, p=1, dklen=64).hex())\'',
text
)
else
vim.notify("Hacker Helper: unsupported algorithm " .. algorithm, vim.log.levels.ERROR)
end

-- Execute the Python command and capture the output
local handle = io.popen(python_cmd)
if handle then
local result = handle:read("*a")
handle:close()
-- Remove trailing newlines from the result
return result:gsub("%s+", "")
else
vim.notify("Hacker Helper: Python dependencies for hashing are missing", vim.log.levels.ERROR)
end
end

M.hello = function()
return module.my_first_function(M.config.opt)
end
Expand Down
58 changes: 58 additions & 0 deletions lua/hacker-helper/selection_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,63 @@ M.transform_selection = function(transform_func, mode, encoding_type)
-- Reset the cursor position to prevent jumping to another line
vim.cmd("normal! gv") -- Ensure the visual selection is active
end
-- Utility function to capture the visual selection, apply a hash, and insert the hash one line above the selection
M.hash_selection = function(hash_func)
-- Reselect the current visual block to ensure the latest selection is active
vim.cmd("normal! gv")

-- Get the visual selection range using visual marks
local start_pos = vim.fn.getpos("'<") -- Start of the visual selection
local end_pos = vim.fn.getpos("'>") -- End of the visual selection

-- Ensure start_pos and end_pos are valid
start_pos = start_pos or { 0, 0, 0 }
end_pos = end_pos or { 0, 0, 0 }

-- Adjust to capture the correct lines in visual line mode (V)
local start_line = math.min(start_pos[2], end_pos[2])
local end_line = math.max(start_pos[2], end_pos[2])

local start_col = math.max(0, start_pos[3] - 1) -- 0-based index for inline selection
local end_col = math.max(0, end_pos[3]) -- inclusive for inline selection

-- Get the selected lines, replacing nil values with empty strings
local lines = vim.fn.getline(start_line, end_line) or {}
for i = 1, #lines do
lines[i] = lines[i] or "" -- Ensure no nil values
end

-- Handle visual line selection (V) and inline selection (v)
if vim.fn.visualmode() == "V" then
-- Full line selection
local selected_text = table.concat(lines, "\n")
local hash_result = hash_func(selected_text)
-- Insert the hash result above the first line
vim.fn.append(start_line - 1, hash_result)
else
-- Inline selection (v mode)
if start_line == end_line then
-- Handle inline selection on a single line
local line = lines[1] or ""
start_col = math.max(0, start_col)
end_col = math.min(#line, end_col)
local selection = string.sub(line, start_col + 1, end_col)
local hash_result = hash_func(selection)
-- Insert the hash result above the current line
vim.fn.append(start_line - 1, hash_result)
else
-- Multi-line partial selection
local first_line = string.sub(lines[1] or "", start_col + 1)
local last_line = string.sub(lines[#lines] or "", 1, end_col)
local selected_text = first_line .. "\n" .. table.concat(lines, "\n", 2, #lines - 1) .. "\n" .. last_line
local hash_result = hash_func(selected_text)
-- Insert the hash result above the first line
vim.fn.append(start_line - 1, hash_result)
end
end

-- Reselect the visual selection after transformation (optional)
vim.cmd("normal! gv")
end

return M
Loading