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



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

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

  EzCrypto::Key.decrypt_with_password(TwitterAuth.encryption_key, salt, encrypted_data)
end

.encrypt(data) ⇒ Object



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

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

.generate_saltObject



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

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