Class: NewRelic::Agent::Obfuscator

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/obfuscator.rb

Constant Summary collapse

EMPTY_KEY_BYTES =
[0]
PACK_FORMAT =
'm'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, length = nil) ⇒ Obfuscator

RUM uses a shortened key, so just trim it up front



16
17
18
19
20
21
22
23
# File 'lib/new_relic/agent/obfuscator.rb', line 16

def initialize(key, length = nil)
  if key.nil? || key.empty?
    @key_bytes = EMPTY_KEY_BYTES
  else
    @key_bytes = key.bytes.to_a
    @key_bytes = @key_bytes.first(length) if length
  end
end

Instance Attribute Details

#key_bytesObject (readonly)



10
11
12
# File 'lib/new_relic/agent/obfuscator.rb', line 10

def key_bytes
  @key_bytes
end

Instance Method Details

#deobfuscate(text) ⇒ Object



29
30
31
# File 'lib/new_relic/agent/obfuscator.rb', line 29

def deobfuscate(text)
  encode(text.unpack(PACK_FORMAT).first)
end

#encode(text) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/new_relic/agent/obfuscator.rb', line 33

def encode(text)
  return text unless key_bytes

  encoded = +''
  encoded.force_encoding('binary') if encoded.respond_to?(:force_encoding)
  index = 0
  text.each_byte do |byte|
    encoded.concat((byte ^ key_bytes[index % key_bytes.length]))
    index += 1
  end
  encoded
end

#obfuscate(text) ⇒ Object



25
26
27
# File 'lib/new_relic/agent/obfuscator.rb', line 25

def obfuscate(text)
  [encode(text)].pack(PACK_FORMAT).delete("\n")
end