cyberark/secrets-provider-for-k8s

View on GitHub
pkg/utils/checksum.go

Summary

Maintainability
A
0 mins
Test Coverage
B
85%
package utils

import (
    "bytes"
    "crypto/sha256"
    "io"
)

type Checksum []byte

func FileChecksum(buf *bytes.Buffer) (Checksum, error) {
    hash := sha256.New()
    bufCopy := bytes.NewBuffer(buf.Bytes())
    if _, err := io.Copy(hash, bufCopy); err != nil {
        return nil, err
    }
    checksum := hash.Sum(nil)
    return checksum, nil
}

func ContentHasChanged(groupName string, newChecksum Checksum, prevChecksums map[string]Checksum) bool {
    if prevChecksum, exists := prevChecksums[groupName]; exists {
        if bytes.Equal(newChecksum, prevChecksum) {
            return false
        }
    }
    return true
}