auth.ComparePasswords   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package auth
2
3
import (
4
	"github.com/memnix/memnix-rest/config"
5
	"github.com/pkg/errors"
6
)
7
8
// GenerateEncryptedPassword generates a password hash using the crypto helper
9
func GenerateEncryptedPassword(password string) ([]byte, error) {
10
	hash, err := config.CryptoHelper.Hash(password)
11
	if err != nil {
12
		return nil, err
13
	}
14
	return hash, nil
15
}
16
17
// ComparePasswords compares a hashed password with its possible plaintext equivalent.
18
//
19
// password is the plaintext password to verify.
20
// hash is the bcrypt hashed password.
21
//
22
// Returns true if the password matches, false if it does not.
23
// Returns nil on success, or an error on failure.
24
func ComparePasswords(password string, hash []byte) (bool, error) {
25
	return config.CryptoHelper.Verify(password, hash)
26
}
27
28
// VerifyPassword verifies a password
29
// Returns an error if the password is invalid
30
func VerifyPassword(password string) error {
31
	// Convert password to byte array
32
	passwordBytes := []byte(password)
33
	if len(passwordBytes) < config.MinPasswordLength {
34
		return errors.New("password too short")
35
	}
36
37
	if len(passwordBytes) > config.MaxPasswordLength {
38
		return errors.New("password too long")
39
	}
40
41
	return nil
42
}
43