Module: TwitterAuth::Cryptify

Defined in:
lib/twitter_auth/cryptify.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.decrypt(encrypted_data_or_hash, salt = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/twitter_auth/cryptify.rb', line 11

def self.decrypt(encrypted_data_or_hash, salt=nil)
  case encrypted_data_or_hash
  when String
    encrypted_data = encrypted_data_or_hash
    raise ArgumentError, 'Must provide a salt to decrypt.' unless salt
  when Hash
    encrypted_data = encrypted_data_or_hash[:encrypted_data]
    salt = encrypted_data_or_hash[:salt]
  else
    raise ArgumentError, 'Must provide either an encrypted hash result or encrypted string and salt.'
  end
  key = EzCrypto::Key.with_password(TwitterAuth.encryption_key, salt)
  key.decrypt64(encrypted_data)
end

.encrypt(data) ⇒ Object



5
6
7
8
9
# File 'lib/twitter_auth/cryptify.rb', line 5

def self.encrypt(data)
  salt = generate_salt
  key = EzCrypto::Key.with_password(TwitterAuth.encryption_key, salt)
  {:encrypted_data => key.encrypt64(data), :salt => salt}
end

.generate_saltObject



26
27
28
# File 'lib/twitter_auth/cryptify.rb', line 26

def self.generate_salt
  ActiveSupport::SecureRandom.hex(4)
end