netbird/relay/messages/id.go

30 lines
673 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 = prefixLength + sha256.Size
)
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[:])
2024-07-08 21:53:20 +02:00
var prefixedHash []byte
prefixedHash = append(prefixedHash, prefix...)
prefixedHash = append(prefixedHash, 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
}