| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |