class Obfusc::Encryptor
Recursive find files from origin or pattern
Constants
- SEP_FROM
- SEP_TO
Public Class Methods
new(config)
click to toggle source
# File lib/obfusc/encryptor.rb, line 9 def initialize(config) @config = config @prefix = "#{config.prefix}__" @suffix = ".#{config.extension}" end
Public Instance Methods
decrypt(path)
click to toggle source
# File lib/obfusc/encryptor.rb, line 23 def decrypt(path) expand_path_for_decrypt(path).join(SEP_FROM) end
encrypt(path)
click to toggle source
# File lib/obfusc/encryptor.rb, line 15 def encrypt(path) crypted_filename = expand_path_for_encrypt(path).map do |step| step.chars.map { |char| charlist[char] || char }.join end.join(SEP_TO) [@prefix, crypted_filename, @suffix].join end
obfuscated?(file)
click to toggle source
# File lib/obfusc/encryptor.rb, line 27 def obfuscated?(file) !!(file =~ obfuscated_expression) end
Protected Instance Methods
charlist()
click to toggle source
# File lib/obfusc/encryptor.rb, line 66 def charlist @charlist ||= begin keys = @config.token.split('') values = @config.secret.split('') Hash[keys.zip(values)] end end
decrypt_segment(segment)
click to toggle source
# File lib/obfusc/encryptor.rb, line 53 def decrypt_segment(segment) segment.chars.map { |char| charlist.key(char) || char }.join end
expand_path_for_decrypt(path, memo = [])
click to toggle source
# File lib/obfusc/encryptor.rb, line 41 def expand_path_for_decrypt(path, memo = []) return memo if String(path).length.zero? if path =~ obfuscated_expression parts = normalize(path).split(SEP_TO) return [*memo, *parts.map { |part| decrypt_segment(part) }] end dir, new_path = path.split(SEP_FROM, 2) expand_path_for_decrypt(new_path, [*memo, dir]) end
expand_path_for_encrypt(path)
click to toggle source
# File lib/obfusc/encryptor.rb, line 33 def expand_path_for_encrypt(path) parts = [] path.split(SEP_FROM).each do |part| parts.push(*try_decrypt(part).split(SEP_FROM)) end parts end
normalize(path)
click to toggle source
# File lib/obfusc/encryptor.rb, line 74 def normalize(path) path.sub!(/^(#{Regexp.escape(@prefix)})/, '') path.sub!(/(#{Regexp.escape(@suffix)})$/, '') path end
obfuscated_expression()
click to toggle source
# File lib/obfusc/encryptor.rb, line 62 def obfuscated_expression /^(#{Regexp.escape(@prefix)}).*(#{Regexp.escape(@suffix)})$/ end
try_decrypt(step)
click to toggle source
# File lib/obfusc/encryptor.rb, line 57 def try_decrypt(step) return step unless step =~ obfuscated_expression decrypt(step) end