hashpasswd 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Ben
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ hashpasswd
2
+ ==========
3
+
4
+ Password hashing and validation.
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ t.test_files = FileList['test/test*.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -25,15 +25,15 @@ module Hashpasswd
25
25
  @delimeter = options[:delimter] || ':'
26
26
  @digest = options[:digest] || 'SHA1'
27
27
 
28
- salt = SecureRandom.base64( @salt_byte_size )
28
+ salt = SecureRandom.base64(@salt_byte_size)
29
29
  pbkdf2 = OpenSSL::PKCS5::pbkdf2_hmac(
30
30
  password,
31
31
  salt,
32
32
  @pbkdf2_iterations,
33
33
  @hash_byte_size,
34
34
  @digest
35
- )
36
- return [@digest, @pbkdf2_iterations, salt, Base64.encode64( pbkdf2 )].join( @delimeter )
35
+ )
36
+ return [@digest, @pbkdf2_iterations, salt, Base64.encode64(pbkdf2)].join(@delimeter)
37
37
  end
38
38
 
39
39
  # Validates a password against a hash
@@ -49,14 +49,14 @@ module Hashpasswd
49
49
  params = hash.split(@delimeter)
50
50
  return false if params.length != HASH_SECTIONS
51
51
 
52
- pbkdf2 = Base64.decode64( params[HASH_INDEX] )
52
+ pbkdf2 = Base64.decode64(params[HASH_INDEX])
53
53
  testhash = OpenSSL::PKCS5::pbkdf2_hmac(
54
54
  password,
55
55
  params[SALT_INDEX],
56
56
  params[ITERATIONS_INDEX].to_i,
57
57
  pbkdf2.length,
58
58
  params[0]
59
- )
59
+ )
60
60
 
61
61
  return pbkdf2 == testhash
62
62
  end
@@ -0,0 +1,135 @@
1
+ require 'test/unit'
2
+ require 'securerandom'
3
+ require './lib/hashpasswd'
4
+
5
+ class HashpasswdTest < Test::Unit::TestCase
6
+
7
+ def test_getconstants
8
+ constants = Hashpasswd.getconstants
9
+ assert_equal 4,
10
+ constants[:HASH_SECTIONS]
11
+ assert_equal 1,
12
+ constants[:ITERATIONS_INDEX]
13
+ assert_equal 2,
14
+ constants[:SALT_INDEX]
15
+ assert_equal 3,
16
+ constants[:HASH_INDEX]
17
+ assert_equal ':',
18
+ constants[:DEFAULT_DELIMETER]
19
+ end
20
+
21
+ def test_create_and_validate_hash
22
+ correct_password = 'testpassword'
23
+ wrong_password = 'nottestpassword'
24
+ hash = Hashpasswd.createhash(correct_password)
25
+ assert(Hashpasswd.validatepasswd(correct_password, hash) == true, "correct password")
26
+ assert(Hashpasswd.validatepasswd(wrong_password, hash) == false, "wrong password")
27
+ end
28
+
29
+ def test_hash_and_salt_difference
30
+ password = 'somepassword'
31
+ hash = Hashpasswd.createhash(password)
32
+ h1 = hash.split(Hashpasswd.getconstants[:DEFAULT_DELIMETER])
33
+ h2 = Hashpasswd.createhash(password).split(Hashpasswd.getconstants[:DEFAULT_DELIMETER])
34
+ assert(h1[Hashpasswd.getconstants[:HASH_INDEX]] != h2[Hashpasswd.getconstants[:HASH_INDEX]], "different hashes")
35
+ assert(h1[Hashpasswd.getconstants[:SALT_INDEX]] != h2[Hashpasswd.getconstants[:SALT_INDEX]], "different salt")
36
+ end
37
+
38
+ def test_passwords_validate
39
+ password=[]
40
+ password[0] = "42+' x53q~16%vQ"
41
+ password[1] = 'v,@8DDD>mwy%Io0'
42
+ password[2] = 'f=j2p~/U5~bLmgg'
43
+ password[3] = 'Y~k.[*6K{ }^~1V'
44
+ password[4] = 'iLf<!,:P!tF&lg^/w)=4'
45
+ password[5] = 'I2O/#+;|@0<$L."c^%k!;Uz}4'
46
+ password[6] = ",}9#ps&+/'@^=.=3'Nr*X+B2>G+=uh"
47
+ password[7] = 'GPXE)uQxdEx39>sOAfLfxGPkE,KHCT11C}q'
48
+ password[8] = 'p3"3f(V5k7-0%{&}7)[U{/p&7g$}Tv*9+"+^[G1F'
49
+ password[9] = 'bS2FCtXKRxxFTn0m7hyn2TS6MfPxkzkeC42I=9uaIc4vR6PP5r'
50
+ wrong_password = 'P@ssw0rd!'
51
+
52
+ hash = []
53
+ for i in 0..(password.length - 1) do
54
+ hash[i] = Hashpasswd.createhash(password[i])
55
+ end
56
+
57
+ for i in 0..(hash.length - 1) do
58
+ assert(Hashpasswd.validatepasswd(password[i], hash[i]) == true, "correct password")
59
+ assert(Hashpasswd.validatepasswd(wrong_password, hash[i]) == false, "wrong password")
60
+ end
61
+ end
62
+
63
+ def test_hash_difference
64
+ password=[]
65
+ password[0] = "42+' x53q~16%vQ"
66
+ password[1] = 'v,@8DDD>mwy%Io0'
67
+ password[2] = 'f=j2p~/U5~bLmgg'
68
+ password[3] = 'Y~k.[*6K{ }^~1V'
69
+ password[4] = 'iLf<!,:P!tF&lg^/w)=4'
70
+ password[5] = 'I2O/#+;|@0<$L."c^%k!;Uz}4'
71
+ password[6] = ",}9#ps&+/'@^=.=3'Nr*X+B2>G+=uh"
72
+ password[7] = 'GPXE)uQxdEx39>sOAfLfxGPkE,KHCT11C}q'
73
+ password[8] = 'p3"3f(V5k7-0%{&}7)[U{/p&7g$}Tv*9+"+^[G1F'
74
+ password[9] = 'bS2FCtXKRxxFTn0m7hyn2TS6MfPxkzkeC42I=9uaIc4vR6PP5r'
75
+
76
+ hash = []
77
+ for i in 0..(password.length - 1) do
78
+ hash[i] = Hashpasswd.createhash(password[i])
79
+ end
80
+
81
+ hash2 = []
82
+ for i in 0..(password.length - 1) do
83
+ hash2[i] = Hashpasswd.createhash(password[i])
84
+ end
85
+
86
+ for i in 0..(hash.length - 1) do
87
+ assert(Hashpasswd.validatepasswd(password[i], hash[i]) == true, "password for hash[#{i}] validates")
88
+ assert(Hashpasswd.validatepasswd(password[i], hash2[i]) == true, "password for hash2[#{i}] validates")
89
+ assert(hash[i] != hash2[i], "hash[#{i}] != hash2[#{i}]")
90
+ end
91
+ end
92
+
93
+ def test_random_passwords_validate
94
+ password = []
95
+ wrong_password = []
96
+ for i in 0..9 do
97
+ password[i] = SecureRandom.base64((i+1)*5)
98
+ wrong_password[i] = SecureRandom.base64((i+1)*5)
99
+ end
100
+
101
+ hash = []
102
+ for i in 0..(password.length - 1) do
103
+ hash[i] = Hashpasswd.createhash(password[i])
104
+ end
105
+
106
+ for i in 0..(hash.length - 1) do
107
+ assert(Hashpasswd.validatepasswd(password[i], hash[i]) == true, "correct password")
108
+ assert(Hashpasswd.validatepasswd(wrong_password[i], hash[i]) == false, "wrong password")
109
+ end
110
+ end
111
+
112
+ def test_random_hash_difference
113
+ password=[]
114
+ for i in 0..9 do
115
+ password[i] = SecureRandom.base64((i+1)*5)
116
+ end
117
+
118
+ hash = []
119
+ for i in 0..(password.length - 1) do
120
+ hash[i] = Hashpasswd.createhash(password[i])
121
+ end
122
+
123
+ hash2 = []
124
+ for i in 0..(password.length - 1) do
125
+ hash2[i] = Hashpasswd.createhash(password[i])
126
+ end
127
+
128
+ for i in 0..(hash.length - 1) do
129
+ assert(Hashpasswd.validatepasswd(password[i], hash[i]) == true, "password for hash[#{i}] validates")
130
+ assert(Hashpasswd.validatepasswd(password[i], hash2[i]) == true, "password for hash2[#{i}] validates")
131
+ assert(hash[i] != hash2[i], "hash[#{i}] != hash2[#{i}]")
132
+ end
133
+ end
134
+
135
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashpasswd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-06 00:00:00.000000000 Z
12
+ date: 2013-08-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Password hashing and validation using OpenSSL::PKCS5::pbkdf2_hmac for
15
15
  the hash and SecureRandom.base64 for the salt. Digest defaults to SHA1, but can
@@ -20,6 +20,10 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - lib/hashpasswd.rb
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - test/test_hashpasswd.rb
23
27
  homepage: http://rubygems.org/gems/hashpasswd
24
28
  licenses: []
25
29
  post_install_message: