pcmt/modules/localbreach/breach.go
surtur eff9b1e0eb
All checks were successful
continuous-integration/drone/push Build is passing
go(deps): use gopkg.in/yaml.v3
2023-08-10 19:10:57 +02:00

65 lines
1.4 KiB
Go

// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: AGPL-3.0-only
package localbreach
import (
"bytes"
"io"
"os"
"time"
"gopkg.in/yaml.v3"
)
type BreachDataSchema struct {
Name string `yaml:"name" validate:"required,name"`
Time time.Time `yaml:"time"`
IsVerified bool `yaml:"isVerified"`
ContainsPasswords bool `yaml:"containsPasswds"`
ContainsHashes bool `yaml:"containsHashes"`
HashType string `yaml:"hashType"`
HashSalted bool `yaml:"hashSalted"`
HashPeppered bool `yaml:"hashPeppered"`
ContainsUsernames bool `yaml:"containsUsernames"`
ContainsEmails bool `yaml:"containsEmails"`
Data any `yaml:"data"`
}
// Load loads local breach data.
func Load(path string) (*[]BreachDataSchema, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
lb, err := loadLocalBreach(b)
if err != nil {
return nil, err
}
return lb, nil
}
func loadLocalBreach(b []byte) (*[]BreachDataSchema, error) {
r := bytes.NewReader(b)
decoder := yaml.NewDecoder(r)
lb := make([]BreachDataSchema, 0)
var bds BreachDataSchema
for {
if err := decoder.Decode(&bds); err != nil {
if err != io.EOF {
return nil, err
}
break // get out when there are no more documents to read.
}
lb = append(lb, bds)
}
return &lb, nil
}