Class: Xml::Kit::Fingerprint

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/kit/fingerprint.rb

Overview

This generates a fingerprint for an X509 Certificate.

certificate, _ = Xml::Kit::SelfSignedCertificate.new.create

puts Xml::Kit::Fingerprint.new(certificate).to_s
# B7:AB:DC:BD:4D:23:58:65:FD:1A:99:0C:5F:89:EA:87:AD:F1:D7:83:34:7A:E9:E4:88:12:DD:46:1F:38:05:93

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_certificate) ⇒ Fingerprint

Returns a new instance of Fingerprint.



17
18
19
# File 'lib/xml/kit/fingerprint.rb', line 17

def initialize(raw_certificate)
  @x509 = Certificate.to_x509(raw_certificate)
end

Instance Attribute Details

#x509Object (readonly)

The OpenSSL::X509::Certificate



15
16
17
# File 'lib/xml/kit/fingerprint.rb', line 15

def x509
  @x509
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
# File 'lib/xml/kit/fingerprint.rb', line 29

def ==(other)
  to_s == other.to_s
end

#algorithm(algorithm) ⇒ String

Generates a formatted fingerprint using the specified hash algorithm.

Parameters:

  • algorithm (OpenSSL::Digest)

    the openssl algorithm to use ‘OpenSSL::Digest::SHA256`, `OpenSSL::Digest::SHA1`.

Returns:

  • (String)

    in the format of ‘“BF:ED:C5:F1:6C:AB:F5:B2:15:1F:BF:BD:7D:68:1A:F9:A5:4E:4C:19:30:BC:6D:25:B1:8E:98:D4:23:FD:B4:09”`



25
26
27
# File 'lib/xml/kit/fingerprint.rb', line 25

def algorithm(algorithm)
  pretty_fingerprint(algorithm.new.hexdigest(x509.to_der))
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/xml/kit/fingerprint.rb', line 33

def eql?(other)
  self == other
end

#hashObject



37
38
39
# File 'lib/xml/kit/fingerprint.rb', line 37

def hash
  to_s.hash
end

#to_sObject



41
42
43
# File 'lib/xml/kit/fingerprint.rb', line 41

def to_s
  algorithm(OpenSSL::Digest::SHA256)
end