blob: 9eca8beb872d7ca9b96d3e7165fd140351fa9b92 [file] [log] [blame]
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package discovery
import (
"crypto/rand"
"encoding/hex"
"v.io/v23/verror"
)
var (
errAdIdSizeMismatch = verror.Register("v.io/v23/discovery.errAdIdSizeMismatch", verror.NoRetry, "id string size mismatch")
zeroId = AdId{}
)
// IsValid reports whether the id is valid.
func (id AdId) IsValid() bool {
return id != zeroId
}
// String returns the string corresponding to the id.
func (id AdId) String() string {
return hex.EncodeToString(id[:])
}
// NewId returns a new random id.
func NewAdId() (AdId, error) {
var id AdId
if _, err := rand.Read(id[:]); err != nil {
return zeroId, err
}
return id, nil
}
// Parse decodes the hexadecimal string into id.
func ParseAdId(s string) (AdId, error) {
decoded, err := hex.DecodeString(s)
if err != nil {
return zeroId, err
}
var id AdId
if len(decoded) != len(id) {
return zeroId, verror.New(errAdIdSizeMismatch, nil)
}
copy(id[:], decoded)
return id, nil
}