Skip to content

Commit

Permalink
Parameterize the PBKDF2 algorithm in application config
Browse files Browse the repository at this point in the history
  • Loading branch information
dougalcorn committed Jul 23, 2013
1 parent 4b269de commit 35a2bb7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 1 addition & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def ensure_password_is_hashed
end

def hash_password(password, salt)
Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations)
Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations, Rails.configuration.pbkdf2_algorithm)
end

def add_trust_level
Expand Down Expand Up @@ -674,4 +674,3 @@ def self.discourse_hub_nickname_operation
# index_users_on_username (username) UNIQUE
# index_users_on_username_lower (username_lower) UNIQUE
#

1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Application < Rails::Application

# per https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet
config.pbkdf2_iterations = 64000
config.pbkdf2_algorithm = "sha256"

# dumping rack lock cause the message bus does not work with it (throw :async, it catches Exception)
# see: https://github.com/sporkrb/spork/issues/66
Expand Down
18 changes: 9 additions & 9 deletions lib/pbkdf2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
#
# Also PBKDF2 monkey patches string ... don't like that at all
#
# Happy to move back to PBKDF2 ruby gem provided:
# Happy to move back to PBKDF2 ruby gem provided:
#
# 1. It works on Ruby 2.0
# 2. It works on 1.9.3
# 2. It works on 1.9.3
# 3. It does not monkey patch string

require 'openssl'
require 'xor'

class Pbkdf2

def self.hash_password(password, salt, iterations)

h = OpenSSL::Digest::Digest.new("sha256")

def self.hash_password(password, salt, iterations, algorithm = "sha256")

h = OpenSSL::Digest::Digest.new(algorithm)

u = ret = prf(h, password, salt + [1].pack("N"))

2.upto(iterations) do
2.upto(iterations) do
u = prf(h, password, u)
ret.xor!(u)
ret.xor!(u)
end

ret.bytes.map{|b| ("0" + b.to_s(16))[-2..-1]}.join("")
end

protected
protected

# fallback xor in case we need it for jruby ... way slower
def self.xor(x,y)
Expand Down

0 comments on commit 35a2bb7

Please sign in to comment.