github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/server/ids/ids.go (about) 1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package ids defines identifier types and utility methods specific to the 16 // fleetspeak server and server components. 17 package ids 18 19 import ( 20 "crypto/rand" 21 "encoding/hex" 22 "errors" 23 ) 24 25 // BroadcastID identifies a broadcast using 8 arbitrary bytes and can be used as 26 // a map key. 27 type BroadcastID struct { 28 id string 29 } 30 31 // BytesToBroadcastID creates a BroadcastID based on the provided bytes. 32 func BytesToBroadcastID(b []byte) (BroadcastID, error) { 33 if len(b) == 0 { 34 return BroadcastID{}, nil 35 } 36 if len(b) != 8 { 37 return BroadcastID{}, errors.New("BroadcastID must be 8 bytes") 38 } 39 return BroadcastID{id: string(b)}, nil 40 } 41 42 // RandomBroadcastID creates a new random BroadcastID. 43 func RandomBroadcastID() (BroadcastID, error) { 44 b := make([]byte, 8) 45 if _, err := rand.Read(b); err != nil { 46 return BroadcastID{""}, err 47 } 48 return BroadcastID{string(b)}, nil 49 } 50 51 // StringToBroadcastID creates a BroadcastID based on the provided hex string. 52 func StringToBroadcastID(s string) (BroadcastID, error) { 53 if len(s) != 16 { 54 return BroadcastID{}, errors.New("BroadcastID must be 16 characters") 55 } 56 b, err := hex.DecodeString(s) 57 if err != nil { 58 return BroadcastID{}, err 59 } 60 return BytesToBroadcastID(b) 61 } 62 63 // String implements fmt.Stringer. 64 func (i BroadcastID) String() string { 65 if i.id == "" { 66 return "nil" 67 } 68 return hex.EncodeToString([]byte(i.id)) 69 } 70 71 // Bytes returns bytes stored by BroadcastID 72 func (i BroadcastID) Bytes() []byte { 73 if i.id == "" { 74 return nil 75 } 76 return []byte(i.id) 77 } 78 79 // AllocationID identifies an allocation from a broadcast. 80 type AllocationID struct { 81 id string 82 } 83 84 // RandomAllocationID creates a new random AllocationID. 85 func RandomAllocationID() (AllocationID, error) { 86 b := make([]byte, 8) 87 if _, err := rand.Read(b); err != nil { 88 return AllocationID{""}, err 89 } 90 return AllocationID{string(b)}, nil 91 } 92 93 // Bytes returns bytes stored by AllocationID 94 func (i AllocationID) Bytes() []byte { 95 return []byte(i.id) 96 } 97 98 // String implements fmt.Stringer 99 func (i AllocationID) String() string { 100 return hex.EncodeToString(i.Bytes()) 101 }