Sha256: 3f46bd7f2d5f6f3733cfa5c30d114612245e68bde22eecb3b1de9392e456a87b

Contents?: true

Size: 970 Bytes

Versions: 3

Compression:

Stored size: 970 Bytes

Contents

module TwitterAuth
  module Cryptify
    class Error < StandardError; end
    
    def self.encrypt(data)
      salt = generate_salt
      key = EzCrypto::Key.with_password(TwitterAuth.encryption_key, salt)
      {:encrypted_data => key.encrypt64(data), :salt => salt}
    end
 
    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
  
    def self.generate_salt
      ActiveSupport::SecureRandom.hex(4)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twitter-auth-with-mongo-mapper-0.1.1 lib/twitter_auth/cryptify.rb
twitter-auth-with-mongo-mapper-0.1.0 lib/twitter_auth/cryptify.rb
twitter-auth-with-mongo-mapper-0.0.9 lib/twitter_auth/cryptify.rb