diff --git a/auth/auth.go b/auth/auth.go index 688ec2c6..f2f8d0b5 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -131,7 +131,7 @@ func SignoutUser(r *http.Request, user *models.User) { } func SignupUser(ctx context.Context, username string, name string, password string) (*models.User, error) { - passwordHash, err := hashPassword(password) + passwordHash, err := HashPassword(password) if err != nil { return nil, fmt.Errorf("Cannot signup user, failed to create hashed password: %w", err) } @@ -182,7 +182,7 @@ func findUser(ctx context.Context, user_id int) (*models.User, error) { return user, err } -func hashPassword(password string) (string, error) { +func HashPassword(password string) (string, error) { bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) return string(bytes), err } @@ -205,7 +205,7 @@ func validatePassword(password, hash string) bool { } func validateUser(ctx context.Context, username string, password string) (*models.User, error) { - passwordHash, err := hashPassword(password) + passwordHash, err := HashPassword(password) if err != nil { return nil, fmt.Errorf("Failed to hash password: %w", err) } diff --git a/cmd/passwordgen/main.go b/cmd/passwordgen/main.go new file mode 100644 index 00000000..e0b3aad4 --- /dev/null +++ b/cmd/passwordgen/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "log" + "os" + + "github.com/Gleipnir-Technology/nidus-sync/auth" +) + +func main() { + var password string + scanValue("Please enter your password : ", &password) + + hash, err := auth.HashPassword(password) + if err != nil { + fmt.Printf("Failed to hash password: %v\n", err) + os.Exit(1) + } + + fmt.Println("Password:", password) + fmt.Println("Hash: ", hash) + +} + +func scanValue(message string, result *string) { + fmt.Printf(message) + scanner := bufio.NewScanner(os.Stdin) + if ok := scanner.Scan(); !ok { + log.Fatal(errors.New("Failed to scan input")) + } + *result = scanner.Text() +}