password_encryptor 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -2
- data/lib/password_encryptor.rb +9 -1
- data/spec/unit/password_encryptor_spec.rb +25 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f3aff9002bc2806e31cb8982f5f8eb6fd3329d9
|
4
|
+
data.tar.gz: 34221c99437f9256a43e97a3b0840d7f3c8b925a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84ad7fb687b0897ac795ba5d1a251b1e7bceeb3436f838eb6eba695de3121526ce241181cbd57bc74f320363f72378592cce5a3e6b37a3607d0f101e384561f6
|
7
|
+
data.tar.gz: 659509dbc0304f67b2c66a6b0174d7546dcae204d2452993b3db1b1a4d89a946bdf98141e90ef760b6d0d03f17d804da2e8c8e06b7cd49884f59a9bd82823924
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# PasswordEncryptor
|
2
2
|
|
3
|
-
|
3
|
+
This is a simple class that wraps BCrypt usage pattern into single
|
4
|
+
method that does password encryption, verification, and takes makes it
|
5
|
+
use minimum cost when running in Rails test and development environments
|
6
|
+
(but it works without Rails too).
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -18,7 +21,19 @@ Or install it yourself as:
|
|
18
21
|
|
19
22
|
## Usage
|
20
23
|
|
21
|
-
|
24
|
+
To encrypt and get String that you can store to `encrypted_password`
|
25
|
+
column in database, use:
|
26
|
+
|
27
|
+
encrypted_password = PasswordEncryptor.encrypt('password')
|
28
|
+
|
29
|
+
To verify the password matches given String use:
|
30
|
+
|
31
|
+
PasswordEncryptor.new(encrypted_password) == 'password'
|
32
|
+
# => true
|
33
|
+
|
34
|
+
Password verification does not raise any errors when encrypted password
|
35
|
+
is invalid BCrypt hash, is empty or nil, just returns false in that
|
36
|
+
case. Returns false on invalid password, of course, too.
|
22
37
|
|
23
38
|
## Contributing
|
24
39
|
|
@@ -27,3 +42,4 @@ TODO: Write usage instructions here
|
|
27
42
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
43
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
44
|
5. Create a new Pull Request
|
45
|
+
|
data/lib/password_encryptor.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'bcrypt'
|
2
2
|
|
3
3
|
class PasswordEncryptor
|
4
|
-
VERSION = "1.0.
|
4
|
+
VERSION = "1.0.1"
|
5
5
|
|
6
6
|
def self.encrypt plain_text
|
7
7
|
PasswordEncryptor.new(plain_text).encrypt
|
@@ -15,6 +15,14 @@ class PasswordEncryptor
|
|
15
15
|
BCrypt::Password.create(@password, cost: cost)
|
16
16
|
end
|
17
17
|
|
18
|
+
def matches?(hash)
|
19
|
+
BCrypt::Password.new(hash) == @password
|
20
|
+
rescue BCrypt::Errors::InvalidHash
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
alias_method :==, :matches?
|
25
|
+
|
18
26
|
private
|
19
27
|
|
20
28
|
def cost
|
@@ -4,4 +4,29 @@ describe PasswordEncryptor do
|
|
4
4
|
it 'should encrypt passwords using BCrypt' do
|
5
5
|
expect(PasswordEncryptor.encrypt('password') == 'password').to eq(true)
|
6
6
|
end
|
7
|
+
|
8
|
+
it 'should be possible to verify if password matches given hash' do
|
9
|
+
hash = PasswordEncryptor.encrypt('password')
|
10
|
+
expect(PasswordEncryptor.new('password').matches?(hash)).to eq(true)
|
11
|
+
|
12
|
+
hash = PasswordEncryptor.encrypt('wrong')
|
13
|
+
expect(PasswordEncryptor.new('password').matches?(hash)).to eq(false)
|
14
|
+
|
15
|
+
hash = "invalid hash"
|
16
|
+
expect(PasswordEncryptor.new('password').matches?(hash)).to eq(false)
|
17
|
+
|
18
|
+
hash = ""
|
19
|
+
expect(PasswordEncryptor.new('password').matches?(hash)).to eq(false)
|
20
|
+
|
21
|
+
hash = nil
|
22
|
+
expect(PasswordEncryptor.new('password').matches?(hash)).to eq(false)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should be possible to verify password with == ' do
|
26
|
+
hash = PasswordEncryptor.encrypt('password')
|
27
|
+
expect(PasswordEncryptor.new('password') == hash).to eq(true)
|
28
|
+
|
29
|
+
hash = PasswordEncryptor.encrypt('wrong')
|
30
|
+
expect(PasswordEncryptor.new('password') == hash).to eq(false)
|
31
|
+
end
|
7
32
|
end
|