Class: PKCS7::Cryptographer::Entity

Inherits:
Object
  • Object
show all
Includes:
Initializers
Defined in:
lib/pkcs7/cryptographer/entity.rb

Overview

Define an entity abel to decrypt or encrypt messages to send them to other entities. It uses a Cryptographer to do the dirty work and just provide a more human readable way to read an pass messages between trustable entities.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(certificate:, key: nil, ca_store: OpenSSL::X509::Store.new) ⇒ Entity

PUBLIC METHODS




20
21
22
23
24
25
26
27
28
29
# File 'lib/pkcs7/cryptographer/entity.rb', line 20

def initialize(
  certificate:,
  key: nil,
  ca_store: OpenSSL::X509::Store.new
)
  @key = key ? rsa_key(key) : nil
  @certificate = x509_certificate(certificate)
  @cryptographer = PKCS7::Cryptographer.new
  @ca_store = ca_store
end

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate.



16
17
18
# File 'lib/pkcs7/cryptographer/entity.rb', line 16

def certificate
  @certificate
end

Instance Method Details

#decrypt_data(data:, sender:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pkcs7/cryptographer/entity.rb', line 46

def decrypt_data(data:, sender:)
  perform_safely(sender) do
    @cryptographer.decrypt_and_verify(
      data: data,
      key: @key,
      certificate: @certificate,
      public_certificate: sender.certificate,
      ca_store: @ca_store
    )
  end
end

#encrypt_data(data:, receiver:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/pkcs7/cryptographer/entity.rb', line 35

def encrypt_data(data:, receiver:)
  perform_safely(receiver) do
    @cryptographer.sign_and_encrypt(
      data: data,
      key: @key,
      certificate: @certificate,
      public_certificate: receiver.certificate
    )
  end
end

#trustable_entity?(entity) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/pkcs7/cryptographer/entity.rb', line 31

def trustable_entity?(entity)
  @ca_store.verify(entity.certificate)
end