-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (45 loc) · 1.28 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
package main
import (
"math"
"os"
"strconv"
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.RFC3339,
NoColor: false,
})
if len(os.Args) != 3 {
log.Fatal().Msg("must specify two integer values")
}
// n is the number of possible characters, such as 26 lowercase letters,
// or 10 numeric digits
n, err := strconv.ParseUint(os.Args[1], 10, 64)
if err != nil {
log.Fatal().Err(err).Msgf("could not parse %q as integer", os.Args[1])
}
// l is the length of the password
l, err := strconv.ParseUint(os.Args[2], 10, 64)
if err != nil {
log.Fatal().Err(err).Msgf("could not parse %q as integer", os.Args[2])
}
// cache the value of n^l
value := math.Pow(float64(n), float64(l))
// tp will add commas to indicate thousands
tp := message.NewPrinter(language.English)
// vs is the value string, e.g., 1,234,567 instead of 1234567
vs := tp.Sprintf("%d", int64(value))
// e is the entropy bits
e := math.Log2(value)
// debugging info for checking the math
log.Debug().Msgf("%d ^ %d = %s", n, l, vs)
log.Debug().Msgf("log2( %s ) = %f", vs, e)
// print the entropy bits
log.Info().Msgf("%f", e)
}