Mise en place système de hashage

This commit is contained in:
2022-03-15 17:11:31 +01:00
parent 6c33573ad1
commit 02de46bed2
7 changed files with 72 additions and 48 deletions

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