Sha256: 0dc18f5c2d825c567016e7c3eb736c944247afcd13d1dd02294eec512147877b

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require "digest/sha1"

module Authlogic
  module CryptoProviders
    class Sha1
      # A poor choice. There are known attacks against this algorithm.
      class V2
        class << self
          def join_token
            @join_token ||= "--"
          end
          attr_writer :join_token

          # The number of times to loop through the encryption.
          def stretches
            @stretches ||= 10
          end
          attr_writer :stretches

          # Turns your raw password into a Sha1 hash.
          def encrypt(*tokens)
            tokens = tokens.flatten
            digest = tokens.shift
            stretches.times do
              digest = Digest::SHA1.digest([digest, *tokens].join(join_token))
            end
            digest.unpack("H*")[0]
          end

          # Does the crypted password match the tokens? Uses the same tokens that
          # were used to encrypt.
          def matches?(crypted, *tokens)
            encrypt(*tokens) == crypted
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authlogic-6.0.0 lib/authlogic/crypto_providers/sha1/v2.rb