Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Attempt to add a way to set the BCrypt cost of the hash.

This currently doesn't work, and I am not sure why.
  • Loading branch information...
commit 675208d568ed82a9e3e91ed1f4d345efb8f172e9 1 parent e193130
@namelessjon namelessjon authored
View
3  dm-types/lib/dm-types/bcrypt_hash.rb
@@ -6,6 +6,7 @@ module Types
class BCryptHash < DataMapper::Type
primitive String
size 60
+ scale BCrypt::Engine::DEFAULT_COST
def self.load(value, property)
if value.nil?
@@ -21,7 +22,7 @@ def self.dump(value, property)
if value.nil?
nil
elsif value.is_a?(String)
- BCrypt::Password.create(value)
+ BCrypt::Password.create(value, :cost => scale)
else
raise ArgumentError.new("+value+ must be nil or a String")
end
View
82 dm-types/spec/integration/bcrypt_hash_spec.rb
@@ -3,33 +3,73 @@
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'
describe DataMapper::Types::BCryptHash do
- before(:each) do
- class User
- include DataMapper::Resource
+ describe "with no options" do
+ before(:each) do
+ class User
+ include DataMapper::Resource
- property :id, Serial
- property :password, BCryptHash
+ property :id, Serial
+ property :password, BCryptHash
+ end
+ User.auto_migrate!
+ User.create!(:password => "DataMapper R0cks!")
end
- User.auto_migrate!
- User.create!(:password => "DataMapper R0cks!")
- end
- it "should save a password to the DB on creation" do
- repository(:default) do
- User.create!(:password => "password1")
+ it "should save a password to the DB on creation" do
+ repository(:default) do
+ User.create!(:password => "password1")
+ end
+ user = User.all
+ user[0].password.should == "DataMapper R0cks!"
+ user[1].password.should == "password1"
+ end
+
+ it "should change the password on attribute update" do
+ @user = User.first
+ @user.attribute_set(:password, "D@t@Mapper R0cks!")
+ @user.save
+ @user.password.should_not == "DataMapper R0cks!"
+ @user.password.should == "D@t@Mapper R0cks!"
end
- user = User.all
- user[0].password.should == "DataMapper R0cks!"
- user[1].password.should == "password1"
- end
- it "should change the password on attribute update" do
- @user = User.first
- @user.attribute_set(:password, "D@t@Mapper R0cks!")
- @user.save
- @user.password.should_not == "DataMapper R0cks!"
- @user.password.should == "D@t@Mapper R0cks!"
+ it "should have a cost of BCrypt::Engine::DEFAULT_COST" do
+ @user = User.first
+ @user.password.cost.should == BCrypt::Engine::DEFAULT_COST
+ end
end
+ describe "with a scale set" do
+ before(:each) do
+ class User
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :password, BCryptHash, :scale => 8
+ end
+ User.auto_migrate!
+ User.create!(:password => "DataMapper R0cks!")
+ end
+ it "should save a password to the DB on creation" do
+ repository(:default) do
+ User.create!(:password => "password1")
+ end
+ user = User.all
+ user[0].password.should == "DataMapper R0cks!"
+ user[1].password.should == "password1"
+ end
+
+ it "should change the password on attribute update" do
+ @user = User.first
+ @user.attribute_set(:password, "D@t@Mapper R0cks!")
+ @user.save
+ @user.password.should_not == "DataMapper R0cks!"
+ @user.password.should == "D@t@Mapper R0cks!"
+ end
+
+ it "should have the set cost" do
+ @user = User.first
+ @user.password.cost.should == 8
+ end
+ end
end
View
7 dm-types/spec/unit/bcrypt_hash_spec.rb
@@ -22,11 +22,14 @@
it "should return nil if nil is passed" do
BCryptHash.dump(nil, :property).should be_nil
end
-
end
describe ".load" do
- it "should return the password as the password" do
+ it "should return the password as a BCrypt::Password" do
+ BCryptHash.load(@crypted_password, :property).should be_an_instance_of(BCrypt::Password)
+ end
+
+ it "should return the password as a password which matches" do
BCryptHash.load(@crypted_password, :property).should == @clear_password
end
Please sign in to comment.
Something went wrong with that request. Please try again.