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

pkg/crypto/crypto.go   A

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A crypto.*Crypto.Hash 0 2 1
A crypto.*Crypto.Verify 0 2 1
1
package crypto
2
3
var CryptoHelper = BcryptCrypto{}
4
5
// ICrypto is the interface for the crypto methods
6
// It's used to abstract the crypto methods used in the application
7
// so that they can be easily swapped out if needed
8
type ICrypto interface {
9
	// Hash hashes a password using the configured crypto method
10
	Hash(password string) ([]byte, error)
11
	// Verify compares a crypto hashed password with its possible plaintext equivalent
12
	Verify(password string, hash []byte) (bool, error)
13
}
14
15
// Crypto is the struct that holds the crypto methods
16
type Crypto struct {
17
	crypto ICrypto
18
}
19
20
// Hash hashes a password using the configured crypto method
21
// password is the plaintext password to hash.
22
// Returns the hashed password, or an error on failure.
23
func (c *Crypto) Hash(password string) ([]byte, error) {
24
	return c.crypto.Hash(password) //nolint:wrapcheck
25
}
26
27
// Verify compares a crypto hashed password with its possible plaintext equivalent
28
// password is the plaintext password to verify.
29
// hash is the bcrypt hashed password.
30
// Returns nil on success, or an error on failure.
31
// Returns true if the password matches, false if it does not.
32
func (c *Crypto) Verify(password string, hash []byte) (bool, error) {
33
	return c.crypto.Verify(password, hash) //nolint:wrapcheck
34
}
35