Module: NmDatafile::Crypto

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

Instance Method Summary collapse

Instance Method Details

#clean_decrypt_string(string, symmetric_key) ⇒ Object

This method needs to be available on the NmDatafile module



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

def clean_decrypt_string(string, symmetric_key)
  fast_decrypt_string_with_pass(symmetric_key, string)
end

#clean_encrypt_string(string, symmetric_key) ⇒ Object



84
85
86
# File 'lib/nm_datafile/crypto.rb', line 84

def clean_encrypt_string(string, symmetric_key)
  fast_encrypt_string_with_pass(symmetric_key, string)
end

#convert_newline_chars_back_to_symbols(clear_text_hash) ⇒ Object



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

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”



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

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



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

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
# File 'lib/nm_datafile/crypto.rb', line 24

def decode_string_as_password_protected(password, decryptable_portion)
  clear_text = Blowfish.decrypt(password, decryptable_portion)
  
  clear_text
end

#decode_string_into_NMDatafile_stores(password, crypt_string) ⇒ Object



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

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



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

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



142
143
144
145
146
# File 'lib/nm_datafile/crypto.rb', line 142

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



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

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

#fast_encrypt_string_with_pass(pass, string) ⇒ Object



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

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

#obfuscated_ending(s) ⇒ Object



114
115
116
117
# File 'lib/nm_datafile/crypto.rb', line 114

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

#obfuscated_ending_undo(s) ⇒ Object



119
120
121
122
# File 'lib/nm_datafile/crypto.rb', line 119

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

#rearrangement(s) ⇒ Object



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

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

#rearrangement_undo(s) ⇒ Object



109
110
111
# File 'lib/nm_datafile/crypto.rb', line 109

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



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

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



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

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

#the_last_three_chars(s) ⇒ Object

hide these somewhere, so ugly



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

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

#the_string_minus_the_first_three_chars(s) ⇒ Object



137
138
139
# File 'lib/nm_datafile/crypto.rb', line 137

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

#the_string_minus_the_last_three_chars(s) ⇒ Object



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

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