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 46

Instance Method Details

#algorithmInteger

Returns the algorithm used by the public key.

Returns:

  • (Integer)

    the algorithm identifier.



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

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



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

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 71

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



109
110
111
# File 'lib/pgp.rb', line 109

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 79

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

#expired?Boolean

Checks whether the public key has expired.

Returns:

  • (Boolean)

    true if the key has expired, false otherwise.



99
100
101
102
103
# File 'lib/pgp.rb', line 99

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 75

#fingerprintString

Returns the fingerprint of the public key.

Returns:

  • (String)

    the fingerprint of the public key.



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

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



87
88
89
# File 'lib/pgp.rb', line 87

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 59

#versionInteger

Returns the version of the public key.

Returns:

  • (Integer)

    the version of the public key.



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