github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/utils/htpasswd.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package utils provides generic utility functions.
     5  package utils
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"golang.org/x/crypto/bcrypt"
    11  )
    12  
    13  // GetHtpasswdString converts a username and password to a properly formatted and hashed format for `htpasswd`.
    14  func GetHtpasswdString(username string, password string) (string, error) {
    15  	hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  	return fmt.Sprintf("%s:%s", username, hash), nil
    20  }