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



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

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)



8
9
10
# File 'lib/new_relic/agent/obfuscator.rb', line 8

def key_bytes
  @key_bytes
end

Instance Method Details

#deobfuscate(text) ⇒ Object



27
28
29
# File 'lib/new_relic/agent/obfuscator.rb', line 27

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

#encode(text) ⇒ Object



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

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



23
24
25
# File 'lib/new_relic/agent/obfuscator.rb', line 23

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