mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
provisioning: only update dashboard if hash of json changed
This commit is contained in:
26
pkg/util/md5.go
Normal file
26
pkg/util/md5.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Md5Sum calculates the md5sum of a stream
|
||||
func Md5Sum(reader io.Reader) (string, error) {
|
||||
var returnMD5String string
|
||||
hash := md5.New()
|
||||
if _, err := io.Copy(hash, reader); err != nil {
|
||||
return returnMD5String, err
|
||||
}
|
||||
hashInBytes := hash.Sum(nil)[:16]
|
||||
returnMD5String = hex.EncodeToString(hashInBytes)
|
||||
return returnMD5String, nil
|
||||
}
|
||||
|
||||
// Md5Sum calculates the md5sum of a string
|
||||
func Md5SumString(input string) (string, error) {
|
||||
buffer := strings.NewReader(input)
|
||||
return Md5Sum(buffer)
|
||||
}
|
||||
17
pkg/util/md5_test.go
Normal file
17
pkg/util/md5_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMd5Sum(t *testing.T) {
|
||||
input := "dont hash passwords with md5"
|
||||
|
||||
have, err := Md5SumString(input)
|
||||
if err != nil {
|
||||
t.Fatal("expected err to be nil")
|
||||
}
|
||||
|
||||
want := "2d6a56c82d09d374643b926d3417afba"
|
||||
if have != want {
|
||||
t.Fatalf("expected: %s got: %s", want, have)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user