module NmDatafile::Crypto

Public Instance Methods

clean_decrypt_string(string, symmetric_key) click to toggle source

This method needs to be available on the NmDatafile module

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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_name, “file_data”}

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

TODu: rename to decode_string_as_password_protected

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/nm_datafile/crypto.rb, line 114
def obfuscated_ending(s)
  junk = "tlD3=\n"
  s + junk
end
obfuscated_ending_undo(s) click to toggle source
# File lib/nm_datafile/crypto.rb, line 119
def obfuscated_ending_undo(s)
  junk = "tlD3=\n"
  s[0...(-1*junk.length)]
end
rearrangement(s) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

converts the string pairs into symbol/ string pairs

# 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) click to toggle source
# File lib/nm_datafile/crypto.rb, line 129
def the_first_three_chars(s)
  s[0..3]
end
the_last_three_chars(s) click to toggle source

hide these somewhere, so ugly

# 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) click to toggle source
# 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) click to toggle source
# File lib/nm_datafile/crypto.rb, line 133
def the_string_minus_the_last_three_chars(s)
  s[0...-3]
end