Mise en place système de hashage

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-15 17:11:31 +01:00
parent 6c33573ad1
commit 02de46bed2
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
7 changed files with 72 additions and 48 deletions

View File

@ -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 !")

View File

@ -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

View File

@ -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)
}
}

View File

@ -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

View File

@ -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
}

52
pkg/utils/SHA.go Normal file
View File

@ -0,0 +1,52 @@
package utils
/**
* SHA-256 Hashing Class
*
* @author Jérémi N <contact@enmove.eu>
* @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
}

View File

@ -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"
}