summaryrefslogtreecommitdiffhomepage
path: root/auth.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-10-09 08:42:33 +0900
committernsfisis <nsfisis@gmail.com>2023-10-09 08:42:33 +0900
commitceb264cb65f4a62531e11b3ce666f931074b778a (patch)
treeb727df20ca1c6ef35c4dcea2798f29e19a2035c9 /auth.go
parentd137a764d050e3d5296da2830a32f6d83bdb364f (diff)
downloadmioproxy-ceb264cb65f4a62531e11b3ce666f931074b778a.tar.gz
mioproxy-ceb264cb65f4a62531e11b3ce666f931074b778a.tar.zst
mioproxy-ceb264cb65f4a62531e11b3ce666f931074b778a.zip
support basic authv0.2.0
Diffstat (limited to 'auth.go')
-rw-r--r--auth.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/auth.go b/auth.go
new file mode 100644
index 0000000..53e8db0
--- /dev/null
+++ b/auth.go
@@ -0,0 +1,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
+}