Sha256: 36f03889d8726fcf7a6e7151dd0f87e90684ddc68b6c051d310580c5dc10b508

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "digest/sha2"

module Authlogic
  module CryptoProviders
    class Sha512
      # SHA-512 does not have any practical known attacks against it. However,
      # there are better choices. We recommend transitioning to a more secure,
      # adaptive hashing algorithm, like scrypt.
      class V2
        class << self
          attr_accessor :join_token

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

          # Turns your raw password into a Sha512 hash.
          def encrypt(*tokens)
            digest = tokens.flatten.join(join_token)
            stretches.times do
              digest = Digest::SHA512.digest(digest)
            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/sha512/v2.rb