joao/internal/op-client/checksum.go
Roberto Hidalgo a0f9d13549 Bump deps, get op item edit with json stdin, finally (#1)
Co-authored-by: Roberto Hidalgo <un@rob.mx>
Reviewed-on: #1
2023-11-26 20:57:42 +00:00

39 lines
836 B
Go

package opclient
import (
"fmt"
"sort"
"strings"
op "github.com/1Password/connect-sdk-go/onepassword"
"golang.org/x/crypto/blake2b"
)
func Checksum(fields []*op.ItemField) string {
newHash, err := blake2b.New256(nil)
if err != nil {
panic(err)
}
df := []string{}
for _, field := range fields {
if field.ID == "password" || field.ID == "notesPlain" || (field.Section != nil && field.Section.ID == "~annotations") {
continue
}
label := field.Label
if field.Section != nil {
sectionID := field.Section.Label
if sectionID == "" {
sectionID = field.Section.ID
}
if sectionID != "" {
label = sectionID + "." + label
}
}
df = append(df, label+field.Value)
}
sort.Strings(df)
newHash.Write([]byte(strings.Join(df, "")))
checksum := newHash.Sum(nil)
return fmt.Sprintf("%x", checksum)
}