Class: Pnthr::Security

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, secret, options = {}) ⇒ Security

Returns a new instance of Security.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pnthr.rb', line 12

def initialize(id, secret, options = {})
  @cipher = OpenSSL::Cipher::AES.new(secret.length * 8, :CFB)

  options[:url] ||= 'https://pnthr-api.herokuapp.com/'
  options[:ssl] = options[:ssl].nil? ? true : options[:ssl]
  options[:iv] ||= Base64.encode64(rand.to_s)[0..15]

  @request = {
    url: options[:url],
    uri: URI.parse(options[:url]),
    id: id,
    iv: options[:iv],
    secret: secret,
    ssl: options[:ssl]
  }
end

Instance Attribute Details

#cipherObject

Returns the value of attribute cipher.



10
11
12
# File 'lib/pnthr.rb', line 10

def cipher
  @cipher
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/pnthr.rb', line 10

def request
  @request
end

Instance Method Details

#decrypt(data, key = nil, iv = nil) ⇒ Object

Decrypt - Simple AES decryption

+ Needs to retrieve IV from the first layer



61
62
63
64
65
66
67
68
69
70
# File 'lib/pnthr.rb', line 61

def decrypt(data, key = nil, iv = nil)
  key ||= @request[:secret]
  iv ||= @request[:iv]

  @cipher.decrypt
  @cipher.key = key
  @cipher.iv = iv

  @cipher.update(Base64.decode64(data))
end

#encrypt(data, key = nil, iv = nil) ⇒ Object

Encrypt - Simple AES encryption

  • a variable length key is used for greatest flexibility

  • CFB is used

+ Needs HMAC + Needs variable IV to be passed with request



45
46
47
48
49
50
51
52
53
54
# File 'lib/pnthr.rb', line 45

def encrypt(data, key = nil, iv = nil)
  key ||= @request[:secret]
  iv ||= @request[:iv]

  @cipher.encrypt
  @cipher.key = key
  @cipher.iv = iv

  @cipher.update(data)
end

#roar(payload) ⇒ Object

Roar - Encrypts the payload, makes the request and returns the response



32
33
34
# File 'lib/pnthr.rb', line 32

def roar(payload)
  make_request(encrypt(payload))
end