github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/v1/hash_nonwasm.go (about)

     1  //go:build !wasm
     2  // +build !wasm
     3  
     4  package v1
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  
    10  	"github.com/cespare/xxhash/v2"
    11  	"golang.org/x/exp/maps"
    12  )
    13  
    14  func computeAPICallHash(apiName string, arguments map[string]string) (string, error) {
    15  	hasher := xxhash.New()
    16  	_, err := hasher.WriteString(apiName)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  
    21  	_, err = hasher.WriteString(":")
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  
    26  	keys := maps.Keys(arguments)
    27  	sort.Strings(keys)
    28  
    29  	for _, key := range keys {
    30  		_, err = hasher.WriteString(key)
    31  		if err != nil {
    32  			return "", err
    33  		}
    34  
    35  		_, err = hasher.WriteString(":")
    36  		if err != nil {
    37  			return "", err
    38  		}
    39  
    40  		_, err = hasher.WriteString(arguments[key])
    41  		if err != nil {
    42  			return "", err
    43  		}
    44  
    45  		_, err = hasher.WriteString(";")
    46  		if err != nil {
    47  			return "", err
    48  		}
    49  	}
    50  
    51  	return fmt.Sprintf("%x", hasher.Sum(nil)), nil
    52  }