Module: LockrFileUtils

Included in:
PasswordManager, SFTP
Defined in:
lib/lockr/fileutils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calculate_sha512_hash(filename) ⇒ Object

calculate the sha512 hash of a file



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lockr/fileutils.rb', line 61

def LockrFileUtils.calculate_sha512_hash( filename)
  sha512 = 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)
      sha512.update(buffer)
    end
  end
  
  sha512.to_s
end

.copy(file_src, file_target) ⇒ Object

copy file_src to file_target



28
29
30
31
32
33
34
35
36
# File 'lib/lockr/fileutils.rb', line 28

def LockrFileUtils.copy( file_src, file_target)
  return unless File.exists?( file_src)
  
  dst = File.new( file_target, 'w')
  File.open( file_src, 'r') do |src|
    dst.write( src.read)
  end
  dst.close
end

.load_obj_yaml(file) ⇒ Object

load an yaml object from file



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lockr/fileutils.rb', line 46

def LockrFileUtils.load_obj_yaml( file)
  object = {}
  
  unless File.exist?( file)
    return object
  end
  
  File.open( file, 'r') do |f|
    object = YAML::load(f)
  end
  
  object
end

.rotate_file(file, limit) ⇒ Object

rotate the provided file with a maximum of ‘limit’ backups renamed filed will be named file_0, file_1, …



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lockr/fileutils.rb', line 7

def LockrFileUtils.rotate_file( file, limit)
  return unless File.exists?(file)
  
  # move old files first
  max_files = limit - 1 
  max_files.downto( 0) { |i|
    
    if i == 0
      LockrFileUtils.copy( file, "#{file}_#{i}")
    else
      j = i - 1
      if File.exists?("#{file}_#{j}")
        LockrFileUtils.copy( "#{file}_#{j}", "#{file}_#{i}")  
      end
    end
  }
  
  puts "Rotated local vault file(s)"
end

.store_obj_yaml(file, object) ⇒ Object

store an object as yaml to file



39
40
41
42
43
# File 'lib/lockr/fileutils.rb', line 39

def LockrFileUtils.store_obj_yaml( file, object)
  File.open( file, 'w') do |f|
    f.write( object.to_yaml)
  end
end

Instance Method Details

#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



94
95
96
# File 'lib/lockr/fileutils.rb', line 94

def load_from_vault( vault)
  LockrFileUtils.load_obj_yaml( vault)
end

#save_to_vault(storelist, vault) ⇒ Object



77
78
79
# File 'lib/lockr/fileutils.rb', line 77

def save_to_vault( storelist, vault)
  LockrFileUtils.store_obj_yaml( vault, storelist)
end