portal/cid/cid.go

17 lines
391 B
Go
Raw Normal View History

2023-05-04 08:14:47 +00:00
package cid
import (
"encoding/binary"
2023-05-04 08:14:47 +00:00
"github.com/multiformats/go-multibase"
)
2023-05-06 08:38:09 +00:00
func Encode(hash [32]byte, size uint64) (string, error) {
sizeBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(sizeBytes, size)
2023-05-04 08:14:47 +00:00
prefixedHash := append([]byte{0x26, 0x1f}, hash[:]...)
prefixedHash = append(prefixedHash, sizeBytes...)
2023-05-04 08:14:47 +00:00
return multibase.Encode(multibase.Base58BTC, prefixedHash)
}