Class: Botan::PK::PublicKey
- Inherits:
-
Object
- Object
- Botan::PK::PublicKey
- Defined in:
- lib/botan/pk/publickey.rb
Overview
Public Key
Instance Attribute Summary collapse
- #ptr ⇒ Object readonly private
Class Method Summary collapse
- .destroy(ptr) ⇒ Object private
-
.from_data(data) ⇒ Botan::PK::PublicKey
Creates a PublicKey from BER/PEM data.
Instance Method Summary collapse
-
#algo ⇒ String
Returns the public-key algorithm name.
-
#encrypt(data, padding: nil, rng: Botan::RNG.new) ⇒ String
Encrypts data using the key.
-
#estimated_strength ⇒ Integer
Returns the estimated strength of the key.
-
#export_der ⇒ String
Returns the DER-encoded public key.
-
#export_pem ⇒ String
Returns the PEM-encoded public key.
-
#fingerprint(hash = 'SHA-256') ⇒ String
Returns the fingerprint of the key.
-
#get_field(field) ⇒ Integer
Retrieves a field of key material.
-
#initialize(ptr) ⇒ PublicKey
constructor
A new instance of PublicKey.
- #inspect ⇒ Object
- #to_s ⇒ Object
-
#valid?(rng = nil, thorough = false) ⇒ Boolean
Checks whether the key appears to be valid.
-
#verify(data:, signature:, padding: nil) ⇒ Boolean
Verifies a signature using the key.
Constructor Details
Instance Attribute Details
#ptr ⇒ Object (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.
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
#algo ⇒ String
Returns the public-key algorithm name.
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.
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_strength ⇒ Integer
Returns the estimated strength of the key.
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_der ⇒ String
Returns the DER-encoded public key.
69 70 71 |
# File 'lib/botan/pk/publickey.rb', line 69 def export_der export(pem: false) end |
#export_pem ⇒ String
Returns the PEM-encoded public key.
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.
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.
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 |
#inspect ⇒ Object
146 147 148 |
# File 'lib/botan/pk/publickey.rb', line 146 def inspect Botan.inspect_ptr(self) end |
#to_s ⇒ Object
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.
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.
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 |