Class: Aspera::Keychain::EncryptedHash::OsslCipher

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/keychain/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.



118
119
120
121
# File 'lib/aspera/keychain/encrypted_hash.rb', line 118

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

Instance Method Details

#decrypt(ciphertext) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/aspera/keychain/encrypted_hash.rb', line 131

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



123
124
125
126
127
128
129
# File 'lib/aspera/keychain/encrypted_hash.rb', line 123

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