mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* Update copyright comment for go files Signed-off-by: nwneisen <nwneisen@gmail.com> * Update copyright in assortment of file types Signed-off-by: nwneisen <nwneisen@gmail.com> * Remove missed copyright date Signed-off-by: nwneisen <nwneisen@gmail.com>
35 lines
754 B
Go
35 lines
754 B
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package bcrypt provides a hasher implementation utilising bcrypt.
|
|
package bcrypt
|
|
|
|
import (
|
|
"github.com/mainflux/mainflux/users"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const cost int = 10
|
|
|
|
var _ users.Hasher = (*bcryptHasher)(nil)
|
|
|
|
type bcryptHasher struct{}
|
|
|
|
// New instantiates a bcrypt-based hasher implementation.
|
|
func New() users.Hasher {
|
|
return &bcryptHasher{}
|
|
}
|
|
|
|
func (bh *bcryptHasher) Hash(pwd string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), cost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(hash), nil
|
|
}
|
|
|
|
func (bh *bcryptHasher) Compare(plain, hashed string) error {
|
|
return bcrypt.CompareHashAndPassword([]byte(hashed), []byte(plain))
|
|
}
|