Passed
Push — main ( f6597a...58b626 )
by Yume
01:31 queued 12s
created

auth.GenerateEncryptedPassword   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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