Class: NmDatafile::NmDatafile

Inherits:
Object
  • Object
show all
Includes:
Crypto, Debug, FileEncoding
Defined in:
lib/nm_datafile/nm_datafile.rb

Overview

PRIVATE (m) encrypt_file!: Does all encryption procedures before rendering to a string or file

(m) decrypt_file!: reverse of encrypt_file!

(m) encrypt_string: symetrically encrypts a string using a password

(m) decrypt_string: reverse of encrypt_string

(m) encrypt_symetric_key: uses PGP to encrypt the password that encrypted the data

(m) decrypt_symetric_key: reverse of encrypt_symetric_key.

(m) corrupt_zip: corrupts the zip file so it doesn’t look like a zip file, make it look like a jpeg

(m) uncorrupt_zip: reverses corrupt_zip file so the zip can be processed

(m)

aNonyMousDatafile

Constant Summary collapse

@@clear_text_path =

used for using system calls to decrypt and encrypt using a zip password

"clear_text_protected_nmd"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Crypto

#clean_decrypt_string, #clean_encrypt_string, #convert_newline_chars_back_to_symbols, #decode_password_protected_string, #decode_protected_7z, #decode_protected_zip_old_zip_based, #decode_string_as_password_protected, #decode_string_into_NMDatafile_stores, #decrypt_encryptable_data!, #encrypt_using_gpg, #fast_decrypt_string_with_pass, #fast_encrypt_string_with_pass, #obfuscated_ending, #obfuscated_ending_undo, #rearrangement, #rearrangement_undo, #symbolize_keys, #the_first_three_chars, #the_last_three_chars, #the_string_minus_the_first_three_chars, #the_string_minus_the_last_three_chars

Methods included from FileEncoding

#deobfuscate_file_format, #encode_datafiles_as_zip, #encode_string_as_password_protected, #encode_string_as_password_protected_old_zip_based, #obfuscate_file_format

Methods included from Debug

#==, #add_sale, #all_data_matches?, #capture_addresses, #capture_line_items, #create_sales_and_return_data, #create_sales_and_return_data_address_completion_file, #create_sales_for_shippable_file, #generate_upload_params, #inspect, #regenerate_rfsb, #should_gen_a_new_rfsb?, #simulate_address_completion_response, #simulate_rfsb_existance_on_webserver, #to_s

Constructor Details

#initialize(config, *args) ⇒ NmDatafile

notice migration to loading.rb



110
111
112
113
114
115
116
117
118
119
# File 'lib/nm_datafile/nm_datafile.rb', line 110

def initialize(config, *args)
  file_type = config[:file_type]
  @symmetric_key = config[:symmetric_key]
  @schemas = ::NmDatafile::SCHEMA[:schemas]
  set_file_type(file_type)
  
  load_data(args)
  
  setup_object_for_schema
end

Instance Attribute Details

#file_typeObject (readonly)

Returns the value of attribute file_type.



96
97
98
# File 'lib/nm_datafile/nm_datafile.rb', line 96

def file_type
  @file_type
end

#passwordObject (readonly)

Returns the value of attribute password.



96
97
98
# File 'lib/nm_datafile/nm_datafile.rb', line 96

def password
  @password
end

#schemasObject

Returns the value of attribute schemas.



97
98
99
# File 'lib/nm_datafile/nm_datafile.rb', line 97

def schemas
  @schemas
end

#symmetric_keyObject (readonly)

Returns the value of attribute symmetric_key.



96
97
98
# File 'lib/nm_datafile/nm_datafile.rb', line 96

def symmetric_key
  @symmetric_key
end

Instance Method Details

#build_attributesObject



199
200
201
202
203
# File 'lib/nm_datafile/nm_datafile.rb', line 199

def build_attributes
  hash = { file_type: @file_type
            # build_date: Time.zone.now,           # TODO:  change me to the date the data was last modified...
          }.to_json
end

#build_data_collectionsObject



191
192
193
# File 'lib/nm_datafile/nm_datafile.rb', line 191

def build_data_collections
  @data_collections.to_json
end

#build_data_objectsObject



195
196
197
# File 'lib/nm_datafile/nm_datafile.rb', line 195

def build_data_objects
  @data_objects.to_json
end

#build_encryptable_portionObject



211
212
213
214
215
# File 'lib/nm_datafile/nm_datafile.rb', line 211

def build_encryptable_portion
  e = { :data_collections => @data_collections,
    :data_objects => @data_objects
  }.to_json
end

#build_encryptionObject



205
206
207
208
209
# File 'lib/nm_datafile/nm_datafile.rb', line 205

def build_encryption
  hash = { integrity_hash: integrity_hash,
           password: clean_encrypt_string(@password, @symmetric_key)
         }.to_json
end

#data_collection_namesObject



227
228
229
# File 'lib/nm_datafile/nm_datafile.rb', line 227

def data_collection_names
  schema[:data_collections]
end

#data_object_namesObject



231
232
233
# File 'lib/nm_datafile/nm_datafile.rb', line 231

def data_object_names
  schema[:data_objects]
end

#duplicate_batch?(previous_batch) ⇒ Boolean

Move to… NmDatafile

Returns:

  • (Boolean)


300
301
302
303
304
# File 'lib/nm_datafile/nm_datafile.rb', line 300

def duplicate_batch?(previous_batch)
  return false if previous_batch.nil?
  
  previous_batch.sf_integrity_hash == integrity_hash
end

#get_temp_directoryObject

This method get’s the temp directory. If it’s a rails app, that would be Rails.root/tmp, else just /tmp



283
284
285
# File 'lib/nm_datafile/nm_datafile.rb', line 283

def get_temp_directory
  defined?(Rails) ? "#{Rails.root}/tmp" : "/tmp"
end

#init_data_arraysObject



245
246
247
248
249
250
# File 'lib/nm_datafile/nm_datafile.rb', line 245

def init_data_arrays
  @data_collections = []
  data_collection_names.count.times { @data_collections << [] }
  @data_objects = []
  data_object_names.count.times { @data_objects << [] }
end

#integrity_hashObject



186
187
188
189
# File 'lib/nm_datafile/nm_datafile.rb', line 186

def integrity_hash
  encryptable_portion = build_encryptable_portion
  Digest::SHA2.hexdigest(encryptable_portion)
end

#load_attributes(attribute_data) ⇒ Object



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

def load_attributes(attribute_data)
  d = YAML::load attribute_data
  @file_type = d["file_type"].to_sym
  @build_date = Time.zone.parse d["build_date"] unless d["build_date"].nil?
end

#load_data(*args) ⇒ Object

(m) load_data: loads array of data into memory as an NmDatafile object clears the data arrays so it’s like reinitializing the whole file



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/nm_datafile/nm_datafile.rb', line 135

def load_data(*args)
  init_data_arrays  # wipes all preexisting data in @data_collections and @data_objects
  
  args[0].each.with_index do |array_or_variable, i|
    if i < data_collection_names.count
      @data_collections[i] += array_or_variable 
    else
     j = i - data_collection_names.count
     @data_objects[j] = array_or_variable #  if array_or_variable.class != Array
    end
  end
  self
end

#load_encryption(encryption_data) ⇒ Object



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

def load_encryption(encryption_data)
  d = YAML::load encryption_data
  @integrity_hash = d["integrity_hash"] unless d["integrity_hash"].nil?
  @password = ::NmDatafile.clean_decrypt_string(d["password"], @symmetric_key) unless d["password"].nil?
end

#save_to_file(path) ⇒ Object



163
164
165
# File 'lib/nm_datafile/nm_datafile.rb', line 163

def save_to_file(path)
  File.write(path, save_to_string)
end

#save_to_stringObject



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/nm_datafile/nm_datafile.rb', line 150

def save_to_string
  set_password
  
  clear_text = build_encryptable_portion
  encrypted_data = encode_string_as_password_protected(clear_text)
  
  hash_of_entries = { :crypt => encrypted_data,
                      :encryption => build_encryption,
                      :attributes => build_attributes }
  
  encode_datafiles_as_zip(hash_of_entries)
end

#schemaObject

schema related junk #



223
224
225
# File 'lib/nm_datafile/nm_datafile.rb', line 223

def schema
  @schemas[@file_type]
end

#set_file_type(file_type) ⇒ Object

specify the schema type



240
241
242
243
# File 'lib/nm_datafile/nm_datafile.rb', line 240

def set_file_type(file_type)
  @file_type = file_type
  init_data_arrays
end

#set_passwordObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/nm_datafile/nm_datafile.rb', line 167

def set_password
  len = 41
  
  password = ""
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')']
  
  1.upto(len) { |i| password << chars[rand(chars.size-1)]}
  
  space = rand(len-1)
  password[space] = ("0".."9").to_a[rand(9)]  # ensure there's number and letter
  space += 1
  space = 0 if space == len
  password[space] = ("a".."z").to_a[rand(22)]
  
  @password = password
end

#setup_object_for_schemaObject

This makes it so you can call file.attribute to get direct access to an attribute



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/nm_datafile/nm_datafile.rb', line 253

def setup_object_for_schema
  data_collection_names.each.with_index do |data_collection_name, i|
    # define getter
    self.define_singleton_method(var_names[i]) do
      @data_collections[i]
    end
    # define setter
    self.define_singleton_method((data_collection_names[i].to_s + "=").to_sym) do |val|
      @data_collections[i] = val
    end
  end
  
  data_object_names.each.with_index do |data_object_name, i|
    # getters
    self.define_singleton_method(data_object_name) do
      @data_objects[i]
    end
    # setters
    self.define_singleton_method((data_object_name.to_s + "=").to_sym) do |val|
      @data_objects[i] = val
    end
  end
  
end

#var_namesObject



235
236
237
# File 'lib/nm_datafile/nm_datafile.rb', line 235

def var_names
  data_collection_names + data_object_names
end