Class: Botan::PK::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/botan/pk/publickey.rb

Overview

Public Key

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ PublicKey

Returns a new instance of PublicKey.

Raises:



20
21
22
23
# 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

Instance Attribute Details

#ptrObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/botan/pk/publickey.rb', line 19

def ptr
  @ptr
end

Class Method Details

.destroy(ptr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/botan/pk/publickey.rb', line 26

def self.destroy(ptr)
  LibBotan.botan_pubkey_destroy(ptr)
end

.from_data(data) ⇒ Botan::PK::PublicKey

Creates a Botan::PK::PublicKey from BER/PEM data.

Parameters:

  • data (String)

    the public key data in a supported format

Returns:



34
35
36
37
38
39
# 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

Instance Method Details

#algoString

Returns the public-key algorithm name.

Returns:

  • (String)


53
54
55
56
57
# 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) ⇒ String

Encrypts data using the key.

Parameters:

  • data (String)

    the data to encrypt

  • padding (String) (defaults to: nil)

    the padding method to use

  • rng (Botan::RNG) (defaults to: Botan::RNG.new)

    the RNG to use

Returns:

  • (String)

    the encrypted data



130
131
132
133
# 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_strengthInteger

Returns the estimated strength of the key.

Returns:

  • (Integer)


44
45
46
47
48
# 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_derString

Returns the DER-encoded public key.

Returns:

  • (String)


69
70
71
# File 'lib/botan/pk/publickey.rb', line 69

def export_der
  export(pem: false)
end

#export_pemString

Returns the PEM-encoded public key.

Returns:

  • (String)


62
63
64
# File 'lib/botan/pk/publickey.rb', line 62

def export_pem
  export(pem: true)
end

#fingerprint(hash = 'SHA-256') ⇒ String

Returns the fingerprint of the key.

Parameters:

  • hash (String) (defaults to: 'SHA-256')

    the hash algorithm to use for the calculation

Returns:

  • (String)


81
82
83
84
85
86
87
88
# 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) ⇒ Integer

Retrieves a field of key material.

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

Parameters:

  • field (String)

    the name of the field to retrieve

Returns:

  • (Integer)


110
111
112
113
114
115
116
117
118
119
120
121
122
# 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

#inspectObject



146
147
148
# File 'lib/botan/pk/publickey.rb', line 146

def inspect
  Botan.inspect_ptr(self)
end

#to_sObject



73
74
75
# File 'lib/botan/pk/publickey.rb', line 73

def to_s
  export_pem
end

#valid?(rng = nil, thorough = false) ⇒ Boolean

Checks whether the key appears to be valid.

Parameters:

  • rng (Botan::RNG) (defaults to: nil)

    the RNG to use

  • thorough (Boolean) (defaults to: false)

    whether to perform more thorough checks that may be slower

Returns:

  • (Boolean)

    true if the key appears to be valid



96
97
98
99
100
101
# 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) ⇒ Boolean

Verifies a signature using the key.

Parameters:

  • data (String)

    the signature data to verify

  • padding (String) (defaults to: nil)

    the padding method

Returns:

  • (Boolean)

    true if the signature is valid



140
141
142
143
144
# 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