Class: TextSmuggler

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

Constant Summary collapse

DEFAULT_CIPHER_TYPE =
'aes-256-cbc'
DEFAULT_KEY =
'0123456789abcdef0123456789abcdef'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TextSmuggler

Returns a new instance of TextSmuggler.



9
10
11
12
13
14
# File 'lib/text_smuggler.rb', line 9

def initialize opts={}
  @opts = {
    cipher_type: DEFAULT_CIPHER_TYPE, 
    key: (ENV['TEXT_SMUGGLER_KEY'] || DEFAULT_KEY)
  }.merge(opts)
end

Instance Method Details

#decode(s) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/text_smuggler.rb', line 24

def decode s
  cipher = cipher_for :decrypt
  encrypted = from_url_friendly s
  cipher.iv = encrypted.slice!(0,16)
  decrypted = cipher.update(encrypted) + cipher.final
  decrypted
end

#encode(s) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/text_smuggler.rb', line 16

def encode s
  cipher = cipher_for :encrypt
  iv = cipher.random_iv
  encrypted = cipher.update(s) + cipher.final
  encrypted = iv + encrypted
  url_friendly(encrypted)
end