def binary_encrypt(str, random_iv: SymmetricEncryption.randomize_iv?, compress: false, header: always_add_header)
      return if str.nil?

      string = str.to_s
      return string if string.empty?

      # Header required when adding a random_iv or compressing
      header = Header.new(version: version, compress: compress) if header || random_iv || compress

      # Creates a new OpenSSL::Cipher with every call so that this call is thread-safe.
      openssl_cipher = ::OpenSSL::Cipher.new(cipher_name)
      openssl_cipher.encrypt
      openssl_cipher.key = @key

      result =
        if header
          if random_iv
            openssl_cipher.iv = header.iv = openssl_cipher.random_iv
          elsif iv
            openssl_cipher.iv = iv
          end
          header.to_s + openssl_cipher.update(compress ? Zlib::Deflate.deflate(string) : string)
        else
          openssl_cipher.iv = iv if iv
          openssl_cipher.update(string)
        end
      result << openssl_cipher.final
    end
def binary_encrypt(str, random_iv: SymmetricEncryption.randomize_iv?, compress: false, header: always_add_header)
      return if str.nil?

      string = str.to_s
      return string if string.empty?

      # Header required when adding a random_iv or compressing
      header = Header.new(version: version, compress: compress) if header || random_iv || compress

      # Creates a new OpenSSL::Cipher with every call so that this call is thread-safe.
      openssl_cipher = ::OpenSSL::Cipher.new(cipher_name)
      openssl_cipher.encrypt
      openssl_cipher.key = @key

      result =
        if header
          if random_iv
            openssl_cipher.iv = header.iv = openssl_cipher.random_iv
          elsif iv
            openssl_cipher.iv = iv
          end
          header.to_s + openssl_cipher.update(compress ? Zlib::Deflate.deflate(string) : string)
        else
          openssl_cipher.iv = iv if iv
          openssl_cipher.update(string)
        end
      result << openssl_cipher.final
    end
Advanced use only

 Returns a Binary encrypted string without applying Base64, or any other encoding.

   str [String]
     String to be encrypted. If str is not a string, #to_s will be called on it
     to convert it to a string

   random_iv [true|false]
     Whether the encypted value should use a random IV every time the
     field is encrypted.
     Notes:
     * Setting random_iv to true will result in a different encrypted output for
       the same input string.
     * It is recommended to set this to true, except if it will be used as a lookup key.
     * Only set to true if the field will never be used as a lookup key, since
       the encrypted value needs to be same every time in this case.
     * When random_iv is true it adds the random IV string to the header.
     Default: false
     Highly Recommended where feasible: true

   compress [true|false]
     Whether to compress str before encryption.
     Default: false
     Notes:
     * Should only be used for large strings since compression overhead and
       the overhead of adding the encryption header may exceed any benefits of
       compression

   header [true|false]
     Whether to add a header to the encrypted string.
     Default: `always_add_header`

 See #encrypt to encrypt and encode the result as a string.