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) ⇒ StorageItem
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.
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.
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.
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.
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 |