github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/apis/v1/types_hash.go (about)

     1  // Copyright 2022 Ewout Prangsma
     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  //     http://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  // Author Ewout Prangsma
    16  //
    17  
    18  package v1
    19  
    20  import (
    21  	"crypto/sha1"
    22  	fmt "fmt"
    23  	"hash"
    24  	"io"
    25  	"sort"
    26  	"strconv"
    27  )
    28  
    29  // HashTo appends the device into to the given hash.
    30  func (s *Device) HashTo(h hash.Hash) {
    31  	io.WriteString(h, s.GetAddress())
    32  	io.WriteString(h, string(s.GetId()))
    33  	io.WriteString(h, string(s.GetType()))
    34  }
    35  
    36  // HashTo appends the object into to the given hash.
    37  func (s *Object) HashTo(h hash.Hash) {
    38  	io.WriteString(h, string(s.GetId()))
    39  	io.WriteString(h, string(s.GetType()))
    40  	for _, conn := range s.GetConnections() {
    41  		conn.HashTo(h)
    42  	}
    43  	configKeys := make([]string, 0, len(s.GetConfiguration()))
    44  	for k := range s.GetConfiguration() {
    45  		configKeys = append(configKeys, string(k))
    46  	}
    47  	sort.Strings(configKeys)
    48  	for _, k := range configKeys {
    49  		io.WriteString(h, k)
    50  		io.WriteString(h, s.GetConfiguration()[ObjectConfigKey(k)])
    51  	}
    52  }
    53  
    54  // HashTo appends the connection into to the given hash.
    55  func (s *Connection) HashTo(h hash.Hash) {
    56  	io.WriteString(h, string(s.GetKey()))
    57  	for _, pin := range s.GetPins() {
    58  		pin.HashTo(h)
    59  	}
    60  	configKeys := make([]string, 0, len(s.GetConfiguration()))
    61  	for k := range s.GetConfiguration() {
    62  		configKeys = append(configKeys, string(k))
    63  	}
    64  	sort.Strings(configKeys)
    65  	for _, k := range configKeys {
    66  		io.WriteString(h, k)
    67  		io.WriteString(h, s.GetConfiguration()[ConfigKey(k)])
    68  	}
    69  }
    70  
    71  // HashTo appends the pin into to the given hash.
    72  func (s *DevicePin) HashTo(h hash.Hash) {
    73  	io.WriteString(h, string(s.GetDeviceId()))
    74  	io.WriteString(h, strconv.FormatUint(uint64(s.GetDeviceIndex()), 10))
    75  }
    76  
    77  // HashTo appends the config into to the given hash.
    78  func (s *LocalWorkerConfig) HashTo(h hash.Hash) {
    79  	io.WriteString(h, s.GetAlias())
    80  	for _, dev := range s.GetDevices() {
    81  		dev.HashTo(h)
    82  	}
    83  	for _, obj := range s.GetObjects() {
    84  		obj.HashTo(h)
    85  	}
    86  }
    87  
    88  // Sha1 returns a sha1 hash of the config.
    89  func (s *LocalWorkerConfig) Sha1() string {
    90  	h := sha1.New()
    91  	s.HashTo(h)
    92  	return fmt.Sprintf("%x", h.Sum(nil))
    93  }