Class: Cryptorecord::Openpgpkeys
- Inherits:
-
Object
- Object
- Cryptorecord::Openpgpkeys
- Defined in:
- lib/cryptorecord/openpgpkeys.rb
Overview
Cryptorecord::Openpgpkeys-class generates openphpkeys-dns-records. Instances must have an uid. The PGP-Key can be read from file
Instance Attribute Summary collapse
-
#key ⇒ String
The pgp-key as a string.
-
#uid ⇒ Mail::Address
The userid or nil.
Instance Method Summary collapse
-
#domain ⇒ String
This getter returns the domain-part of the uid(email-address) or nil.
-
#initialize(args = {}) ⇒ Openpgpkeys
constructor
This constructor initializes uid and key by calling the setters.
-
#localpart ⇒ String
This getter returns the SHA256sum of the uid-local-part(email-address) as defined in rfc7929.
-
#read_file(keyfile) ⇒ Object
This method reads the pgp-key from a given file.
-
#to_s ⇒ String
This method concats the openpgpkey-record.
Constructor Details
#initialize(args = {}) ⇒ Openpgpkeys
This constructor initializes uid and key by calling the setters.
42 43 44 45 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 42 def initialize(args = {}) self.uid = args.fetch(:uid, nil) self.key = args.fetch(:key, nil) end |
Instance Attribute Details
#key ⇒ String
Returns the pgp-key as a string.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 33 class Openpgpkeys attr_reader :uid, :key # This constructor initializes uid and key by calling the setters. # @see uid= # # @param [Hash] args the options to initialize the object with # @option args [String] uid email-address associated with the pgp-key # @option args [String] key pgp-key def initialize(args = {}) self.uid = args.fetch(:uid, nil) self.key = args.fetch(:key, nil) end # This setter takes the argument val to create a Mail::Address-object. # The argument val can be a email-address-string or a Mail::Address-object. # Make sure this is the proper uid for the pgp-key! # # @param [String|Mail::Address] val The email-address without brackets # @raise Cryptorecord::ArgumentError def uid=(val) if val.nil? @uid = nil return end case val when String @uid = Mail::Address.new("<#{val}>") when Mail::Address @uid = Mail::Address.new("<#{val.address}>") else raise Cryptorecord::ArgumentError, "Unsupported datatype #{val.class} for val" end end # This getter returns the SHA256sum of the # uid-local-part(email-address) as defined # in rfc7929 # # @return [String] the local-part of the keys # uid(email-address) as SHA256 reduced to 56bytes or nil def localpart @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55] end # This getter returns the domain-part of the uid(email-address) or nil # # @return [String] domain the domain-part of the keys uid(email-address) def domain @uid.nil? ? nil : @uid.domain end # This method sets the pgp-key. It takes the public-key-block # and trims the header, blankline and checksum # # @param [String] val PGP-Public-Key-Block(ASCII Armor) # as defined in rfc4880 section 6.2 def key=(val) return if val.nil? @key = '' val.split(/\n/).each do |x| @key += trimpgpkey(x).to_s end @key = @key.gsub(/=.{4}$/, '') end # This method reads the pgp-key from a given file # # @param [String] keyfile Path to the keyfile # @raise Cryptorecord::ArgumentError def read_file(keyfile) raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil? data = File.read(keyfile) self.key = data end # This method concats the openpgpkey-record # # @return [String] openpgpkey dns-record as defined in rfc7929 def to_s "#{localpart}._openpgpkey.#{domain}. IN OPENPGPKEY #{@key}" end private # This function trims the pgpkey so that all headers, footers, # blanklines, and stuff # are gone # # @param [String] val onne line of the pgpkey-block # # @return An empty string if something has to be trimmed, # otherwise the line itself def trimpgpkey(val) case val when '-----BEGIN PGP PUBLIC KEY BLOCK-----' '' when '-----END PGP PUBLIC KEY BLOCK-----' '' when /^\s*\n$/ '' else val.to_s end end end |
#uid ⇒ Mail::Address
Returns the userid or nil.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 33 class Openpgpkeys attr_reader :uid, :key # This constructor initializes uid and key by calling the setters. # @see uid= # # @param [Hash] args the options to initialize the object with # @option args [String] uid email-address associated with the pgp-key # @option args [String] key pgp-key def initialize(args = {}) self.uid = args.fetch(:uid, nil) self.key = args.fetch(:key, nil) end # This setter takes the argument val to create a Mail::Address-object. # The argument val can be a email-address-string or a Mail::Address-object. # Make sure this is the proper uid for the pgp-key! # # @param [String|Mail::Address] val The email-address without brackets # @raise Cryptorecord::ArgumentError def uid=(val) if val.nil? @uid = nil return end case val when String @uid = Mail::Address.new("<#{val}>") when Mail::Address @uid = Mail::Address.new("<#{val.address}>") else raise Cryptorecord::ArgumentError, "Unsupported datatype #{val.class} for val" end end # This getter returns the SHA256sum of the # uid-local-part(email-address) as defined # in rfc7929 # # @return [String] the local-part of the keys # uid(email-address) as SHA256 reduced to 56bytes or nil def localpart @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55] end # This getter returns the domain-part of the uid(email-address) or nil # # @return [String] domain the domain-part of the keys uid(email-address) def domain @uid.nil? ? nil : @uid.domain end # This method sets the pgp-key. It takes the public-key-block # and trims the header, blankline and checksum # # @param [String] val PGP-Public-Key-Block(ASCII Armor) # as defined in rfc4880 section 6.2 def key=(val) return if val.nil? @key = '' val.split(/\n/).each do |x| @key += trimpgpkey(x).to_s end @key = @key.gsub(/=.{4}$/, '') end # This method reads the pgp-key from a given file # # @param [String] keyfile Path to the keyfile # @raise Cryptorecord::ArgumentError def read_file(keyfile) raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil? data = File.read(keyfile) self.key = data end # This method concats the openpgpkey-record # # @return [String] openpgpkey dns-record as defined in rfc7929 def to_s "#{localpart}._openpgpkey.#{domain}. IN OPENPGPKEY #{@key}" end private # This function trims the pgpkey so that all headers, footers, # blanklines, and stuff # are gone # # @param [String] val onne line of the pgpkey-block # # @return An empty string if something has to be trimmed, # otherwise the line itself def trimpgpkey(val) case val when '-----BEGIN PGP PUBLIC KEY BLOCK-----' '' when '-----END PGP PUBLIC KEY BLOCK-----' '' when /^\s*\n$/ '' else val.to_s end end end |
Instance Method Details
#domain ⇒ String
This getter returns the domain-part of the uid(email-address) or nil
83 84 85 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 83 def domain @uid.nil? ? nil : @uid.domain end |
#localpart ⇒ String
This getter returns the SHA256sum of the uid-local-part(email-address) as defined in rfc7929
76 77 78 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 76 def localpart @uid.nil? ? nil : OpenSSL::Digest::SHA256.new(@uid.local.to_s).to_s[0..55] end |
#read_file(keyfile) ⇒ Object
This method reads the pgp-key from a given file
106 107 108 109 110 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 106 def read_file(keyfile) raise Cryptorecord::ArgumentError, 'No keyfile defined' if keyfile.nil? data = File.read(keyfile) self.key = data end |
#to_s ⇒ String
This method concats the openpgpkey-record
115 116 117 |
# File 'lib/cryptorecord/openpgpkeys.rb', line 115 def to_s "#{localpart}._openpgpkey.#{domain}. IN OPENPGPKEY #{@key}" end |