Class: Pnthr::Security

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

Overview

Everything lives in the security class for now

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Security.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pnthr.rb', line 20

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.



18
19
20
# File 'lib/pnthr.rb', line 18

def cipher
  @cipher
end

#requestObject

Returns the value of attribute request.



18
19
20
# File 'lib/pnthr.rb', line 18

def request
  @request
end

Instance Method Details

#cage(payload) ⇒ Object

Cage - Will make our payload without sending



49
50
51
# File 'lib/pnthr.rb', line 49

def cage(payload)
  Base64.encode64(encrypt(payload)).strip! + "-" + @request[:iv]
end

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

Decrypt - Simple AES decryption



85
86
87
88
89
90
91
92
93
94
# File 'lib/pnthr.rb', line 85

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

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

  @cipher.update(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



71
72
73
74
75
76
77
78
79
80
# File 'lib/pnthr.rb', line 71

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

#release(payload, password) ⇒ Object

Release - Will fully decrypt a payload to raw text



56
57
58
59
60
61
# File 'lib/pnthr.rb', line 56

def release(payload, password)
  part = payload.split('-')

  level1 = decrypt(Base64.decode64(part[0]), @request[:secret], part[1])
  decrypt(level1, Digest::MD5.hexdigest(password), part[1])
end

#roar(payload) ⇒ Object

Encrypt the payload, makes the request and returns the response



40
41
42
43
44
# File 'lib/pnthr.rb', line 40

def roar(payload)
  https = Net::HTTP.new(@request[:uri].host, @request[:uri].port)
  https.use_ssl = @request[:ssl]
  https.post(@request[:uri].path, cage(payload), { 'pnthr' => @request[:id] })
end