Module: Objc_Obfuscator::Obfuscator

Included in:
CLI
Defined in:
lib/objc-obfuscator/obfuscator.rb

Instance Method Summary collapse

Instance Method Details

#obfuscate_file(filepath, tmp_dir, key, backup = true) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/objc-obfuscator/obfuscator.rb', line 6

def obfuscate_file(filepath, tmp_dir, key, backup=true)
  keyword = '__obfuscated'
  objc_string_regex = /@"(.*)"\s*;/

  encryptor = Objc_Obfuscator::StringEncryptor.new key

  source_file_path = filepath
  temp_file_path = File.join tmp_dir, "#{File.basename(source_file_path)}.bak"
  
  File.delete(temp_file_path) if File.exist?(temp_file_path)
  
  source_file = File.open(source_file_path, 'r')
  dest_file = File.open(temp_file_path, 'w')

  file_changed = false
  say_status :info, "Processing file: #{source_file_path}", :blue
  while !source_file.eof? 
    line = source_file.readline
    if line.include? keyword 
      unencrypted_string = line.scan(objc_string_regex).last.first
      if unencrypted_string.empty?
        say_status :warning, "Found an occurrence of #{keyword} but there's no obj-string there: \"#{line}\""
      else
        say_status :info, "Found occurrence of __obfuscated string: '#{unencrypted_string}'."
        encrypted_string = encryptor.encrypt unencrypted_string 
        line.slice! keyword # remove keyword
        line = line.gsub(unencrypted_string, encrypted_string) # replace the unencrypted with the encrypted
        file_changed = true
      end
    end
    dest_file.puts line
  end
  source_file.close
  dest_file.close

  if file_changed
    if backup
      FileUtils.mv source_file_path, "#{source_file_path}.bak"
    else
      File.delete source_file_path
    end
    FileUtils.mv temp_file_path, source_file_path
  end
end