class Botan::PK::PublicKey

Public Key

Attributes

ptr[R]

@api private

Public Class Methods

destroy(ptr) click to toggle source

@api private

# File lib/botan/pk/publickey.rb, line 26
def self.destroy(ptr)
  LibBotan.botan_pubkey_destroy(ptr)
end
from_data(data) click to toggle source

Creates a {PublicKey} from BER/PEM data.

@param data [String] the public key data in a supported format @return [Botan::PK::PublicKey]

# File lib/botan/pk/publickey.rb, line 34
def self.from_data(data)
  ptr = FFI::MemoryPointer.new(:pointer)
  buf = FFI::MemoryPointer.from_data(data)
  Botan.call_ffi(:botan_pubkey_load, ptr, buf, buf.size)
  PublicKey.new(ptr.read_pointer)
end
new(ptr) click to toggle source
# File lib/botan/pk/publickey.rb, line 20
def initialize(ptr)
  raise Botan::Error, 'PublicKey received a NULL pointer' if ptr.null?
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
end

Public Instance Methods

algo() click to toggle source

Returns the public-key algorithm name.

@return [String]

# File lib/botan/pk/publickey.rb, line 53
def algo
  Botan.call_ffi_with_buffer(lambda { |b, bl|
    LibBotan.botan_pubkey_algo_name(@ptr, b, bl)
  }, guess: 32, string: true)
end
encrypt(data, padding: nil, rng: Botan::RNG.new) click to toggle source

Encrypts data using the key.

@param data [String] the data to encrypt @param padding [String] the padding method to use @param rng [Botan::RNG] the RNG to use @return [String] the encrypted data

# File lib/botan/pk/publickey.rb, line 130
def encrypt(data, padding: nil, rng: Botan::RNG.new)
  enc = Botan::PK::Encrypt.new(key: self, padding: padding)
  enc.encrypt(data, rng: rng)
end
estimated_strength() click to toggle source

Returns the estimated strength of the key.

@return [Integer]

# File lib/botan/pk/publickey.rb, line 44
def estimated_strength
  strength_ptr = FFI::MemoryPointer.new(:size_t)
  Botan.call_ffi(:botan_pubkey_estimated_strength, @ptr, strength_ptr)
  strength_ptr.read(:size_t)
end
export_der() click to toggle source

Returns the DER-encoded public key.

@return [String]

# File lib/botan/pk/publickey.rb, line 69
def export_der
  export(pem: false)
end
export_pem() click to toggle source

Returns the PEM-encoded public key.

@return [String]

# File lib/botan/pk/publickey.rb, line 62
def export_pem
  export(pem: true)
end
fingerprint(hash = 'SHA-256') click to toggle source

Returns the fingerprint of the key.

@param hash [String] the hash algorithm to use for the calculation @return [String]

# File lib/botan/pk/publickey.rb, line 81
def fingerprint(hash = 'SHA-256')
  n = Botan::Digest.new(hash).length
  buf = FFI::MemoryPointer.new(:uint8, n)
  buf_len_ptr = FFI::MemoryPointer.new(:size_t)
  buf_len_ptr.write(:size_t, n)
  Botan.call_ffi(:botan_pubkey_fingerprint, @ptr, hash, buf, buf_len_ptr)
  buf.read_bytes(buf_len_ptr.read(:size_t))
end
get_field(field) click to toggle source

Retrieves a field of key material.

For example, the 'e' field of an RSA key might return the value 0x1001.

@param field [String] the name of the field to retrieve @return [Integer]

# File lib/botan/pk/publickey.rb, line 110
def get_field(field)
  mp = nil
  mp_ptr = FFI::MemoryPointer.new(:pointer)
  Botan.call_ffi(:botan_mp_init, mp_ptr)
  mp = mp_ptr.read_pointer
  Botan.call_ffi(:botan_pubkey_get_field, mp, @ptr, field)
  hex_str = Botan.call_ffi_with_buffer(lambda { |b, bl|
    LibBotan.botan_mp_to_str(mp, 16, b, bl)
  }, string: true)
  hex_str.hex
ensure
  LibBotan.botan_mp_destroy(mp) if mp && !mp.null?
end
inspect() click to toggle source
# File lib/botan/pk/publickey.rb, line 146
def inspect
  Botan.inspect_ptr(self)
end
to_s() click to toggle source
# File lib/botan/pk/publickey.rb, line 73
def to_s
  export_pem
end
valid?(rng = nil, thorough = false) click to toggle source

Checks whether the key appears to be valid.

@param rng [Botan::RNG] the RNG to use @param thorough [Boolean] whether to perform more thorough checks

that may be slower

@return [Boolean] true if the key appears to be valid

# File lib/botan/pk/publickey.rb, line 96
def valid?(rng = nil, thorough = false)
  rng ||= Botan::RNG.new
  flags = thorough ? 1 : 0
  rc = LibBotan.botan_pubkey_check_key(@ptr, rng.ptr, flags)
  rc.zero?
end
verify(data:, signature:, padding: nil) click to toggle source

Verifies a signature using the key.

@param data [String] the signature data to verify @param padding [String] the padding method @return [Boolean] true if the signature is valid

# File lib/botan/pk/publickey.rb, line 140
def verify(data:, signature:, padding: nil)
  verify = Botan::PK::Verify.new(key: self, padding: padding)
  verify << data
  verify.check_signature(signature)
end

Private Instance Methods

export(pem:) click to toggle source
# File lib/botan/pk/publickey.rb, line 152
def export(pem:)
  flags = pem ? 1 : 0
  Botan.call_ffi_with_buffer(lambda { |b, bl|
    LibBotan.botan_pubkey_export(@ptr, b, bl, flags)
  }, string: pem)
end