Class: Eco::Data::Crypto::EncryptedData

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/data/crypto/encryption.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = {}) ⇒ EncryptedData

Returns a new instance of EncryptedData.



214
215
216
217
218
219
220
# File 'lib/eco/data/crypto/encryption.rb', line 214

def initialize(init = {})
  @key = init[:key]
  @iv = init[:iv]
  @content = init[:content]
  @sha = sha256(@content)
  load() if !@key || !@iv
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



213
214
215
# File 'lib/eco/data/crypto/encryption.rb', line 213

def content
  @content
end

#ivObject (readonly)

Returns the value of attribute iv.



213
214
215
# File 'lib/eco/data/crypto/encryption.rb', line 213

def iv
  @iv
end

#keyObject (readonly)

Returns the value of attribute key.



213
214
215
# File 'lib/eco/data/crypto/encryption.rb', line 213

def key
  @key
end

Instance Method Details

#load(file = nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/eco/data/crypto/encryption.rb', line 221

def load (file = nil)
  parser = get_parser(file)
  sha64 = Base64.encode64(@sha) if @sha
  if parser.key?(sha64)
    #puts "found key!"
    meta = parser[sha64]
    key64, iv64 = meta.values_at("key", "iv")
    @key = Base64.decode64(key64) if key64
    @iv = Base64.decode64(iv64) if iv64
  else
    #puts "key not found! #{sha64}"
  end
end

#save(file = nil) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/eco/data/crypto/encryption.rb', line 234

def save (file = nil)
  parser = get_parser(file)
  sha64 = Base64.encode64(@sha) if @sha
  #puts "save key: #{sha64}"
  if parser.key?(sha64)
    meta = parser[sha64]
  else
    meta = {key: nil, iv: nil}
    parser[sha64] = meta
  end
  meta[:key] = Base64.encode64(@key) if @key
  meta[:iv] = Base64.encode64(@iv) if @iv
  set_parser(file, parser)
end