netbird/relay/messages/id.go

28 lines
615 B
Go
Raw Normal View History

2024-05-23 13:24:02 +02:00
package messages
import (
"crypto/sha256"
"encoding/base64"
"fmt"
2024-05-23 13:24:02 +02:00
)
const (
prefixLength = 4
IDSize = sha256.Size + 4 // 4 is equal with len(prefix)
)
var (
prefix = []byte("sha-") // 4 bytes
2024-05-23 13:24:02 +02:00
)
func HashID(peerID string) ([]byte, string) {
idHash := sha256.Sum256([]byte(peerID))
idHashString := string(prefix) + base64.StdEncoding.EncodeToString(idHash[:])
prefixedHash := append(prefix, idHash[:]...)
return prefixedHash, idHashString
2024-05-23 13:24:02 +02:00
}
func HashIDToString(idHash []byte) string {
return fmt.Sprintf("%s%s", idHash[:prefixLength], base64.StdEncoding.EncodeToString(idHash[prefixLength:]))
2024-05-23 13:24:02 +02:00
}