github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/cache/hash.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cache
     5  
     6  import (
     7  	"crypto/sha256"
     8  	"encoding/hex"
     9  
    10  	"github.com/juju/errors"
    11  	"gopkg.in/yaml.v2"
    12  )
    13  
    14  // hash returns a hash of the yaml serialized settings.
    15  // If the settings are not able to be serialized an error is returned.
    16  func hash(settings map[string]interface{}) (string, error) {
    17  	bytes, err := yaml.Marshal(settings)
    18  	if err != nil {
    19  		return "", errors.Trace(err)
    20  	}
    21  	hash := sha256.New()
    22  	_, err = hash.Write(bytes)
    23  	if err != nil {
    24  		return "", errors.Trace(err)
    25  	}
    26  	return hex.EncodeToString(hash.Sum(nil)), nil
    27  }