Class: Pnthr::Security
- Inherits:
-
Object
- Object
- Pnthr::Security
- Defined in:
- lib/pnthr.rb
Instance Attribute Summary collapse
-
#cipher ⇒ Object
Returns the value of attribute cipher.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
-
#decrypt(data, key = nil, iv = nil) ⇒ Object
Decrypt - Simple AES decryption.
-
#encrypt(data, key = nil, iv = nil) ⇒ Object
Encrypt - Simple AES encryption.
-
#initialize(id, secret, options = {}) ⇒ Security
constructor
A new instance of Security.
-
#roar(payload) ⇒ Object
Roar - Encrypts the payload, makes the request and returns the response.
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, = {}) @cipher = OpenSSL::Cipher::AES.new(secret.length * 8, :CFB) [:url] ||= 'https://pnthr-api.herokuapp.com/' [:ssl] = [:ssl].nil? ? true : [:ssl] [:iv] ||= Base64.encode64(rand.to_s)[0..15] @request = { url: [:url], uri: URI.parse([:url]), id: id, iv: [:iv], secret: secret, ssl: [:ssl] } end |
Instance Attribute Details
#cipher ⇒ Object
Returns the value of attribute cipher.
10 11 12 |
# File 'lib/pnthr.rb', line 10 def cipher @cipher end |
#request ⇒ Object
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 |