Sha256: 7007b06866e5e6329c00f7afd14165a4c5e24df9669223d1745d597bfa9b59ed

Contents?: true

Size: 1.76 KB

Versions: 67

Compression:

Stored size: 1.76 KB

Contents

require "openssl"

module Authlogic
  module CryptoProviders
    # This encryption method is reversible if you have the supplied key. So in order to use this encryption method you must supply it with a key first.
    # In an initializer, or before your application initializes, you should do the following:
    #
    #   Authlogic::CryptoProviders::AES256.key = "my really long and unique key, preferrably a bunch of random characters"
    #
    # My final comment is that this is a strong encryption method, but its main weakness is that its reversible. If you do not need to reverse the hash
    # then you should consider Sha512 or BCrypt instead.
    #
    # Keep your key in a safe place, some even say the key should be stored on a separate server.
    # This won't hurt performance because the only time it will try and access the key on the separate server is during initialization, which only
    # happens once. The reasoning behind this is if someone does compromise your server they won't have the key also. Basically, you don't want to
    # store the key with the lock.
    class AES256
      class << self
        attr_writer :key
    
        def encrypt(*tokens)
          aes.encrypt
          aes.key = @key
          [aes.update(tokens.join) + aes.final].pack("m").chomp
        end
    
        def matches?(crypted, *tokens)
          aes.decrypt
          aes.key = @key
          (aes.update(crypted.unpack("m").first) + aes.final) == tokens.join
        rescue OpenSSL::CipherError
          false
        end
    
        private
          def aes
            raise ArgumentError.new("You must provide a key like #{name}.key = my_key before using the #{name}") if @key.blank?
            @aes ||= OpenSSL::Cipher::Cipher.new("AES-256-ECB")
          end
      end
    end
  end
end

Version data entries

67 entries across 67 versions & 25 rubygems

Version Path
authlogic-2.1.11 lib/authlogic/crypto_providers/aes256.rb
authlogic-2.1.10 lib/authlogic/crypto_providers/aes256.rb
arpitjain11-rubycas-server-0.8.0.20090613 lib/casserver/authenticators/authlogic_crypto_providers/aes256.rb
binarylogic-authlogic-2.1.0 lib/authlogic/crypto_providers/aes256.rb
binarylogic-authlogic-2.1.1 lib/authlogic/crypto_providers/aes256.rb
crankharder-authlogic-2.1.1 lib/authlogic/crypto_providers/aes256.rb
gunark-rubycas-server-0.8.0.20090812 lib/casserver/authenticators/authlogic_crypto_providers/aes256.rb
jlecour-authlogic-2.1.2 lib/authlogic/crypto_providers/aes256.rb
kschrader-authlogic-2.1.2 lib/authlogic/crypto_providers/aes256.rb
kschrader-authlogic-2.1.3 lib/authlogic/crypto_providers/aes256.rb
authlogic-3.1.3 lib/authlogic/crypto_providers/aes256.rb
authlogic-2.1.8 lib/authlogic/crypto_providers/aes256.rb
authlogic-2.1.7 lib/authlogic/crypto_providers/aes256.rb
authlogic-3.1.2 lib/authlogic/crypto_providers/aes256.rb
authlogic-3.1.1 lib/authlogic/crypto_providers/aes256.rb
nulogy-authlogic-3.1.0.1 lib/authlogic/crypto_providers/aes256.rb
Empact-authlogic-3.1.0 lib/authlogic/crypto_providers/aes256.rb
authlogic-3.1.0 lib/authlogic/crypto_providers/aes256.rb
Empact-authlogic-3.0.3 lib/authlogic/crypto_providers/aes256.rb
expertiza-authlogic-2.1.6.1 lib/authlogic/crypto_providers/aes256.rb