Skip to content

Commit

Permalink
Add algorithm to Passwd::AbstractConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
i2bskn committed Dec 23, 2013
1 parent 4c35061 commit 204294f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## 0.1.5

Features:

- Can be specified algorithm of hashing
- Change default hashing algorithm to SHA512 from SHA1
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ Passwd.config # => Get config object.
Passwd.config(length: 10) # => Change to the default length.

Passwd.configure do |c|
c.algorithm = :sha512
c.length = 10
end
```

Options that can be specified:

* :algorithm => Hashing algorithm. default is :sha512.
* :length => Number of characters. default is 8.
* :lower => Skip lower case if set false. default is true.
* :upper => Skip upper case if set false. default is true.
Expand Down
1 change: 1 addition & 0 deletions lib/passwd.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8

require "digest/sha1"
require "digest/sha2"

require "passwd/version"
require "passwd/errors"
Expand Down
8 changes: 6 additions & 2 deletions lib/passwd/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ def auth(password_text, salt_hash, password_hash)
password_hash == enc_pass
end

def hashing(plain)
Digest::SHA1.hexdigest plain
def hashing(plain, algorithm=nil)
if algorithm.nil?
eval "Digest::#{@config.algorithm.to_s.upcase}.hexdigest \"#{plain}\""
else
eval "Digest::#{algorithm.to_s.upcase}.hexdigest \"#{plain}\""
end
end

def confirm_check(password, confirm, with_policy=false)
Expand Down
1 change: 1 addition & 0 deletions lib/passwd/configuration/abstract_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Passwd
class AbstractConfig
VALID_OPTIONS_KEYS = [
:algorithm,
:length,
:lower,
:upper,
Expand Down
1 change: 1 addition & 0 deletions lib/passwd/configuration/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize
end

def reset
self.algorithm = :sha512
self.length = 8
self.lower = true
self.upper = true
Expand Down
4 changes: 2 additions & 2 deletions spec/passwd/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class User
define_column
end

let(:salt) {Digest::SHA1.hexdigest("salt")}
let(:salt) {Digest::SHA512.hexdigest("salt")}
let(:password_text) {"secret"}
let(:password_hash) {Digest::SHA1.hexdigest("#{salt}#{password_text}")}
let(:password_hash) {Digest::SHA512.hexdigest("#{salt}#{password_text}")}

describe ".#included" do
it "define singleton methods" do
Expand Down
14 changes: 12 additions & 2 deletions spec/passwd/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,19 @@
end

describe "#hashing" do
it "should call SHA512.#hexdigest" do
Digest::SHA512.should_receive(:hexdigest)
Passwd.hashing("secret")
end

it "return hashed password" do
Digest::SHA1.should_receive(:hexdigest).with("secret").and_return("hash")
expect(Passwd.hashing("secret")).to eq("hash")
hashed = Digest::SHA512.hexdigest "secret"
expect(Passwd.hashing("secret")).to eq(hashed)
end

it "return hashed password specified algorithm" do
hashed = Digest::SHA256.hexdigest "secret"
expect(Passwd.hashing("secret", :sha256)).to eq(hashed)
end

it "should create exception if not specified argument" do
Expand Down
8 changes: 8 additions & 0 deletions spec/passwd/configuration/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
let(:config) {Passwd::Config.instance}

describe "defined accessors" do
it "defined algorithm" do
expect(config.respond_to? :algorithm).to be_true
end

it "defined length" do
expect(config.respond_to? :length).to be_true
end
Expand Down Expand Up @@ -36,6 +40,10 @@
end

describe "#initialize" do
it "algorithm should be a default" do
expect(config.algorithm).to eq(:sha512)
end

it "length should be a default" do
expect(config.length).to eq(8)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/passwd/configuration/tmp_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def tmp_config(options={})
end

describe "defined accessors" do
it "defined algorithm" do
expect(config.respond_to? :algorithm).to be_true
end

it "defined length" do
expect(tmp_config.respond_to? :length).to be_true
end
Expand Down Expand Up @@ -41,6 +45,10 @@ def tmp_config(options={})

describe "#initialize" do
context "with empty options" do
it "algorithm should be a default" do
expect(config.algorithm).to eq(:sha512)
end

it "length should be a default" do
expect(tmp_config.length).to eq(8)
end
Expand Down

0 comments on commit 204294f

Please sign in to comment.