Sha256: 91a16fd9524a82c397e5166809412dce0ef7b7d737a2585abec480fcf1249052

Contents?: true

Size: 1.57 KB

Versions: 9

Compression:

Stored size: 1.57 KB

Contents

# encoding: UTF-8

module Spontaneous
  module Crypt
    VERSION_SEP = "%".freeze

    module ModuleMethods
      def current
        return version(@forced) if @forced
        versions.last
      end

      def force_version(n)
        @forced = n
      end

      def default_version
        @forced = nil
      end

      def versions
        Version.subclasses.sort { |v1, v2| v1.version <=> v2.version }
      end

      def hash_password(password)
        current.create(password)
      end

      def version(version)
        versions.detect { |v| v.version == version }
      end

      def valid?(password, blob)
        new(password, blob).valid?
      end

      def serialize(version, hash)
        [version, hash].join VERSION_SEP
      end

      def version_split(blob)
        version, hash = blob.split(VERSION_SEP)
        [version.to_i, hash]
      end

      def outdated?(version)
        version.version < current.version
      end

      def new(password, blob)
        version, hash = version_split(blob)
        Validator.new(password, Crypt.version(version).new(hash))
      end
    end

    extend ModuleMethods

    class Validator
      def initialize(password, version)
        @password, @version = password, version
      end

      def valid?
        @version.valid?(@password)
      end

      def outdated?
        @version.outdated?
      end

      alias_method :needs_upgrade?, :outdated?

      def upgrade
        Spontaneous::Crypt.hash_password(@password)
      end

      def salt
        @version.salt
      end
    end
  end
end

require 'spontaneous/crypt/version'

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta10 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta9 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta8 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta7 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta6 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta5 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta4 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta3 lib/spontaneous/crypt.rb
spontaneous-0.2.0.beta2 lib/spontaneous/crypt.rb