1
0
mirror of https://github.com/chylex/SMTP-Relay.git synced 2025-04-09 08:15:44 +02:00

Change hasher utility to accept passwords from stdin

This commit is contained in:
chylex 2023-07-02 19:54:43 +02:00
parent 7a6128c724
commit a4a97716e2
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 22 additions and 8 deletions

View File

@ -2,5 +2,5 @@
To run the hasher, do like this
```bash
$ go run hasher.go hunter2
$ go run hasher.go
```

View File

@ -1,6 +1,7 @@
package main
import (
"bufio"
"fmt"
"os"
@ -8,15 +9,28 @@ import (
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage: hasher PASSWORD")
os.Exit(1)
}
password := os.Args[1]
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
password := scanner.Text()
if len(password) == 0 {
break
}
hash, err := hash(password)
if err != nil {
fmt.Printf("Error generating hash: %s\n", err)
} else {
fmt.Printf("%s: %s\n", password, hash)
}
}
}
func hash(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
fmt.Fprintln(os.Stderr, "Error generating hash: %s", err)
return "", err
} else {
return string(hash), nil
}
fmt.Println(string(hash))
}