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 51

Instance Method Details

#algorithmInteger

Returns the algorithm used by the public key.

Returns:

  • (Integer)

    the algorithm identifier.



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

#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.



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

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 76

#encrypt(data, algorithm = ENCRYPTION_ALGORITHM_AES_128) ⇒ String

Encrypts data using the specified encryption algorithm.

Parameters:

  • data (String)

    the data to be encrypted.

  • algorithm (Integer) (defaults to: ENCRYPTION_ALGORITHM_AES_128)

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

Returns:

  • (String)

    the encrypted data encoded by base64.



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

def encrypt(data, algorithm = ENCRYPTION_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 84

#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 68

#expired?Boolean

Checks whether the public key has expired.

Returns:

  • (Boolean)

    true if the key has expired, false otherwise.



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

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 80

#fingerprintString

Returns the fingerprint of the public key.

Returns:

  • (String)

    the fingerprint of the public key.



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

#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.



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

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 64

#versionInteger

Returns the version of the public key.

Returns:

  • (Integer)

    the version of the public key.



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