Class: OneSecret::Secret

Inherits:
Object
  • Object
show all
Defined in:
lib/one_secret/secret.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Secret

Returns a new instance of Secret.



15
16
17
18
19
# File 'lib/one_secret/secret.rb', line 15

def initialize(value)
  @iv = SecureRandom.hex(16)
  @salt = Time.now.to_i.to_s
  @value = Encryptor.encrypt(to_hash.merge(value: value))
end

Class Method Details

.key=(key) ⇒ Object



10
11
12
# File 'lib/one_secret/secret.rb', line 10

def key=(key)
  Encryptor.default_options.merge!({key: key})
end

.load(encrypted_value) ⇒ Object



30
31
32
# File 'lib/one_secret/secret.rb', line 30

def self.load(encrypted_value)
  Encryptor.decrypt(encrypted_value) rescue encrypted_value
end

.unlockedObject



4
5
6
7
8
# File 'lib/one_secret/secret.rb', line 4

def unlocked
  self.key = KeyResolution.try(:env, :rails, :stdin)
  yield
  self.key = nil
end

Instance Method Details

#to_hashObject



21
22
23
24
25
26
27
28
# File 'lib/one_secret/secret.rb', line 21

def to_hash
  {
    value: @value,
    iv: @iv,
    salt: @salt,
    algorithm: Encryptor.default_options[:algorithm]
  }
end