Class: Aspera::Secret::Store::EncryptedHash::OsslCipher

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/secret/store/encrypted_hash.rb

Overview

Minimal cipher wrapper using OpenSSL, compatible with the SymmetricEncryption binary format: a 6-byte header "@EnC\x00\x00" followed by AES ciphertext produced with a zero IV (encoding: :none, no random IV in header).

Constant Summary collapse

HEADER =
"@EnC\x00\x00".b.freeze
HEADER_SIZE =
HEADER.bytesize

Instance Method Summary collapse

Constructor Details

#initialize(cipher_name, key) ⇒ OsslCipher

Returns a new instance of OsslCipher.



231
232
233
234
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 231

def initialize(cipher_name, key)
  @cipher_name = cipher_name
  @key         = key
end

Instance Method Details

#decrypt(ciphertext) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 244

def decrypt(ciphertext)
  body = ciphertext.b[HEADER_SIZE..]
  c = OpenSSL::Cipher.new(@cipher_name)
  c.decrypt
  c.key = @key
  c.iv  = "\x00".b * c.iv_len
  (c.update(body) + c.final).force_encoding(Encoding::UTF_8)
end

#encrypt(plaintext) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/aspera/secret/store/encrypted_hash.rb', line 236

def encrypt(plaintext)
  c = OpenSSL::Cipher.new(@cipher_name)
  c.encrypt
  c.key = @key
  c.iv  = "\x00".b * c.iv_len
  HEADER + c.update(plaintext.b) + c.final
end