summaryrefslogtreecommitdiffhomepage
path: root/auth.go
blob: 53e8db0037f1065ea6a2de55f48692be82e74111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"syscall"

	"golang.org/x/crypto/bcrypt"
	"golang.org/x/term"
)

func ReadPasswordFromUserInput() (string, error) {
	bs, err := term.ReadPassword(int(syscall.Stdin))
	if err != nil {
		return "", err
	} else {
		return string(bs), nil
	}
}

func GeneratePasswordHash(password string) (string, error) {
	bs, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	} else {
		return string(bs), nil
	}
}

func VerifyPassword(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}