module AWS::S3::EncryptionUtils

def encrypt data, key

Returns:
  • (String) - Returns the data encrypted with the key given.

Other tags:
    Note: - This should not be used for data longer than the key length as
    Note: - Use check_encryption_materials before this method to check

Parameters:
  • data (String) -- Data to be encrypted.
  • key (OpenSSL::PKey::RSA, String) -- Key used to encrypt.
def encrypt data, key
  rsa = OpenSSL::PKey::RSA
  data_cipher_size = get_cipher_size(data.length)
  # Encrypting data key
  case key
  when rsa # Asymmetric encryption
    warn UNSAFE_MSG if key.public_key.n.num_bits < data_cipher_size
    key.public_encrypt(data)
  when String             # Symmetric encryption
    warn UNSAFE_MSG if get_cipher_size(key.length) < data_cipher_size
    cipher = get_aes_cipher(:encrypt, :ECB, key)
    cipher.update(data) + cipher.final
  end
end