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

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