Class: Evervault::Crypto::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/evervault/crypto/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_handler:, config: ::Evervault::Config.new) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
# File 'lib/evervault/crypto/client.rb', line 18

def initialize(request_handler:, config: ::Evervault::Config.new)
  @config = config
  @p256 = Evervault::Crypto::Curves::P256.new
  @koblitz = Evervault::Crypto::Curves::Koblitz.new
  @ev_version = EV_VERSION[config.curve]
  response = request_handler.get('cages/key')
  key = config.curve == 'secp256k1' ? 'ecdhKey' : 'ecdhP256Key'
  @team_key = response[key]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/evervault/crypto/client.rb', line 16

def config
  @config
end

Instance Method Details

#encrypt(data, role = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/evervault/crypto/client.rb', line 28

def encrypt(data, role = nil)
  if data.nil? || (data.instance_of?(String) && data.empty?)
    raise Evervault::Errors::EvervaultError, 'Data is required for encryption'
  end

  unless encryptable_data?(data) || data.instance_of?(Hash) || data.instance_of?(Array)
    raise Evervault::Errors::EvervaultError, "Encryption is not supported for #{data.class}"
  end

  traverse_and_encrypt(data, role)
end

#generate_metadata(role) ⇒ Object



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
# File 'lib/evervault/crypto/client.rb', line 40

def (role)
  buffer = []

  # Binary representation of a fixed map with 2 or 3 items, followed by the key-value pairs
  buffer.push(0x80 | (role.nil? ? 2 : 3))

  if role
    # Binary representation for a fixed string of length 2, followed by `dr` (for "data role")
    buffer.push(0xA2)
    buffer.push(*'dr'.bytes)
    # Binary representation for a fixed string of role name length, followed by the role name itself
    buffer.push(0xA0 | role.length)
    buffer.push(*role.bytes)
  end

  # Binary representation for a fixed string of length 2, followed by `eo` (for "encryption origin")
  buffer.push(0xA2)
  buffer.push(*'eo'.bytes)
  # Binary representation for the integer 8 (Ruby SDK)
  buffer.push(8)

  # Binary representation for a fixed string of length 2, followed by `et` (for "encryption timestamp")
  buffer.push(0xA2)
  buffer.push(*'et'.bytes)
  # Binary representation for a 4-byte unsigned integer (uint 32), followed by the epoch time (big-endian)
  buffer.push(0xCE)
  buffer.push(*[Time.now.to_i].pack('I!>').bytes)

  buffer.pack('C*')
end