2019-03-23 22:40:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Philipp Haefelfinger (http://www.haefelfinger.ch/). All Rights Reserved.
|
|
|
|
* This application is licensed under GPLv2. See the LICENSE file in the root directory of the project.
|
|
|
|
*/
|
|
|
|
|
2019-02-27 22:11:47 +01:00
|
|
|
package localFileStructure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-03-17 22:58:49 +01:00
|
|
|
func CalculateFileCheckSums(filePath string) (string, error) {
|
2019-02-27 23:26:18 +01:00
|
|
|
file, err := os.Open(filePath)
|
2019-02-27 22:11:47 +01:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Could not open file %s", filePath)
|
|
|
|
return "", err
|
|
|
|
}
|
2019-02-27 23:26:18 +01:00
|
|
|
defer file.Close()
|
2019-02-27 22:11:47 +01:00
|
|
|
|
2019-02-27 23:26:18 +01:00
|
|
|
hash := md5.New()
|
|
|
|
if _, err := io.Copy(hash, file); err != nil {
|
2019-02-27 22:11:47 +01:00
|
|
|
logrus.Errorf("Could calculate md5 sum of file %s", filePath)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-02-27 23:26:18 +01:00
|
|
|
md5sum := fmt.Sprintf("%x", hash.Sum(nil))
|
2019-02-27 22:11:47 +01:00
|
|
|
|
|
|
|
logrus.Tracef("Calculated md5 sum of %s - %s", filePath, md5sum)
|
|
|
|
|
|
|
|
return md5sum, nil
|
|
|
|
}
|