Sha256: 17169b9e6083277e7c87056c1e9d62f0db6080f153815b608826d6d80b8c704d

Contents?: true

Size: 1.84 KB

Versions: 24

Compression:

Stored size: 1.84 KB

Contents

require "openssl"

module Sorcery
  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:
    #
    #   Sorcery::Model::ConfigAES256.key = "my 32 bytes long key"
    #
    # 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)
          decrypt(crypted) == tokens.join
        rescue OpenSSL::CipherError
          false
        end
        
        def decrypt(crypted)
          aes.decrypt
          aes.key = @key
          (aes.update(crypted.unpack("m").first) + aes.final)
        end
    
        private
        
        def aes
          raise ArgumentError.new("#{name} expects a 32 bytes long key. Please use Sorcery::Model::Config.encryption_key to set it.") if ( @key.nil? || @key == "" )
          @aes ||= OpenSSL::Cipher::Cipher.new("AES-256-ECB")
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
sorcery-0.9.1 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.9.0 lib/sorcery/crypto_providers/aes256.rb
cbsorcery-0.8.6 lib/sorcery/crypto_providers/aes256.rb
cb-sorcery-0.8.6 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.8.6 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.8.5 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.8.4 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.8.2 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.8.1 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.8.0 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.13 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.12 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.11 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.10 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.9 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.8 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.7 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.6 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.5 lib/sorcery/crypto_providers/aes256.rb
sorcery-0.7.4 lib/sorcery/crypto_providers/aes256.rb