-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (52 loc) · 1.12 KB
/
main.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"eagain.net/go/scram-password/internal/scramble"
)
func run(usernames []string) error {
scanner := bufio.NewScanner(os.Stdin)
for _, username := range usernames {
if !scanner.Scan() {
// error or EOF
err := scanner.Err()
if err == nil {
err = io.ErrUnexpectedEOF
}
return fmt.Errorf("reading passwords from standard input: %w", err)
}
password := scanner.Text()
salted, err := scramble.Hash(username, password)
if err != nil {
return fmt.Errorf("cannot SCRAM hash: %w", err)
}
fmt.Println(salted)
}
return nil
}
var prog = filepath.Base(os.Args[0])
func usage() {
out := flag.CommandLine.Output()
fmt.Fprintf(out, "Usage: %s [OPTS] USERNAME.. <PASSWORDS\n", prog)
fmt.Fprintf(out, "Outputs a SCRAM hashed password.\n")
fmt.Fprintf(out, "\n")
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
log.SetPrefix(prog + ": ")
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 {
log.Printf("missing username")
os.Exit(2)
}
if err := run(flag.Args()); err != nil {
log.Fatalf("error:: %v", err)
}
}