Files
valpass/example/test.go
2025-12-06 20:25:43 +01:00

76 lines
1.6 KiB
Go

package main
import (
"bufio"
"fmt"
"log"
"os"
"codeberg.org/scip/valpass/v2"
)
const template string = `
Metric Random Threshhold Result
------------------------------------------------------------------
Compression rate 0%% min %d%% %d%%
Character distribution 100%% min %0.2f%% %0.2f%%
Character entropy 8.0 bits/char min %0.2f %0.2f bits/char
Character redundancy 0.0%% max %0.2f%% %0.2f%%
Dictionary match false false %t
------------------------------------------------------------------
Validation response %t
`
func main() {
opts := valpass.Options{
Compress: valpass.MIN_COMPRESS,
CharDistribution: valpass.MIN_DIST,
Entropy: valpass.MIN_ENTROPY,
Dictionary: &valpass.Dictionary{Words: ReadDict("t/american-english")},
}
res, err := valpass.Validate(os.Args[1], opts)
if err != nil {
log.Fatal(err)
}
fmt.Printf(
template,
opts.Compress,
res.Compress,
opts.CharDistribution,
res.CharDistribution,
opts.Entropy,
res.Entropy,
100-opts.CharDistribution,
100-res.CharDistribution,
res.DictionaryMatch,
res.Ok,
)
if !res.Ok {
os.Exit(1)
}
}
func ReadDict(path string) []string {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer func() {
if err := file.Close(); err != nil {
log.Fatal(err)
}
}()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}