Files
valpass/example/test.go

76 lines
1.6 KiB
Go
Raw Normal View History

2024-10-11 17:06:47 +02:00
package main
import (
"bufio"
"fmt"
"log"
"os"
2025-12-06 20:25:43 +01:00
"codeberg.org/scip/valpass/v2"
2024-10-11 17:06:47 +02:00
)
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%%
2024-10-13 13:31:57 +02:00
Dictionary match false false %t
2024-10-11 17:06:47 +02:00
------------------------------------------------------------------
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,
)
2024-10-11 17:08:32 +02:00
if !res.Ok {
os.Exit(1)
}
2024-10-11 17:06:47 +02:00
}
func ReadDict(path string) []string {
file, err := os.Open(path)
if err != nil {
panic(err)
}
2025-12-06 20:25:43 +01:00
defer func() {
if err := file.Close(); err != nil {
log.Fatal(err)
}
}()
2024-10-11 17:06:47 +02:00
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}