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
|
2024-07-08 17:55:48 +02:00
|
|
|
IDSize = prefixLength + sha256.Size
|
2024-06-25 17:36:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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[:])
|
2024-07-08 21:53:20 +02:00
|
|
|
var prefixedHash []byte
|
|
|
|
prefixedHash = append(prefixedHash, prefix...)
|
|
|
|
prefixedHash = append(prefixedHash, idHash[:]...)
|
2024-06-25 17:36:04 +02:00
|
|
|
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
|
|
|
}
|