Class: PGP::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/pgp.rb

Overview

Public Key class provides an native extension representation for working with PGP public keys.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parsePublicKey

Parses a PGP public key from a given string input.

Parameters:

  • input (String)

    the PGP public key in string format.

Returns:

  • (PublicKey)

    an instance of PublicKey if parsing is successful.



# File 'lib/pgp.rb', line 50

Instance Method Details

#algorithmInteger

Returns the algorithm used by the public key.

Returns:

  • (Integer)

    the algorithm identifier.



# File 'lib/pgp.rb', line 59

#algorithm_nameString

Fetches the name of the algorithm used by the public key from a predefined list of names.

Returns:

  • (String)

    the name of the algorithm.



97
98
99
# File 'lib/pgp.rb', line 97

def algorithm_name
  KEY_ALGORITHM_NAMES.fetch(algorithm, 'Unknown')
end

#created_atTime

Returns the creation time of the public key.

Returns:

  • (Time)

    the creation time of the public key.



# File 'lib/pgp.rb', line 75

#encrypt(data, algorithm = ENCRIPTION_ALGORITHM_AES_128) ⇒ String

Encrypts data using the specified encryption algorithm.

Parameters:

  • data (String)

    the data to be encrypted.

  • algorithm (Integer) (defaults to: ENCRIPTION_ALGORITHM_AES_128)

    the encryption algorithm to use, defaults to AES-128.

Returns:

  • (String)

    the encrypted data encoded by base64.



113
114
115
# File 'lib/pgp.rb', line 113

def encrypt(data, algorithm = ENCRIPTION_ALGORITHM_AES_128)
  encrypt_with_algorithm(data, algorithm)
end

#encrypt_with_algorithm(input, algorithm) ⇒ String

Encrypts data with the specified algorithm.

Parameters:

  • input (String)

    the data to be encrypted.

  • algorithm (Integer)

    the encryption algorithm identifier.

Returns:

  • (String)

    the encrypted data encoded by base64.



# File 'lib/pgp.rb', line 83

#encryption_supported?Boolean

Checks if the public key supports encryption.

Returns:

  • (Boolean)

    true if the key supports encryption, false otherwise.



# File 'lib/pgp.rb', line 67

#expired?Boolean

Checks whether the public key has expired.

Returns:

  • (Boolean)

    true if the key has expired, false otherwise.



103
104
105
106
107
# File 'lib/pgp.rb', line 103

def expired?
  return false if expires_at.nil?

  expires_at.to_i <= Time.now.to_i
end

#expires_atTime?

Returns the expiration time of the public key, if any.

Returns:

  • (Time, nil)

    the expiration time of the public key or nil if it does not expire.



# File 'lib/pgp.rb', line 79

#fingerprintString

Returns the fingerprint of the public key.

Returns:

  • (String)

    the fingerprint of the public key.



# File 'lib/pgp.rb', line 55

#inspectString

Returns a string representation of the PublicKey object, including its fingerprint, algorithm name, and version.

Returns:

  • (String)

    the string representation of the PublicKey object.



91
92
93
# File 'lib/pgp.rb', line 91

def inspect
  "#<#{self.class} #{fingerprint} #{algorithm_name} v#{version}>"
end

#signing_supported?Boolean

Checks if the public key supports signing.

Returns:

  • (Boolean)

    true if the key supports signing, false otherwise.



# File 'lib/pgp.rb', line 63

#versionInteger

Returns the version of the public key.

Returns:

  • (Integer)

    the version of the public key.



# File 'lib/pgp.rb', line 71