Class: OpenToken::Cipher

Inherits:
Object
  • Object
show all
Defined in:
lib/opentoken-newrelic-rails23/cipher.rb

Defined Under Namespace

Classes: InvalidCipherError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Cipher

Returns a new instance of Cipher.



12
13
14
15
16
17
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 12

def initialize(attrs = {})
  @suite = attrs[:suite]
  @iv_length = attrs[:iv_length]
  @key_length = attrs[:key_length]
  @algorithm = attrs[:algorithm]
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



7
8
9
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 7

def algorithm
  @algorithm
end

#iv_lengthObject (readonly)

Returns the value of attribute iv_length.



8
9
10
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 8

def iv_length
  @iv_length
end

#key_lengthObject (readonly)

Returns the value of attribute key_length.



9
10
11
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 9

def key_length
  @key_length
end

#suiteObject (readonly)

Returns the value of attribute suite.



10
11
12
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 10

def suite
  @suite
end

Class Method Details

.for_suite(cipher_suite) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 18

def self.for_suite(cipher_suite)
  cipher = REGISTERED_CIPHERS.detect {|c| c.suite == cipher_suite }
  raise InvalidCipherError.new("Unknown cipher suite: #{cipher_suite}") unless cipher
  cipher
end

Instance Method Details

#decrypt_payload(encrypted_payload, key, iv) ⇒ Object



34
35
36
37
38
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 34

def decrypt_payload(encrypted_payload, key, iv)
  return encrypted_payload unless algorithm
  c = crypt :decrypt, key, iv
  c.update(encrypted_payload) + c.final
end

#encrypt_payload(payload, key, iv) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 39

def encrypt_payload(payload, key, iv)
  c = crypt :encrypt, key, iv
  padding = if payload.length % iv_length == 0
    iv_length
  else
    iv_length - (payload.length % iv_length)
  end
  c.update(payload + (padding.chr * padding))
end

#generate_ivObject



27
28
29
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 27

def generate_iv
  OpenSSL::Random.random_bytes(iv_length)
end

#generate_keyObject



24
25
26
# File 'lib/opentoken-newrelic-rails23/cipher.rb', line 24

def generate_key
  OpenToken::PasswordKeyGenerator.generate OpenToken.password, self
end