Class: EphemeralCalc::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/ephemeral_calc/encryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identity_key, rotation_scalar, initial_time = Time.now) ⇒ Encryptor

Returns a new instance of Encryptor.



10
11
12
13
14
15
16
17
18
19
# File 'lib/ephemeral_calc/encryptor.rb', line 10

def initialize(identity_key, rotation_scalar, initial_time = Time.now)
  if identity_key.size == 32
    # 32 characters means this is a 16-byte hex string
    self.identity_key = [identity_key].pack("H*")
  else
    self.identity_key = identity_key
  end
  self.rotation_scalar = rotation_scalar
  self.initial_time = initial_time.to_i
end

Instance Attribute Details

#identity_keyObject

Returns the value of attribute identity_key.



6
7
8
# File 'lib/ephemeral_calc/encryptor.rb', line 6

def identity_key
  @identity_key
end

#initial_timeObject

Returns the value of attribute initial_time.



8
9
10
# File 'lib/ephemeral_calc/encryptor.rb', line 8

def initial_time
  @initial_time
end

#rotation_scalarObject

Returns the value of attribute rotation_scalar.



7
8
9
# File 'lib/ephemeral_calc/encryptor.rb', line 7

def rotation_scalar
  @rotation_scalar
end

Instance Method Details

#beacon_timeObject



21
22
23
# File 'lib/ephemeral_calc/encryptor.rb', line 21

def beacon_time
  Time.now.to_i - self.initial_time
end

#each_identifierObject

yields the current EID and each subsuquent EID, until the block returns :stop



42
43
44
45
46
47
48
49
50
51
# File 'lib/ephemeral_calc/encryptor.rb', line 42

def each_identifier
  last_quantum = nil
  loop do
    if quantum != last_quantum
      last_quantum = quantum
      break if :stop == yield( get_identifier )
    end
    sleep 1
  end
end

#get_identifier(beacon_time = nil) ⇒ Object

Output is an 8-byte encrypted identifier as a hex string e.g. “0102030405060708”



31
32
33
34
35
36
37
38
39
# File 'lib/ephemeral_calc/encryptor.rb', line 31

def get_identifier(beacon_time = nil)
  beacon_time ||= self.beacon_time
  return nil if beacon_time < 0
  temporary_key = do_aes_encryption(self.identity_key, key_generation_data_block(beacon_time))
  encrypted_data = do_aes_encryption(temporary_key, data_block(beacon_time)).bytes.to_a
  # the identifier is the first 8 bytes of the encrypted output
  identifier_array = encrypted_data[0,8]
  identifier_array.map{|b| sprintf("%02X",b)}.join
end

#quantumObject



25
26
27
# File 'lib/ephemeral_calc/encryptor.rb', line 25

def quantum
  beacon_time / (2**rotation_scalar)
end