Files
trivy/pkg/notification/identifier.go
Owen Rumney 5a0bf9ed31 feat(cli): Add available version checking (#8553)
Signed-off-by: Owen Rumney <owen.rumney@aquasec.com>
Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
Co-authored-by: Itay <itay@itaysk.com>
Co-authored-by: simar7 <1254783+simar7@users.noreply.github.com>
2025-05-28 08:09:16 +00:00

48 lines
883 B
Go

package notification
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"os"
"strings"
)
func getMachineIdentifier() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
interfaces, err := net.Interfaces()
if err != nil {
return "", err
}
var macAddr string
for _, iface := range interfaces {
if iface.HardwareAddr.String() != "" {
macAddr = iface.HardwareAddr.String()
break
}
}
identifier := fmt.Sprintf("%s-%s-%s", hostname, macAddr, strings.ToLower(hostname))
return identifier, nil
}
func generateMachineHash(identifier string) string {
hash := sha256.New()
hash.Write([]byte(identifier))
return hex.EncodeToString(hash.Sum(nil))
}
func uniqueIdentifier() string {
identifier, err := getMachineIdentifier()
if err != nil {
return ""
}
return generateMachineHash(fmt.Sprintf("trivy-%s", identifier))
}