-
Notifications
You must be signed in to change notification settings - Fork 4
/
gosgp.go
115 lines (92 loc) · 3.03 KB
/
gosgp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
/*
gosgp is a command line tool to generate SuperGenPass passwords
for a given domain. gosgp won't trim the relevant parts from your
URL string, so please only use domains.
Copyright (C) 2014 Jochen Breuer <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import (
"flag"
"fmt"
"os"
"golang.org/x/crypto/ssh/terminal"
)
const about = "gosgp - repeatable password generator (golang-port of supergenpass.com)"
func main() {
var (
opts = struct {
domain string
length int
lockMemory bool
sha bool
}{length: 10, lockMemory: true}
password, domain, generated []byte
sgpMd5 = SGPMd5{md5: NewNonleakyMd5()}
sgpSha512 = SGPSha512{sha512: NewNonleakySha512()}
hasher SGP = &sgpMd5
err error
)
flag.StringVar(&opts.domain, "domain", opts.domain, "domain")
flag.IntVar(&opts.length, "length", opts.length, "length")
flag.BoolVar(&opts.sha, "sha", opts.sha, "use sha512 instead of md5")
flag.BoolVar(&opts.lockMemory, "lock", opts.lockMemory, "lock memory")
flag.Usage = usage
flag.Parse()
if opts.lockMemory {
lockMemory()
}
if opts.sha {
hasher = &sgpSha512
}
defer hasher.ZeroBytes()
if opts.length > hasher.MaxLength() {
exit(1, errorRequestTooLong(opts.length, hasher.MaxLength()))
}
if opts.length < minPasswordLength {
exit(1, errorRequestTooShort(opts.length, minPasswordLength))
}
if opts.domain == "" {
if len(flag.Args()) > 0 {
opts.domain = flag.Arg(0)
} else {
fmt.Print(" domain: ")
fmt.Scanf("%s", &opts.domain)
}
}
fmt.Printf("password: ")
if password, err = terminal.ReadPassword(int(os.Stdin.Fd())); err != nil {
exit(1, err)
}
// []byte(...) creates a copy of the string-bytes (strings are read-only)
// so, we have to zero both the string and the copy
domain = []byte(opts.domain)
zeroString(&opts.domain)
generated = make([]byte, opts.length)
defer zeroBytes(generated)
err = SupergenPass(generated, hasher, password, domain)
hasher.ZeroBytes()
zeroBytes(password, domain)
fmt.Println()
if err != nil {
exit(2, err)
}
// fmt.Printf() might keep (unreachable) data in buffers around
os.Stdout.Write(generated)
zeroBytes(generated)
fmt.Println()
}
func usage() {
fmt.Fprintln(os.Stderr, about, fmt.Sprintf("usage of %s:\n", os.Args[0]))
flag.PrintDefaults()
}