Module: Tokenifier::Decrypt

Included in:
Tokenifier
Defined in:
lib/tokenifier/decrypt.rb

Instance Method Summary collapse

Instance Method Details

#decrypt(token, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tokenifier/decrypt.rb', line 16

def decrypt(token, options = {})

  raise Error, "Encrypted data should be a String" unless token.is_a?(String)
  raise Error, "Got an incomlete encrypted string" if token.size < 12

  cipher(options[:secret]) do |c|
    unpack_string(c.dec(token))
  end

rescue OpenSSL::Cipher::CipherError => e
  raise Error, "Got a malformed string"
end

#unpack_string(data) ⇒ Object

data marshaling differs depending to ruby version so its better to pack hash into string accoring to some rule



6
7
8
9
10
11
12
13
14
# File 'lib/tokenifier/decrypt.rb', line 6

def unpack_string(data)
  case data
    when /[\:(\#)?]/
      hsh = data.split('#').map {|s| s.split(':')}.inject({}) {|c, (k, v)| c.merge({k => v})}
      hsh.respond_to?(:with_indifferent_access) ? hsh.with_indifferent_access : hsh
    else
      data
  end
end