Class: BaseAction

Inherits:
Object
  • Object
show all
Defined in:
lib/lockr/action/base.rb

Direct Known Subclasses

AesAction

Instance Method Summary collapse

Instance Method Details

#calculate_hash(filename) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lockr/action/base.rb', line 5

def calculate_hash( filename)
  sha1 = OpenSSL::Digest::SHA512.new

  File.open( filename) do |file|
    buffer = ''

    # Read the file 512 bytes at a time
    while not file.eof
      file.read(512, buffer)
      sha1.update(buffer)
    end
  end
  
  sha1.to_s
end

#load_from_vault(vault) ⇒ Object

loads the datastructure for the password sets from the file it looks like this:

pwd_directory = {

:id => { 
  :enc  => 'encrypted password store list', 
  :salt => 'salt for decryption' 
}

}

decrypted_store_list =

:username => PasswordStore



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lockr/action/base.rb', line 60

def load_from_vault( vault)
  storelist = {} 
  
  if File.exist?( vault)
    File.open( vault, 'r') do |f|
      storelist = YAML::load(f)
    end
  end
  
  storelist
end

#rotate_vault(vault) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lockr/action/base.rb', line 29

def rotate_vault( vault)
  return unless File.exists?(vault)
  
  # move old files first
  max_files = 2 # = 3 - 1
  max_files.downto( 0) { |i|
    
    if i == 0
      File.rename( vault, "#{vault}_#{i}")
    else
      j = i - 1
      if File.exists?("#{vault}_#{j}")
        File.rename( "#{vault}_#{j}", "#{vault}_#{i}")  
      end
    end
  }
end

#save_to_vault(storelist, vault) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/lockr/action/base.rb', line 21

def save_to_vault( storelist, vault)
  rotate_vault( vault)
  
  File.open( vault, 'w') do |f|
    f.write( storelist.to_yaml)
  end
end