Method: Ccrypto::Ruby::PKCS7Engine#encrypt

Defined in:
lib/ccrypto/ruby/engines/pkcs7_engine.rb

#encrypt(val, &block) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ccrypto/ruby/engines/pkcs7_engine.rb', line 123

def encrypt(val, &block)
  validate_input(val, "encrypt") 
  raise PKCS7EngineException, "At least one recipient_cert is required for PKCS7 encrypt" if is_empty?(@config.recipient_certs)
  
  recps = @config.recipient_certs.map do |c|
    raise PKCS7EngineException, "Given recipient_cert must be a Ccrypto::X509Cert object" if not c.is_a?(Ccrypto::X509Cert)
    c.nativeX509
  end

  if block
    cipher = block.call(:cipher)
    teLogger.debug "Application given cipher : #{cipher}"
  end

  cipher = "AES-256-CBC" if is_empty?(cipher)

  teLogger.debug "Setting P7 encryption cipher #{cipher}"
  cip = OpenSSL::Cipher.new(cipher)

  begin
    OpenSSL::PKCS7.encrypt(recps, val, cip, OpenSSL::PKCS7::BINARY)
  rescue OpenSSL::PKCS7::PKCS7Error => ex
    raise PKCS7EngineException, ex
  end

end