2022-03-13 12:29:05 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HashFileCompare(path string, fingerPrint string) bool {
|
2022-03-15 11:58:06 +01:00
|
|
|
sum := GenFingerPrint(path)
|
|
|
|
|
|
|
|
return fingerPrint == fmt.Sprintf("%x", sum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenFingerPrint(path string) []byte {
|
2022-03-13 12:29:05 +01:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
input := bufio.NewReader(f)
|
|
|
|
|
|
|
|
hash := sha256.New()
|
|
|
|
if _, err := io.Copy(hash, input); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
sum := hash.Sum(nil)
|
|
|
|
|
2022-03-15 11:58:06 +01:00
|
|
|
return sum
|
2022-03-13 12:29:05 +01:00
|
|
|
}
|