Skip to content

Commit

Permalink
Introduce hashing without salt
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAppSec committed Sep 7, 2024
1 parent 0850efd commit 9718fbf
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 21 deletions.
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ sudo apt install luarocks -y
sudo luarocks install luasocket
sudo luarocks install lua-zlib
sudo luarocks install luabitop
sudo luarocks install bcrypt
sudo pip install bcrypt argon2-cffi
sudo apt-get install libssl-dev
sudo luarocks install luacrypto

```
## Using it

Expand All @@ -28,21 +33,31 @@ return {
prefix = "<leader>r", -- Change base prefix to <leader>r
keys = {
run_exec = "e", -- <leader>re (Execute Command in Terminal)

encode_prefix = "de", -- <leader>rde (Encode Group)
decode_prefix = "d", -- <leader>rd (Decode Group)
encode_url = "u", -- <leader>rdeu (URL Encode)
decode_url = "u", -- <leader>rdu (URL Decode)
encode_base64 = "b", -- <leader>rdeb (Base64 Encode)
encode_html = "h", -- <leader>rdeh (HTML Encode)
decode_html = "h", -- <leader>rdh (HTML Decode)
encode_ascii_hex = "x", -- <leader>rdex (ASCII Hex Encode)
decode_ascii_hex = "x", -- <leader>rdx (ASCII Hex Decode)
decode_prefix = "d", -- <leader>rd (Decode Group)
encode_url = "u", -- <leader>rdeu (URL Encode)
hash_prefix = "c", -- <leader>rc (Hash Group)

decode_url = "u", -- <leader>rdu (URL Decode)
encode_base64 = "b", -- <leader>rdeb (Base64 Encode)
decode_base64 = "b", -- <leader>rdb (Base64 Decode)
encode_html = "h", -- <leader>rdeh (HTML Encode)
decode_html = "h", -- <leader>rdh (HTML Decode)
encode_ascii_hex = "x", -- <leader>rdex (ASCII Hex Encode)
decode_ascii_hex = "x", -- <leader>rdx (ASCII Hex Decode)
encode_gzip = "g", -- <leader>rdeg (Gzip Encode)
decode_gzip = "g", -- <leader>rdg (Gzip Decode)
encode_binary = "i", -- <leader>rdei (Binary Encode)
decode_binary = "i", -- <leader>rdi (Binary Decode)
encode_octal = "o", -- <leader>rdeo (Octal Encode)
decode_octal = "o", -- <leader>rdo (Octal Decode)
hash_md5 = "m", -- <leader>rcm (MD5 Hash)
hash_sha1 = "s", -- <leader>rcs (SHA-1 Hash)
hash_sha256 = "S", -- <leader>rcS (SHA-256 Hash)
hash_crc32 = "c", -- <leader>rcC (CRC32 Hash)
hash_scrypt = "y", -- <leader>rcy (Scrypt Hash)
hash_bcrypt = "b", -- <leader>rcb (Bcrypt Hash)
},
},
},
Expand Down
90 changes: 90 additions & 0 deletions lua/hacker-helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ local config = {
prefix = "<leader>r", -- Default prefix for Hacker Helper
keys = {
run_exec = "e", -- Default mapping for executing in terminal

encode_prefix = "de", -- <leader>rde (Encode Group)
decode_prefix = "d", -- <leader>rd (Decode Group)
encode_url = "u", -- <leader>rdeu (URL Encode)
hash_prefix = "c", -- <leader>rc (Hash Group)

decode_url = "u", -- <leader>rdu (URL Decode)
encode_base64 = "b", -- <leader>rdeb (Base64 Encode)
decode_base64 = "b", -- <leader>rdb (Base64 Decode)
Expand All @@ -70,6 +73,12 @@ local config = {
decode_binary = "i", -- <leader>rdi (Binary Decode)
encode_octal = "o", -- <leader>rdeo (Octal Encode)
decode_octal = "o", -- <leader>rdo (Octal Decode)
hash_md5 = "m", -- <leader>rcm (MD5 Hash)
hash_sha1 = "s", -- <leader>rcs (SHA-1 Hash)
hash_sha256 = "S", -- <leader>rcS (SHA-256 Hash)
hash_crc32 = "c", -- <leader>rcC (CRC32 Hash)
hash_scrypt = "y", -- <leader>rcy (Scrypt Hash)
hash_bcrypt = "b", -- <leader>rcb (Bcrypt Hash)
},
opt = "Hello!",
}
Expand Down Expand Up @@ -216,6 +225,48 @@ vim.keymap.set("v", M.config.prefix .. M.config.keys.decode_prefix .. M.config.k
end)
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()
M.transform_selection(function(text)
return M.hash_text(text, "md5")
end, "hash", "md5") -- 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()
M.transform_selection(function(text)
return M.hash_text(text, "sha1")
end, "hash", "sha1") -- 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()
M.transform_selection(function(text)
return M.hash_text(text, "sha256")
end, "hash", "sha256") -- 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()
M.transform_selection(function(text)
return M.hash_text(text, "crc32")
end, "hash", "crc32") -- 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()
M.transform_selection(function(text)
return M.hash_text(text, "bcrypt")
end, "hash", "bcrypt") -- 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()
M.transform_selection(function(text)
return M.hash_text(text, "scrypt")
end, "hash", "scrypt") -- Mode: "hash", Encoding: "scrypt"
end, { noremap = true, silent = true, desc = "Scrypt Hash" })

-- Function to handle encoding/decoding based on selection
-- Base64 encoding and decoding utility functions
M.base64_encode = function(text)
Expand Down Expand Up @@ -371,6 +422,45 @@ M.transform_func = function(text, selection_type, encode_or_decode, encoding_typ
return text
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
42 changes: 29 additions & 13 deletions lua/hacker-helper/selection_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

local M = {}

-- Utility function to capture the visual selection, apply a transformation, and replace the selection
M.transform_selection = function(transform_func, encode_or_decode, encoding_type)
M.transform_selection = function(transform_func, mode, encoding_type)
-- Default to "decode" if mode is not provided (backward compatibility)
mode = mode or "decode"

-- Reselect the current visual block to ensure the latest selection is active
vim.cmd("normal! gv")

Expand Down Expand Up @@ -34,16 +36,20 @@ M.transform_selection = function(transform_func, encode_or_decode, encoding_type
vim.notify("Full lines selected: " .. vim.inspect(lines), vim.log.levels.INFO)
-- Apply transformation for full lines
for i, line in ipairs(lines) do
lines[i] = transform_func(line, "full_line", encode_or_decode, encoding_type)
lines[i] = transform_func(line, "full_line", mode, encoding_type)
end

-- If mode is "hash", insert the result above
if mode == "hash" then
vim.fn.append(start_line - 1, lines)
else
vim.fn.setline(start_line, lines) -- Default: Replace lines for encoding/decoding
end
-- Replace the selected lines with the transformed text
vim.fn.setline(start_line, lines)
else
-- Inline selection (v mode)
if start_line == end_line then
-- Handle inline selection on a single line
local line = lines[1] or ""
-- Ensure start_col and end_col are valid
start_col = math.max(0, start_col)
end_col = math.min(#line, end_col)

Expand All @@ -52,11 +58,17 @@ M.transform_selection = function(transform_func, encode_or_decode, encoding_type
vim.notify("Selected part of the line: " .. selection, vim.log.levels.INFO)

-- Transform the selected part
local transformed = transform_func(selection or "", "specific_selection", encode_or_decode, encoding_type)
local transformed = transform_func(selection or "", "specific_selection", mode, encoding_type)

-- Replace the selected part with the transformed text
local new_line = string.sub(line, 1, start_col) .. transformed .. string.sub(line, end_col + 1)
vim.fn.setline(start_line, new_line)

-- If mode is "hash", insert the result above
if mode == "hash" then
vim.fn.append(start_line - 1, transformed)
else
vim.fn.setline(start_line, new_line)
end
else
-- Handle multi-line partial selection
local first_line = string.sub(lines[1] or "", start_col + 1)
Expand All @@ -66,17 +78,21 @@ M.transform_selection = function(transform_func, encode_or_decode, encoding_type

-- Transform first and last lines
lines[1] = string.sub(lines[1] or "", 1, start_col)
.. transform_func(first_line, "multi_line", encode_or_decode, encoding_type)
lines[#lines] = transform_func(last_line, "multi_line", encode_or_decode, encoding_type)
.. transform_func(first_line, "multi_line", mode, encoding_type)
lines[#lines] = transform_func(last_line, "multi_line", mode, encoding_type)
.. string.sub(lines[#lines] or "", end_col + 1)

-- Transform middle lines
for i = 2, #lines - 1 do
lines[i] = transform_func(lines[i], "multi_line", encode_or_decode, encoding_type)
lines[i] = transform_func(lines[i], "multi_line", mode, encoding_type)
end

-- Replace the selected lines with the transformed text
vim.fn.setline(start_line, lines)
-- If mode is "hash", insert the result above
if mode == "hash" then
vim.fn.append(start_line - 1, lines)
else
vim.fn.setline(start_line, lines) -- Default: Replace lines for encoding/decoding
end
end
end

Expand Down
64 changes: 64 additions & 0 deletions tests/hacker-helper/hacker_helper_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,68 @@ describe("encoding/decoding", function()
local decoded = plugin.transform_func(encoded_text, "specific_selection", "decode", "octal")
assert.are.equal("Hello", decoded)
end)

describe("Hashing Functions", function()
-- Test MD5 hashing
it("hashes text using MD5", function()
local text = "hello"
local hashed = plugin.hash_text(text, "md5")
assert.are.equal("5d41402abc4b2a76b9719d911017c592", hashed)
end)

-- Test SHA-1 hashing
it("hashes text using SHA-1", function()
local text = "hello"
local hashed = plugin.hash_text(text, "sha1")
assert.are.equal("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", hashed)
end)

-- Test SHA-256 hashing
it("hashes text using SHA-256", function()
local text = "hello"
local hashed = plugin.hash_text(text, "sha256")
assert.are.equal("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", hashed)
end)

-- Test CRC32 hashing
it("hashes text using CRC32", function()
local text = "hello"
local hashed = plugin.hash_text(text, "crc32")
assert.are.equal("3610a686", hashed)
end)

-- Test Scrypt hashing
it("hashes text using Scrypt", function()
local text = "hello"
local hashed = plugin.hash_text(text, "scrypt")
assert.are.equal(
"de9f496a91b7c783c46a1841f71b4500210adec570f4407fcb2975d8e97e7e747a35816a9988959a6c9d921bbc8b7ea9caa0059e154b732850da77db18497072",
hashed
)
end)
it("hashes test using Bcrypt", function()
local text = "hello"
local hashed = plugin.hash_text(text, "bcrypt")

-- Extract the salt and hashed part from the Bcrypt hash
local salt = string.sub(hashed, 8, 29) -- Salt is from positions 8 to 29 (22 characters)
local hashed_part = string.sub(hashed, 30, 60) -- Hashed part is from positions 30 to 60 (31 characters)

-- Verify Salt
assert.are.equal(22, #salt, "Salt length is incorrect")
for i = 1, #salt do
local char = string.sub(salt, i, i)
local is_valid = string.match(char, "[./A-Za-z0-9]")
assert.is_true(is_valid ~= nil, "Salt contains invalid characters at position " .. i .. ": " .. char)
end

-- Verify Hashed Part
assert.are.equal(31, #hashed_part, "Hashed part length is incorrect")
for i = 1, #hashed_part do
local char = string.sub(hashed_part, i, i)
local is_valid = string.match(char, "[./A-Za-z0-9]")
assert.is_true(is_valid ~= nil, "Hashed part contains invalid characters at position " .. i .. ": " .. char)
end
end)
end)
end)

0 comments on commit 9718fbf

Please sign in to comment.