Class: Virgil::SDK::Cryptography::Keys::KeyStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/virgil/sdk/cryptography/keys/key_storage.rb

Defined Under Namespace

Classes: KeyEntryAlreadyExistsException, KeyEntryNotFoundException, KeyStorageException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path = self.class.default_folder) ⇒ KeyStorage

Returns a new instance of KeyStorage

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 64

def initialize(folder_path=self.class.default_folder)

  raise ArgumentError.new("folder_path is not valid") if (!folder_path.is_a?(String) || folder_path.empty?)

  @folder_path = folder_path
  validate_storage_folder

end

Instance Attribute Details

#folder_pathObject (readonly)

Returns the value of attribute folder_path



42
43
44
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 42

def folder_path
  @folder_path
end

Class Method Details

.default_folderObject



74
75
76
77
78
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 74

def self.default_folder
  path = "./key_storage"
  FileUtils.mkdir(path) unless Dir.exist?(path)
  path
end

Instance Method Details

#delete(item_name) ⇒ Object

Delete the key associated with the given alias.

Parameters:

  • item_name (String)

    The alias name.

Raises:



130
131
132
133
134
135
136
137
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 130

def delete(item_name)

  validate_storage_folder
  raise KeyEntryNotFoundException.new unless exists?(item_name)

  File.delete(item_file_path(item_name))

end

#exists?(item_name) ⇒ Boolean

Checks if the given alias exists in this keystore.

Parameters:

  • item_name (String)

    The alias name.

Returns:

  • (Boolean)

    true if the given alias exists in this keystore and false if the given alias doesn't exist in this keystore.



120
121
122
123
124
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 120

def exists?(item_name)

  File.exist?(item_file_path(item_name))

end

#load(item_name) ⇒ StorageItem

Loads the key associated with the given alias.

Parameters:

  • item_name (String)

    The alias name.

Returns:

Raises:



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 103

def load(item_name)

  validate_storage_folder
  raise KeyEntryNotFoundException.new unless exists?(item_name)

  json_body = File.read(item_file_path(item_name))
  return nil if json_body.nil?

  StorageItem.restore_from_json(item_name, json_body)

end

#store(storage_item) ⇒ Object

Stores the key to the given alias.

Parameters:

  • storage_item (StorageItem)

    The storage item to be kept

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/virgil/sdk/cryptography/keys/key_storage.rb', line 84

def store(storage_item)

  validate_storage_folder
  if exists?(storage_item.name)
    raise KeyEntryAlreadyExistsException.new
  end

  open(item_file_path(storage_item.name), 'w') do |f|
    f.write(storage_item.to_json)
    File.chmod(0400, item_file_path(storage_item.name))
  end

end