passwd 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,6 +34,12 @@ Options that can be specified:
34
34
  * :letters_upper => Define an array of upper case. default is ("A".."Z").to_a
35
35
  * :letters_number => Define an array of numbers. default is ("0".."9").to_a
36
36
 
37
+ Default config is stored in the class variable. (@@config)
38
+ Changing the default configs are as follows:
39
+
40
+ Passwd.config => Get config hash.
41
+ Passwd.config(length: 10) => Change to the default length.
42
+
37
43
  Password hashing:
38
44
 
39
45
  password_hash = Passwd.hashing(password)
@@ -51,6 +57,37 @@ Options that can be specified:
51
57
  * :require_upper => Require upper case if set true. specify_type enabled when true.
52
58
  * :require_number => Require number case if set true. specify_type enabled when true.
53
59
 
60
+ Default policy is stored in the class variable. (@@policy)
61
+ Changing the default policy are as follows:
62
+
63
+ Passwd.policy => Get policy hash.
64
+ Passwd.policy(min_length: 10) => Change to the default min_length.
65
+
66
+ Passwd object:
67
+
68
+ Default password is randomly generated.
69
+ Default salt is "#{Time.now.to_s}".
70
+
71
+ password = Passwd.new
72
+ password.text # return text password.
73
+ password.salt_text # return text salt.
74
+ password.salt_hash # return hash salt.
75
+ password.hash # return hash password.
76
+
77
+ Options that can be specified:
78
+
79
+ * :password => Text password. default is random.
80
+ * :salt_text => Text salt. default is #{Time.now.to_s}.
81
+
82
+ Password authenticate:
83
+
84
+ password = Passwd.new
85
+ Passwd.auth(password.text, password.salt_hash, password.hash) # => true
86
+ Passwd.auth("invalid!!", password.salt_hash, password.hash) # => false
87
+
88
+ password == password.text # => true
89
+ password == "invalid!!" # => false
90
+
54
91
  ## Contributing
55
92
 
56
93
  1. Fork it
data/lib/passwd.rb CHANGED
@@ -4,7 +4,7 @@ require "passwd/version"
4
4
  require "digest/sha1"
5
5
 
6
6
  class Passwd
7
- attr_reader :text, :hash
7
+ attr_reader :text, :hash, :salt_text, :salt_hash
8
8
  @@config = {
9
9
  length: 8,
10
10
  lower: true,
@@ -24,8 +24,8 @@ class Passwd
24
24
  require_number: true
25
25
  }
26
26
 
27
- def initialize(password=nil)
28
- if password.nil?
27
+ def initialize(options={password: nil, salt_text: Time.now.to_s})
28
+ if options[:password].nil?
29
29
  # Create letters
30
30
  letters = Array.new
31
31
  letters += @@config[:letters_lower] if @@config[:lower]
@@ -35,22 +35,45 @@ class Passwd
35
35
  # Create random password
36
36
  @text = Array.new(@@config[:length]){letters[rand(letters.size)]}.join
37
37
  else
38
- @text = password
38
+ @text = options[:password]
39
39
  end
40
40
  # @text = password.nil? ? self.class.create : password
41
- @hash = Passwd.hashing(@text)
41
+ @salt_text = options[:salt_text] || Time.now.to_s
42
+ @salt_hash = Passwd.hashing(@salt_text)
43
+ @hash = Passwd.hashing("#{@salt_hash}#{@text}")
42
44
  end
43
45
 
44
46
  def text=(password)
47
+ @hash = Passwd.hashing("#{@salt_hash}#{password}")
45
48
  @text = password
46
- @hash = Passwd.hashing(@text)
47
- @text
49
+ end
50
+
51
+ def hash=(password_hash)
52
+ @text = nil
53
+ @hash = password_hash
54
+ end
55
+
56
+ def salt_text=(salt_text)
57
+ @salt_hash = Passwd.hashing(salt_text)
58
+ @hash = Passwd.hashing("#{@salt_hash}#{@text}")
59
+ @salt_text = salt_text
60
+ end
61
+
62
+ def salt_hash=(salt_hash)
63
+ @salt_text = nil
64
+ @hash = Passwd.hashing("#{salt_hash}#{@text}")
65
+ @salt_hash = salt_hash
48
66
  end
49
67
 
50
68
  def policy_check
51
69
  Passwd.policy_check @text
52
70
  end
53
71
 
72
+ def ==(password)
73
+ enc_pass = Passwd.hashing("#{@salt_hash}#{password}")
74
+ @hash == enc_pass
75
+ end
76
+
54
77
  class << self
55
78
  def create(options={})
56
79
  config = @@config.merge(options)
@@ -93,6 +116,11 @@ class Passwd
93
116
  true
94
117
  end
95
118
 
119
+ def auth(password_text, salt_hash, password_hash)
120
+ enc_pass = Passwd.hashing("#{salt_hash}#{password_text}")
121
+ password_hash == enc_pass
122
+ end
123
+
96
124
  def hashing(passwd)
97
125
  Digest::SHA1.hexdigest passwd
98
126
  end
@@ -1,3 +1,3 @@
1
1
  class Passwd
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/passwd_spec.rb CHANGED
@@ -71,6 +71,22 @@ describe Passwd do
71
71
  end
72
72
  end
73
73
 
74
+ context "#auth" do
75
+ it "return true with valid password" do
76
+ password = Passwd.create
77
+ salt_hash = Passwd.hashing(Time.now.to_s)
78
+ password_hash = Passwd.hashing("#{salt_hash}#{password}")
79
+ expect(Passwd.auth(password, salt_hash, password_hash)).to eq(true)
80
+ end
81
+
82
+ it "return false with invalid password" do
83
+ password = "Secret!!"
84
+ salt_hash = Passwd.hashing(Time.now.to_s)
85
+ password_hash = Passwd.hashing("#{salt_hash}#{password}")
86
+ expect(Passwd.auth("Secret!", salt_hash, password_hash)).to eq(false)
87
+ end
88
+ end
89
+
74
90
  context "#hashing" do
75
91
  it "return string object" do
76
92
  expect(Passwd.hashing(Passwd.create).class).to eq(String)
@@ -131,25 +147,40 @@ describe Passwd do
131
147
  expect(password.text.size).to eq(8)
132
148
  expect(password.text.class).to eq(String)
133
149
  expect(password.hash.class).to eq(String)
150
+ expect(password.salt_text.class).to eq(String)
151
+ expect(password.salt_hash.class).to eq(String)
134
152
  end
135
153
 
136
154
  it "@text is specified password" do
137
155
  pass_text = Passwd.create
138
- password = Passwd.new(pass_text)
156
+ password = Passwd.new(password: pass_text)
139
157
  expect(password.text).to eq(pass_text)
140
158
  end
141
159
 
142
160
  it "@hash is hash of specified password" do
143
161
  pass_text = Passwd.create
144
- pass_hash = Digest::SHA1.hexdigest(pass_text)
145
- password = Passwd.new(pass_text)
162
+ password = Passwd.new(password: pass_text)
163
+ pass_hash = Passwd.hashing("#{password.salt_hash}#{pass_text}")
146
164
  expect(password.hash).to eq(pass_hash)
147
165
  end
166
+
167
+ it "@salt_text is specified salt" do
168
+ salt_text = "salt"
169
+ password = Passwd.new(salt_text: salt_text)
170
+ expect(password.salt_text).to eq(salt_text)
171
+ end
172
+
173
+ it "@salt_hash is hash of specified salt" do
174
+ salt_text = "salt"
175
+ salt_hash = Passwd.hashing(salt_text)
176
+ password = Passwd.new(salt_text: salt_text)
177
+ expect(password.salt_hash).to eq(salt_hash)
178
+ end
148
179
  end
149
180
 
150
181
  context "#text=" do
151
182
  before(:each) do
152
- @password = Passwd.new("Secret!!")
183
+ @password = Passwd.new(password: "Secret!!")
153
184
  end
154
185
 
155
186
  it "@text is changed" do
@@ -165,13 +196,91 @@ describe Passwd do
165
196
  end
166
197
  end
167
198
 
199
+ context "#hash=" do
200
+ before(:each) do
201
+ @password = Passwd.new
202
+ end
203
+
204
+ it "@text is nil" do
205
+ @password.hash = Passwd.hashing("Secret!!")
206
+ expect(@password.text).to eq(nil)
207
+ end
208
+
209
+ it "@hash is changed" do
210
+ old_hash = @password.hash
211
+ @password.hash = Passwd.hashing("Secret!!")
212
+ expect(@password.hash).not_to eq(old_hash)
213
+ end
214
+ end
215
+
216
+ context "#salt_text=" do
217
+ before(:each) do
218
+ @password = Passwd.new
219
+ end
220
+
221
+ it "@salt_text is changed" do
222
+ old_salt_text = @password.salt_text
223
+ @password.salt_text = "salt"
224
+ expect(@password.salt_text).not_to eq(old_salt_text)
225
+ end
226
+
227
+ it "@salt_hash is changed" do
228
+ old_salt_hash = @password.salt_hash
229
+ @password.salt_text = "salt"
230
+ expect(@password.salt_hash).not_to eq(old_salt_hash)
231
+ end
232
+
233
+ it "@hash is changed" do
234
+ old_hash = @password.hash
235
+ @password.salt_text = "salt"
236
+ expect(@password.hash).not_to eq(old_hash)
237
+ end
238
+ end
239
+
240
+ context "#salt_hash=" do
241
+ before(:each) do
242
+ @password = Passwd.new
243
+ end
244
+
245
+ it "@salt_text is nil" do
246
+ @password.salt_hash = Passwd.hashing("salt")
247
+ expect(@password.salt_text).to eq(nil)
248
+ end
249
+
250
+ it "@salt_hash is changed" do
251
+ old_salt_hash = @password.salt_hash
252
+ @password.salt_hash = Passwd.hashing("salt")
253
+ expect(@password.salt_hash).not_to eq(old_salt_hash)
254
+ end
255
+
256
+ it "@hash is changed" do
257
+ old_hash = @password.hash
258
+ @password.salt_hash = Passwd.hashing("salt")
259
+ expect(@password.hash).not_to eq(old_hash)
260
+ end
261
+ end
262
+
168
263
  context "#policy_check" do
169
264
  it "Passwd.policy_check is called with pass_text" do
170
265
  pass_text = Passwd.create
171
266
  Passwd.should_receive(:policy_check).with(pass_text)
172
- password = Passwd.new(pass_text)
267
+ password = Passwd.new(password: pass_text)
173
268
  password.policy_check
174
269
  end
175
270
  end
271
+
272
+ context "#==" do
273
+ before(:each) do
274
+ @password = Passwd.new
275
+ end
276
+
277
+ it "return true with valid password" do
278
+ expect(@password == @password.text).to eq(true)
279
+ end
280
+
281
+ it "return false with invalid password" do
282
+ expect(@password == "Secret!!").to eq(false)
283
+ end
284
+ end
176
285
  end
177
286
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passwd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -90,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  segments:
92
92
  - 0
93
- hash: 1759901407687586597
93
+ hash: -2718541000702937110
94
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
@@ -99,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
99
  version: '0'
100
100
  segments:
101
101
  - 0
102
- hash: 1759901407687586597
102
+ hash: -2718541000702937110
103
103
  requirements: []
104
104
  rubyforge_project:
105
105
  rubygems_version: 1.8.25