Skip to content

Instantly share code, notes, and snippets.

@adiprnm
Last active July 27, 2025 13:59
Show Gist options
  • Save adiprnm/cae9787b87114a8936bbc4d3f6e37688 to your computer and use it in GitHub Desktop.
Save adiprnm/cae9787b87114a8936bbc4d3f6e37688 to your computer and use it in GitHub Desktop.
Obfuscate the text within a file
# Usage: ruby obfuscate.rb /path/to/file line-offset
# Example: ruby obfuscate.rb ./test.md 3
# The command above will obfuscate the text in the given file path from line 4 onwards. The obfuscated result will be stored in ./test-obfuscated.md
def obfuscate(text)
return text if text == ''
text.split(' ').map do |token|
token.chars.map do |char|
if char.ord.between?(65, 90)
new_char = char.ord + rand(26)
new_char = new_char > 90 ? new_char - 26 : new_char
elsif char.ord.between?(97, 122)
new_char = char.ord + rand(26)
new_char = new_char > 122 ? new_char - 26 : new_char
else
new_char = char
end
new_char.chr
end.join
end.join(' ')
end
path = ARGV[0]
offset = ARGV[1].to_i
raise 'File not exists' unless File.exist?(path)
tokens = File.read(path).split("\n")
first_tokens = tokens[0..(offset - 1)]
remaining_tokens = tokens[offset..]
remaining_tokens = remaining_tokens.map { |token| obfuscate(token) }
result = (first_tokens + remaining_tokens).join("\n")
path_tokens = path.split('.')
extension = path_tokens.last if path_tokens.size > 1
new_file = "#{path_tokens[0..-2].join('.')}-obfuscated"
new_file = "#{new_file}.#{extension}" if extension
File.write(new_file, result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment