Class: Virgil::SDK::Cryptography::Keys::KeyStorage
- Inherits:
-
Object
- Object
- Virgil::SDK::Cryptography::Keys::KeyStorage
- Defined in:
- lib/virgil/sdk/cryptography/keys/key_storage.rb
Defined Under Namespace
Classes: KeyEntryAlreadyExistsException, KeyEntryNotFoundException, KeyStorageException
Instance Attribute Summary collapse
-
#folder_path ⇒ Object
readonly
Returns the value of attribute folder_path.
Class Method Summary collapse
Instance Method Summary collapse
-
#delete(item_name) ⇒ Object
Delete the key associated with the given alias.
-
#exists?(item_name) ⇒ Boolean
Checks if the given alias exists in this keystore.
-
#initialize(folder_path = self.class.default_folder) ⇒ KeyStorage
constructor
A new instance of KeyStorage.
-
#load(item_name) ⇒ Object
Loads the key associated with the given alias.
-
#store(storage_item) ⇒ Object
Stores the key to the given alias.
Constructor Details
#initialize(folder_path = self.class.default_folder) ⇒ KeyStorage
Returns a new instance of KeyStorage.
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_path ⇒ Object (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_folder ⇒ Object
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.
Args:
item_name: The alias name.
Raises:
KeyEntryNotFoundException: if key storage doesn't have item with such name
149 150 151 152 153 154 155 156 |
# 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) ⇒ Boolean
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.
135 136 137 138 139 |
# 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) ⇒ Object
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
114 115 116 117 118 119 120 121 122 123 124 |
# 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) ⇒ Object
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
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 |