Skip to content

Commit 93b627c

Browse files
committed
main: fix reading passwords longer than 64 bytes
Fixes #284
1 parent c210b50 commit 93b627c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

main.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"crypto"
@@ -1777,11 +1778,20 @@ pick a random, unguessable password, preferably at least 12 characters.
17771778
17781779
`)
17791780
fmt.Printf("password: ")
1780-
buf := make([]byte, 64)
1781-
n, err := os.Stdin.Read(buf)
1782-
xcheckf(err, "reading stdin")
1783-
pw := string(buf[:n])
1784-
pw = strings.TrimSuffix(strings.TrimSuffix(pw, "\r\n"), "\n")
1781+
scanner := bufio.NewScanner(os.Stdin)
1782+
// The default splitter for scanners is one that splits by lines, so we
1783+
// don't have to set up another one here.
1784+
1785+
// We discard the return value of Scan() since failing to tokenize could
1786+
// either mean reaching EOF but no newline (which can be legitimate if the
1787+
// CLI was programatically called to set the password, but with no trailing
1788+
// newline), or an actual error. We can distinguish between the two by
1789+
// calling Err() since it will return nil if it were EOF, but the actual
1790+
// error if not.
1791+
scanner.Scan()
1792+
xcheckf(scanner.Err(), "reading stdin")
1793+
// No need to trim, the scanner does not return the token in the output.
1794+
pw := scanner.Text()
17851795
if len(pw) < 8 {
17861796
log.Fatal("password must be at least 8 characters")
17871797
}

0 commit comments

Comments
 (0)