1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

28 lines
609 B
Go
Raw Normal View History

2019-05-10 11:17:55 +02:00
package gobgp
import (
2019-05-10 11:17:55 +02:00
// Standard imports
"crypto/sha1"
"fmt"
2019-05-10 11:17:55 +02:00
"io"
// External imports
api "github.com/osrg/gobgp/api"
// Internal imports
)
2021-10-26 18:05:11 +02:00
// PeerHash calculates a peer hash
2019-05-10 11:17:55 +02:00
func PeerHash(peer *api.Peer) string {
return PeerHashWithASAndAddress(peer.State.PeerAs, peer.State.NeighborAddress)
}
2021-10-26 18:05:11 +02:00
// PeerHashWithASAndAddress creates a peer hash (sha1) from
// the ASN and the address.
func PeerHashWithASAndAddress(asn uint32, address string) string {
h := sha1.New()
2021-10-26 18:05:11 +02:00
io.WriteString(h, fmt.Sprintf("%v", asn))
io.WriteString(h, address)
sum := h.Sum(nil)
2019-05-10 11:17:55 +02:00
return fmt.Sprintf("%x", sum[0:5])
}