tstor/src/host/torrent/id.go
royalcat d30ef6cc9c
Some checks failed
mkdocs / mkdocs (push) Failing after 2s
CodeQL / Analyze (go) (push) Failing after 2s
CodeQL / Analyze (javascript) (push) Failing after 2s
docker / build-docker (inux/arm/v6) (push) Failing after 14s
docker / build-docker (linux/386) (push) Failing after 13s
docker / build-docker (linux/amd64) (push) Has started running
docker / build-docker (linux/arm/v7) (push) Failing after 14s
docker / build-docker (linux/arm64) (push) Has been cancelled
rework 2 (working)
2023-10-16 12:18:40 +03:00

30 lines
427 B
Go

package torrent
import (
"crypto/rand"
"os"
)
var emptyBytes [20]byte
func GetOrCreatePeerID(p string) ([20]byte, error) {
idb, err := os.ReadFile(p)
if err == nil {
var out [20]byte
copy(out[:], idb)
return out, nil
}
if !os.IsNotExist(err) {
return emptyBytes, err
}
var out [20]byte
_, err = rand.Read(out[:])
if err != nil {
return emptyBytes, err
}
return out, os.WriteFile(p, out[:], 0755)
}