From 02de46bed2c07ff6d49055820471cc80012074a1 Mon Sep 17 00:00:00 2001 From: EndMove Date: Tue, 15 Mar 2022 17:11:31 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20en=20place=20syst=C3=A8me=20de=20hashage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 6 +++ .../rules/readers/RetrieveFileRule.go | 9 ++-- pkg/protocol/rules/readers/SendFileRule.go | 16 +++--- pkg/utils/FileReceiver.go | 1 + pkg/utils/Hasher.go | 34 ------------ pkg/utils/SHA.go | 52 +++++++++++++++++++ resources/AppConfig.json | 2 +- 7 files changed, 72 insertions(+), 48 deletions(-) delete mode 100644 pkg/utils/Hasher.go create mode 100644 pkg/utils/SHA.go diff --git a/cmd/main.go b/cmd/main.go index 770cff3..e92d641 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,6 +8,7 @@ import ( "StoreBackEnd/pkg/protocol/rules/readers" "StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/utils" + "log" "time" ) @@ -16,6 +17,11 @@ const ( ) func main() { + hash, err := utils.HashFileCompare("D:\\HELMoCrypted.png", "bacf89bd47be38ac445d5519c4e945bbea79c4a1f250cb31579e331e67941939") + if err != nil { + log.Fatal(err) + } + println("Hash : ", hash) utils.NetworkLister() // TODO REMOVE println("StoreBackEnd started !") diff --git a/pkg/protocol/rules/readers/RetrieveFileRule.go b/pkg/protocol/rules/readers/RetrieveFileRule.go index 6b080f9..b83780b 100644 --- a/pkg/protocol/rules/readers/RetrieveFileRule.go +++ b/pkg/protocol/rules/readers/RetrieveFileRule.go @@ -40,16 +40,17 @@ func (rule RetrieveFileRule) GetCmd() string { } func (rule RetrieveFileRule) Execute(data string) (*protocol.ProtocolWriterResult, func(reader *bufio.Reader) *protocol.ProtocolWriterResult) { - if rule.Match(data) { values := rule.matcher.Parse(data) hashFileName := values[1] path := fmt.Sprintf("%s/%s", rule.storagePath, hashFileName) if fileinfo, err := os.Stat(path); err == nil { - println("OKOKOKOEKOEKE : ", hashFileName, fmt.Sprintf("%d", fileinfo.Size()), fmt.Sprintf("%x", utils.GenFingerPrint(path))) - - return rule.protocolRepo.ExecuteWriter(writers.RetrieveOkRulePrefix, hashFileName, fmt.Sprintf("%d", fileinfo.Size()), fmt.Sprintf("%x", utils.GenFingerPrint(path))), nil + // Retrieve fingerprint sha256 + fileFingerprint, err := utils.HashFile(path) + if err == nil { + return rule.protocolRepo.ExecuteWriter(writers.RetrieveOkRulePrefix, hashFileName, fmt.Sprintf("%d", fileinfo.Size()), fileFingerprint), nil + } } } return rule.protocolRepo.ExecuteWriter(writers.RetrieveErrorRulePrefix), nil diff --git a/pkg/protocol/rules/readers/SendFileRule.go b/pkg/protocol/rules/readers/SendFileRule.go index 7e8f21d..5669350 100644 --- a/pkg/protocol/rules/readers/SendFileRule.go +++ b/pkg/protocol/rules/readers/SendFileRule.go @@ -65,16 +65,14 @@ func (rule SendFileRule) onRead(fileName string, fileSize int, fingerPrint strin return func(reader *bufio.Reader) *protocol.ProtocolWriterResult { path := fmt.Sprintf("%s/%s", rule.storagePath, fileName) - hasReceive := utils.ReceiveFile(path, fileSize, reader) - - // println("OK ", utils.HashFileCompare(path, fingerPrint)) - - if !hasReceive { - os.Remove(path) // Suppression du fichier - return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix) - } else { - return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix) + if utils.ReceiveFile(path, fileSize, reader) { + hashCompare, err := utils.HashFileCompare(path, fingerPrint) + if err == nil && hashCompare { + return rule.protocolRepo.ExecuteWriter(writers.SendOkRulePrefix) + } } + os.Remove(path) + return rule.protocolRepo.ExecuteWriter(writers.SendErrorRulePrefix) } } diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index aaac2fe..a966c03 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -18,6 +18,7 @@ func ReceiveFile(path string, fileSize int, reader *bufio.Reader) bool { buffer := make([]byte, 1024) currentSize := 0 + // Copy file b, done := copyFile(currentSize, fileSize, reader, buffer, file) if done { return b diff --git a/pkg/utils/Hasher.go b/pkg/utils/Hasher.go deleted file mode 100644 index 36e03b2..0000000 --- a/pkg/utils/Hasher.go +++ /dev/null @@ -1,34 +0,0 @@ -package utils - -import ( - "bufio" - "crypto/sha256" - "fmt" - "io" - "log" - "os" -) - -func HashFileCompare(path string, fingerPrint string) bool { - sum := GenFingerPrint(path) - - return fingerPrint == fmt.Sprintf("%x", sum) -} - -func GenFingerPrint(path string) []byte { - 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) - - return sum -} diff --git a/pkg/utils/SHA.go b/pkg/utils/SHA.go new file mode 100644 index 0000000..80fe877 --- /dev/null +++ b/pkg/utils/SHA.go @@ -0,0 +1,52 @@ +package utils + +/** + * SHA-256 Hashing Class + * + * @author Jérémi N + * @version 1.0 + */ +import ( + "bufio" + "crypto/sha256" + "errors" + "fmt" + "io" + "os" + "strings" +) + +// HashStream Make a fingerprint of a stream. +func HashStream(in *bufio.Reader) (string, error) { + sha := sha256.New() + if _, err := io.Copy(sha, in); err != nil { + return "", errors.New("unable to copy data into hashing system") + } + return fmt.Sprintf("%x", sha.Sum(nil)), nil +} + +// HashFile Make a fingerprint of a file content. +func HashFile(filePath string) (string, error) { + file, err := os.Open(filePath) + if err != nil { + return "", err + } + defer file.Close() + hash, err := HashStream(bufio.NewReader(file)) + if err != nil { + return "", err + } + return hash, nil +} + +// HashFileCompare Make a fingerprint of a file and compare with the renseigned fingerprint. +func HashFileCompare(filePath string, fingerprint string) (bool, error) { + fileFingerprint, err := HashFile(filePath) + if err != nil { + return false, err + } + if strings.Compare(fileFingerprint, fingerprint) == 0 { + return true, nil + } + return false, nil +} diff --git a/resources/AppConfig.json b/resources/AppConfig.json index 7c7aaa1..035cc65 100644 --- a/resources/AppConfig.json +++ b/resources/AppConfig.json @@ -2,7 +2,7 @@ "multicastNetworkInterface": "Loopback Pseudo-Interface 1", "multicastAddress": "224.66.66.1:15502", "multicastSecond": 30, - "domain": "org.lightcont01", + "domain": "orglightcont01", "unicastPort": 58000, "storagePath": "D:\\sbe" } \ No newline at end of file