func SetPasswordHash(file, user, hash string) error {
	if len(hash) == 0 {
		return errors.New("you might want to rethink your hashing algorithm, it left you with an empty hash")
	}
	passwords, err := ParseHtpasswdFile(file)
	if err != nil {
		return err
	}
	passwords[user] = hash
	return passwords.WriteToFile(file)
}
SetPasswordHash directly set a hash for a user in a file
package htpasswd
import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)
type HashedPasswords map[string]string
type HashAlgorithm string
const (
	HashAPR1 = "apr1"
	HashBCrypt = "bcrypt"
	HashSHA = "sha"
)
const (
	PasswordSeparator = ":"
	LineSeparator = "\n"
)
const MaxHtpasswdFilesize = 8 * 1024 * 1024
var ErrNotExist = errors.New("user did not exist in file")
func (hp HashedPasswords) Bytes() (passwordBytes []byte) {
	passwordBytes = []byte{}
	for name, hash := range hp {
		passwordBytes = append(passwordBytes, []byte(name+PasswordSeparator+hash+LineSeparator)...)
	}
	return passwordBytes
}
func (hp HashedPasswords) WriteToFile(file string) error {
	return ioutil.WriteFile(file, hp.Bytes(), 0644)
}
func (hp HashedPasswords) SetPassword(name, password string, hashAlgorithm HashAlgorithm) (err error) {
	if len(password) == 0 {
		return errors.New("passwords must not be empty, if you want to delete a user call RemoveUser")
	}
	hash := ""
	prefix := ""
	switch hashAlgorithm {
	case HashAPR1:
		hash, err = hashApr1(password)
	case HashBCrypt:
		hash, err = hashBcrypt(password)
	case HashSHA:
		prefix = "{SHA}"
		hash = hashSha(password)
	}
	if err != nil {
		return err
	}
	hp[name] = prefix + hash
	return nil
}
func ParseHtpasswdFile(file string) (passwords HashedPasswords, err error) {
	htpasswdBytes, err := ioutil.ReadFile(file)
	if err != nil {
		return
	}
	if len(htpasswdBytes) > MaxHtpasswdFilesize {
		err = errors.New("this file is too large, use a database instead")
		return
	}
	return ParseHtpasswd(htpasswdBytes)
}
func ParseHtpasswd(htpasswdBytes []byte) (passwords HashedPasswords, err error) {
	lines := strings.Split(string(htpasswdBytes), LineSeparator)
	passwords = make(map[string]string)
	for lineNumber, line := range lines {
		line = strings.Trim(line, " ")
		if len(line) == 0 {
			continue
		}
		parts := strings.Split(line, PasswordSeparator)
		if len(parts) != 2 {
			err = errors.New(fmt.Sprintln("invalid line", lineNumber+1, "unexpected number of parts split by", PasswordSeparator, len(parts), "instead of 2 in\"", line, "\""))
			return
		}
		for i, part := range parts {
			parts[i] = strings.Trim(part, " ")
		}
		_, alreadyExists := passwords[parts[0]]
		if alreadyExists {
			err = errors.New("invalid htpasswords file - user " + parts[0] + " was already defined")
			return
		}
		passwords[parts[0]] = parts[1]
	}
	return
}
func SetHtpasswdHash(file, name, hash string) error {
	passwords, err := ParseHtpasswdFile(file)
	if err != nil {
		return err
	}
	passwords[name] = hash
	return passwords.WriteToFile(file)
}
func RemoveUser(file, user string) error {
	passwords, err := ParseHtpasswdFile(file)
	if err != nil {
		return err
	}
	_, ok := passwords[user]
	if !ok {
		return ErrNotExist
	}
	delete(passwords, user)
	return passwords.WriteToFile(file)
}