class NmDatafile::NmDatafile

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

Attributes

file_type[R]
password[R]
schemas[RW]
symmetric_key[R]

Public Class Methods

new(config, *args) click to toggle source

notice migration to loading.rb

# 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

Public Instance Methods

build_attributes() click to toggle source
# 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_collections() click to toggle source
# File lib/nm_datafile/nm_datafile.rb, line 191
def build_data_collections
  @data_collections.to_json
end
build_data_objects() click to toggle source
# File lib/nm_datafile/nm_datafile.rb, line 195
def build_data_objects
  @data_objects.to_json
end
build_encryptable_portion() click to toggle source
# 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_encryption() click to toggle source
# 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_names() click to toggle source
# File lib/nm_datafile/nm_datafile.rb, line 227
def data_collection_names
  schema[:data_collections]
end
data_object_names() click to toggle source
# File lib/nm_datafile/nm_datafile.rb, line 231
def data_object_names
  schema[:data_objects]
end
duplicate_batch?(previous_batch) click to toggle source

Move to… NmDatafile

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

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

# File lib/nm_datafile/nm_datafile.rb, line 283
def get_temp_directory
  defined?(Rails) ? "#{Rails.root}/tmp" : "/tmp"
end
init_data_arrays() click to toggle source
# 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_hash() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

(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

# 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) click to toggle source
# 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) click to toggle source
# File lib/nm_datafile/nm_datafile.rb, line 163
def save_to_file(path)
  File.write(path, save_to_string)
end
save_to_string() click to toggle source
# 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
schema() click to toggle source

schema related junk #

# File lib/nm_datafile/nm_datafile.rb, line 223
def schema
  @schemas[@file_type]
end
set_file_type(file_type) click to toggle source

specify the schema type

# File lib/nm_datafile/nm_datafile.rb, line 240
def set_file_type(file_type)
  @file_type = file_type
  init_data_arrays
end
set_password() click to toggle source
# 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_schema() click to toggle source

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

# 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_names() click to toggle source
# File lib/nm_datafile/nm_datafile.rb, line 235
def var_names
  data_collection_names + data_object_names
end