Sha256: d6873c623250d8a2694966ccd2d54d719a28eb62be4f073f86daeaeccdbb441d

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

# (c) 2017 Ribose Inc.

require 'ffi'

require 'botan/defaults'
require 'botan/error'
require 'botan/ffi/libbotan'
require 'botan/pk/privatekey'
require 'botan/utils'

module Botan
  module PK
    # Public Key Decrypt Operation
    #
    # See {Botan::PK::PrivateKey#decrypt} for a simpler interface.
    class Decrypt
      # @param key [Botan::PK::PrivateKey] the private key
      # @param padding [String] the padding method name
      def initialize(key:, padding: nil)
        padding ||= Botan::DEFAULT_EME
        unless key.instance_of?(PrivateKey)
          raise Botan::Error, 'Decryption requires an instance of PrivateKey'
        end
        ptr = FFI::MemoryPointer.new(:pointer)
        flags = 0
        Botan.call_ffi(:botan_pk_op_decrypt_create,
                       ptr, key.ptr, padding, flags)
        ptr = ptr.read_pointer
        if ptr.null?
          raise Botan::Error, 'botan_pk_op_decrypt_create returned NULL'
        end
        @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
      end

      # @api private
      def self.destroy(ptr)
        LibBotan.botan_pk_op_decrypt_destroy(ptr)
      end

      # Decrypts the provided data.
      #
      # @param msg [String] the data
      # @return [String] the decrypted data
      def decrypt(msg)
        msg_buf = FFI::MemoryPointer.from_data(msg)
        Botan.call_ffi_with_buffer(lambda { |b, bl|
          LibBotan.botan_pk_op_decrypt(@ptr, b, bl, msg_buf, msg_buf.size)
        })
      end

      def inspect
        Botan.inspect_ptr(self)
      end
    end # class
  end # module
end # module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
botan-0.1.2 lib/botan/pk/op/decrypt.rb
botan-0.1.0 lib/botan/pk/op/decrypt.rb