Sha256: f514c1c3834e1979d1e53a85a415797a4ad1d252806d997769ae89c76ee5f31f

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'digest/sha1'

module Auth
    # Cryptographic utilities used by the Auth module.
    module Crypt
        #--
        # Ideally, I'd want the next bit to be settings, but they're not
        # behaving quite like I wanted.  Ah, well.
        # FIXME:  Figure out some way to get them into configs or something.
        #
        # setting :crypt_prefix, :default => '-CHANGE-ME-',
        #     :doc => 'Prefix used when hashing any string.'
        # setting :salt_prefix, :default => '-salt-',
        #     :doc => 'Prefix used when generating the salt.'
        #++
        protected
            @@crypt_prefix = "-CHANGE-ME-"
            @@salt_prefix = "-salt-"
            @@session_key_expiration = 60*60*24*30
            def self.crypt_prefix
                @@crypt_prefix
            end
            def self.salt_prefix
                @@salt_prefix
            end

        public
            # Creates a timestamp string for inclusion in hashed strings.
            def self.timestamp
                Time.now.strftime '%Y%m%d-%H%M%S'
            end

            # Hashes the provided string.  Returns a 40-character
            # hex representation of the hashed value.
            def self.make_hash(value)
                Digest::SHA1.hexdigest "#{crypt_prefix}--#{value}--"
            end

            # Create a salt for mixing with hashed strings.
            def self.make_salt
                make_hash "#{salt_prefix}-#{timestamp}"
            end

            # Mix a salt into a password and return the hashed result.
            def self.salt_password(salt, password)
                make_hash salt + password
            end

            # Create a session key, given a password.
            # Use the salted/hashed password here, not the raw one!
            def self.make_session_key(hashed_password)
                make_hash hashed_password + timestamp + rand.to_s
            end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-auth-0.2.0 lib/nitro/auth/util/crypt.rb