Module: NmDatafile::Crypto

Included in:
NmDatafile
Defined in:
lib/nm_datafile/crypto.rb

Instance Method Summary collapse

Instance Method Details

#clean_decrypt_string(string) ⇒ Object



80
81
82
83
84
# File 'lib/nm_datafile/crypto.rb', line 80

def clean_decrypt_string(string)
#  string = Base64.decode64 string
#  decode_password_protected_string(@@unsecure_pass, string)
  NMDatafile.fast_decrypt_string_with_pass(@@unsecure_pass, string)
end

#convert_newline_chars_back_to_symbols(clear_text_hash) ⇒ Object



53
54
55
# File 'lib/nm_datafile/crypto.rb', line 53

def convert_newline_chars_back_to_symbols(clear_text_hash)
  clear_text_hash.gsub("\n", "\\n")
end

#decode_password_protected_string(password, decryptable_portion) ⇒ Object

Takes in a password and binary data of protected archive file and outputs a string of it’s first entry in clear text which should be a hash of “file_data”



35
36
37
# File 'lib/nm_datafile/crypto.rb', line 35

def decode_password_protected_string(password, decryptable_portion)
  decode = decode_string_as_password_protected(password, decryptable_portion)
end

#decode_protected_7z(password, decryptable_portion) ⇒ Object

I think 7z is considered secure and zip is considered insecure so write this



58
59
60
# File 'lib/nm_datafile/crypto.rb', line 58

def decode_protected_7z(password, decryptable_portion)
  # TODu: implement
end

#decode_protected_zip_old_zip_based(password, decryptable_portion) ⇒ Object

The zip implementation… should use 7z though and throw this out when it’s done… or truecript or gpg…



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nm_datafile/crypto.rb', line 7

def decode_protected_zip_old_zip_based(password, decryptable_portion)
  encryptable_portion = Tempfile.new('encryptable_portion', "#{Rails.root}/tmp")
  FileUtils.rm(encryptable_portion.path)
  File.binwrite(encryptable_portion.path, decryptable_portion)
  
  supress_errors = "2>/dev/null"
  decompress_zip_cmd = "funzip -'#{password}' '#{encryptable_portion.path}' #{supress_errors}"

  clear_text_hash = `#{decompress_zip_cmd}`.chomp
  
  clear_text_hash = convert_newline_chars_back_to_symbols(clear_text_hash)
  
  FileUtils.rm(encryptable_portion.path)
  clear_text_hash
end

#decode_string_as_password_protected(password, decryptable_portion) ⇒ Object

TODu: rename to decode_string_as_password_protected



24
25
26
27
28
29
# File 'lib/nm_datafile/crypto.rb', line 24

def decode_string_as_password_protected(password, decryptable_portion)
  #require 'b_f'
  bf = BF.new(password, true)
  
  bf.decrypt(decryptable_portion)
end

#decode_string_into_NMDatafile_stores(password, crypt_string) ⇒ Object



73
74
75
76
# File 'lib/nm_datafile/crypto.rb', line 73

def decode_string_into_NMDatafile_stores(password, crypt_string)
  decode = YAML::load decode_password_protected_string(password, crypt_string)
  decode = symbolize_keys(decode)
end

#decrypt_encryptable_data!(password, hash) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/nm_datafile/crypto.rb', line 63

def decrypt_encryptable_data!(password, hash)
  return if hash[:crypt].nil?  # leave this function if there's no 'crypt' entry to decrypt
  
  decode = decode_string_into_NMDatafile_stores(password, hash[:crypt])
  
  hash.delete :crypt
  
  hash.merge!(decode)
end

#encrypt_using_gpg(pass, string) ⇒ Object



139
140
141
142
143
# File 'lib/nm_datafile/crypto.rb', line 139

def encrypt_using_gpg(pass, string)
  crypto = GPGME::Crypto.new :symmetric => true, :password => "gpgme"
  encrypted_data = crypto.encrypt "string"
  encrypted_data.read
end

#fast_decrypt_string_with_pass(pass, string) ⇒ Object



95
96
97
98
99
100
# File 'lib/nm_datafile/crypto.rb', line 95

def fast_decrypt_string_with_pass(pass, string)
  obfs = Base64.decode64(string)
  rearranged = obfuscated_ending_undo(obfs)
  encoded_as_base64 = rearrangement_undo(rearranged)
  Base64.decode64(encoded_as_base64)
end

#fast_encrypt_string_with_pass(pass, string) ⇒ Object



88
89
90
91
92
93
# File 'lib/nm_datafile/crypto.rb', line 88

def fast_encrypt_string_with_pass(pass, string)
  encoded_as_base64 = Base64.encode64(string)
  rearranged = rearrangement(encoded_as_base64)
  obfs = obfuscated_ending(rearranged)
  Base64.encode64(obfs)
end

#obfuscated_ending(s) ⇒ Object



111
112
113
114
# File 'lib/nm_datafile/crypto.rb', line 111

def obfuscated_ending(s)
  junk = "tlD3=\n"
  s + junk
end

#obfuscated_ending_undo(s) ⇒ Object



116
117
118
119
# File 'lib/nm_datafile/crypto.rb', line 116

def obfuscated_ending_undo(s)
  junk = "tlD3=\n"
  s[0...(-1*junk.length)]
end

#rearrangement(s) ⇒ Object



102
103
104
# File 'lib/nm_datafile/crypto.rb', line 102

def rearrangement(s)
  s = the_last_three_chars(s) + the_string_minus_the_last_three_chars(s)
end

#rearrangement_undo(s) ⇒ Object



106
107
108
# File 'lib/nm_datafile/crypto.rb', line 106

def rearrangement_undo(s)
  s = the_string_minus_the_first_three_chars(s) + the_first_three_chars(s)
end

#symbolize_keys(decode) ⇒ Object

converts the string pairs into symbol/ string pairs



42
43
44
45
46
47
48
# File 'lib/nm_datafile/crypto.rb', line 42

def symbolize_keys(decode)
  p = {}
  decode.each do |key_pair|
    p.merge!( { key_pair[0].to_sym => key_pair[1] } )
  end
  p
end

#the_first_three_chars(s) ⇒ Object



126
127
128
# File 'lib/nm_datafile/crypto.rb', line 126

def the_first_three_chars(s)
  s[0..3]
end

#the_last_three_chars(s) ⇒ Object

hide these somewhere, so ugly



122
123
124
# File 'lib/nm_datafile/crypto.rb', line 122

def the_last_three_chars(s)
  s[-3..-1]
end

#the_string_minus_the_first_three_chars(s) ⇒ Object



134
135
136
# File 'lib/nm_datafile/crypto.rb', line 134

def the_string_minus_the_first_three_chars(s)
  s[2..-1]
end

#the_string_minus_the_last_three_chars(s) ⇒ Object



130
131
132
# File 'lib/nm_datafile/crypto.rb', line 130

def the_string_minus_the_last_three_chars(s)
  s[0...-3]
end