feat: added dictionary generating tool.

This commit is contained in:
Andrey Parhomenko 2024-09-23 03:06:50 +05:00
parent d90d443017
commit e067b06686
6 changed files with 138 additions and 35 deletions

77
dict/dict.go Normal file
View file

@ -0,0 +1,77 @@
package dict
import (
"os"
"io"
"sync"
"strconv"
"surdeus.su/core/cli/mtool"
"surdeus.su/util/combo/pin"
"surdeus.su/util/combo/tmpl"
)
var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var Tool = mtool.T("dict").Func(Run).Usage(
"<token1 token2 ... [token3]>",
).Desc("make password dictionary from custom tokens")
func Run(
flags *mtool.Flags,
) {
var (
maxLen int
)
flags.IntVar(
&maxLen,
"max", 2, "max tokens in output passwords",
)
tokens := flags.Parse()
if len(tokens) < 2 {
flags.Usage()
os.Exit(1)
}
if len(tokens) > len(alphabet) {
flags.Usage()
os.Exit(1)
}
c := alphabet[:len(tokens)]
var (
wg sync.WaitGroup
)
input, output := io.Pipe()
wg.Add(1)
go func() {
pin.Tool.Run([]string{
"-c", c,
"-m", "-max-reps", "1",
"-max-len", strconv.Itoa(maxLen),
"--",
},
io.Reader(os.Stdin), io.Writer(output),
)
output.Close()
wg.Done()
}()
wg.Add(1)
go func() {
tmpl.Tool.Run(append([]string{
"-c", c,
"--",
}, tokens...),
io.Reader(input), io.Writer(os.Stdout),
)
input.Close()
wg.Done()
}()
wg.Wait()
}

2
go.mod
View file

@ -2,4 +2,4 @@ module surdeus.su/util/combo
go 1.16 go 1.16
require surdeus.su/core/cli v0.6.0 require surdeus.su/core/cli v0.8.0

4
go.sum
View file

@ -2,3 +2,7 @@ golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
surdeus.su/core/cli v0.6.0 h1:ezyBljhVn+lTcIMPblUMsQzaXQ+xzkzZ9ZYLcirZwEI= surdeus.su/core/cli v0.6.0 h1:ezyBljhVn+lTcIMPblUMsQzaXQ+xzkzZ9ZYLcirZwEI=
surdeus.su/core/cli v0.6.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA= surdeus.su/core/cli v0.6.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA=
surdeus.su/core/cli v0.7.0 h1:4IBN6PyZp6BUTC2mrDPudCjUbs/fs6Pv1KG/Ua/vBCI=
surdeus.su/core/cli v0.7.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA=
surdeus.su/core/cli v0.8.0 h1:HGmRqWl6W5DxFX1nVkUpS+cK0BgYYJdYrZY3VTq3C4k=
surdeus.su/core/cli v0.8.0/go.mod h1:r9JtQz3aEJzpYzMaNUNQHJoYkoWKNPi047qhd5uGlmA=

View file

@ -2,14 +2,17 @@ package main
import ( import (
"os" "os"
"surdeus.su/util/combo/tmpl"
"surdeus.su/util/combo/pin"
"surdeus.su/core/cli/mtool" "surdeus.su/core/cli/mtool"
"surdeus.su/util/combo/dict"
"surdeus.su/util/combo/pin"
"surdeus.su/util/combo/tmpl"
) )
var root = mtool.T("combo").Subs( var root = mtool.T("combo").Subs(
tmpl.Tool, tmpl.Tool,
pin.Tool, pin.Tool,
dict.Tool,
) )
func main() { func main() {

View file

@ -40,11 +40,12 @@ func FprintPins(
output io.Writer, output io.Writer,
chars []rune, chars []rune,
length int, length int,
maxReps int,
) { ) {
n := Pow(len(chars), length) n := Pow(len(chars), length)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
pin := GetPin(chars, length, i) pin := GetPin(chars, length, i)
if Fits([]rune(pin), chars, length) { if Fits([]rune(pin), chars, maxReps) {
fmt.Fprintln( fmt.Fprintln(
output, output,
GetPin(chars, length, i), GetPin(chars, length, i),
@ -71,11 +72,19 @@ func Fits(s []rune, chrs []rune, maxReps int) bool {
func Run( func Run(
flags *mtool.Flags, flags *mtool.Flags,
) { ) {
cargs := flags.CustomArgs()
if len(cargs) == 0 {
CustomRun( CustomRun(
os.Stdin, os.Stdin,
os.Stdout, os.Stdout,
flags, flags,
) )
return
}
input := cargs[0].(io.Reader)
output := cargs[1].(io.Writer)
CustomRun(input, output, flags)
} }
func CustomRun( func CustomRun(
@ -103,19 +112,19 @@ func CustomRun(
"COMBO_CHARS", "COMBO_CHARS",
) )
flags.IntVar( flags.IntVar(
&minLength, "min-len", 1, &minLength, "min-len", 4,
"min length of the output pins", "min length of the output pins",
) )
flags.BoolVar( flags.BoolVar(
&mFlag, "m", false, &mFlag, "m", false,
"set the '-min-len' flag value to 1 (overrides the '-min' it)", "set the '-min-len' flag value to 1 (overrides the '-min-len')",
) )
flags.IntVar( flags.IntVar(
&length, "max", 0, &length, "max-len", 0,
"max length of the output pins", "max length of the output pins",
) )
flags.IntVar(&maxReps, "max-reps", 1, "max repeats of the rune." ) flags.IntVar(&maxReps, "max-reps", 4, "max repeats of the rune.")
flags.BoolVar(&rFlag, "r", false, "make the -max-reps value equal to the length of input chars") flags.BoolVar(&rFlag, "r", false, "make the -max-reps value equal to the length of input chars")
_ = flags.Parse() _ = flags.Parse()
@ -145,10 +154,10 @@ func CustomRun(
os.Exit(1) os.Exit(1)
} }
for i := minLength; i <= length; i++ { for i := minLength; i <= length; i++ {
FprintPins(output, chrs, i) FprintPins(output, chrs, i, maxReps)
} }
} else { } else {
FprintPins(output, chrs, length) FprintPins(output, chrs, length, maxReps)
} }
} }

View file

@ -4,6 +4,8 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
//"log"
"os" "os"
"surdeus.su/core/cli/mtool" "surdeus.su/core/cli/mtool"
@ -19,7 +21,15 @@ var (
) )
func Run(flags *mtool.Flags) { func Run(flags *mtool.Flags) {
cargs := flags.CustomArgs()
if len(cargs) == 0 {
CustomRun(os.Stdin, os.Stdout, flags) CustomRun(os.Stdin, os.Stdout, flags)
return
}
input := cargs[0].(io.Reader)
output := cargs[1].(io.Writer)
CustomRun(input, output, flags)
} }
func CustomRun( func CustomRun(