class Virgil::SDK::Cryptography::Keys::KeyStorage
Attributes
folder_path[R]
Public Class Methods
default_folder()
click to toggle source
# 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
new(folder_path=self.class.default_folder)
click to toggle source
# 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
Public Instance Methods
delete(item_name)
click to toggle source
Delete the key associated with the given alias.
Args:
item_name: The alias name.
Raises:
KeyEntryNotFoundException: if key storage doesn't have item with such name
# File lib/virgil/sdk/cryptography/keys/key_storage.rb, line 149 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)
click to toggle source
Checks if the given alias exists in this keystore.
Args:
item_name: The alias name.
Returns:
true if the given alias exists in this keystore. false if the given alias doesn't exist in this keystore.
# File lib/virgil/sdk/cryptography/keys/key_storage.rb, line 135 def exists?(item_name) File.exist?(item_file_path(item_name)) end
load(item_name)
click to toggle source
Loads the key associated with the given alias.
Args:
item_name: The alias name.
Returns:
The requested key, or null if the given alias does not exist or does not identify a key-related entry.
Raises:
KeyEntryNotFoundException: if key storage doesn't have item with such name
# File lib/virgil/sdk/cryptography/keys/key_storage.rb, line 114 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)
click to toggle source
Stores the key to the given alias.
Args:
storage_item: The storage item to be kept
Raises:
KeyEntryAlreadyExistsException: if key storage already has item with such name
# File lib/virgil/sdk/cryptography/keys/key_storage.rb, line 88 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
Private Instance Methods
item_file_path(item_name)
click to toggle source
# File lib/virgil/sdk/cryptography/keys/key_storage.rb, line 169 def item_file_path(item_name) File.join(folder_path, item_name) end
validate_storage_folder()
click to toggle source
# File lib/virgil/sdk/cryptography/keys/key_storage.rb, line 161 def validate_storage_folder unless (Dir.exist?(folder_path) && File.writable?(folder_path) && File.readable?(folder_path)) raise KeyStorageException.new("Destination folder doesn't exist or you don't have permission to write there") end end