2024-05-23 13:24:02 +02:00
|
|
|
package messages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2024-06-25 17:36:04 +02:00
|
|
|
"fmt"
|
2024-05-23 13:24:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-06-25 17:36:04 +02:00
|
|
|
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))
|
2024-06-25 17:36:04 +02:00
|
|
|
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 {
|
2024-06-25 17:36:04 +02:00
|
|
|
return fmt.Sprintf("%s%s", idHash[:prefixLength], base64.StdEncoding.EncodeToString(idHash[prefixLength:]))
|
2024-05-23 13:24:02 +02:00
|
|
|
}
|