Destruction sécurisée de fichier implémentée

This commit is contained in:
Maximilien LEDOUX 2022-03-19 16:59:28 +01:00
parent 14f0c61c6e
commit cd68ba52b9
4 changed files with 100 additions and 5 deletions

View File

@ -4,9 +4,9 @@ import (
"StoreBackEnd/pkg/protocol" "StoreBackEnd/pkg/protocol"
"StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/repository"
"StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils"
"bufio" "bufio"
"fmt" "fmt"
"os"
) )
// EraseFileRulePrefix Identifiant de cette règle // EraseFileRulePrefix Identifiant de cette règle
@ -43,8 +43,9 @@ func (rule EraseFileRule) Execute(data string) (*protocol.ProtocolWriterResult,
if rule.Match(data) { if rule.Match(data) {
values := rule.matcher.Parse(data) values := rule.matcher.Parse(data)
errRemoveFile := os.Remove(fmt.Sprintf("%s/%s", rule.storagePath, values[1])) //errRemoveFile := os.Remove(fmt.Sprintf("%s/%s", rule.storagePath, values[1]))
if errRemoveFile != nil { hasRemoveFile := utils.DeleteFile(fmt.Sprintf("%s\\%s", rule.storagePath, values[1]), 10)
if !hasRemoveFile {
return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil return rule.protocolRepo.ExecuteWriter(writers.EraseErrorRulePrefix), nil
} }

View File

@ -0,0 +1,65 @@
package utils
import (
"StoreBackEnd/pkg/utils/crypto"
"math/rand"
"os"
"strconv"
)
func DeleteFile(filePath string, secureLevel int) bool {
for i := 0; i < secureLevel; i++ {
if !writeRandomFile(filePath) {
return false
}
}
errRemoveFile := os.Remove(filePath)
return errRemoveFile == nil
}
func writeRandomFile(filePath string) bool {
file, errOpen := os.OpenFile(filePath, os.O_RDWR, 0660)
if errOpen != nil {
return false
}
// Fermeture du fichier anticipée (ici afin de le fermer même si copy commet une erreur)
defer file.Close()
//Vérifier que la taille du fichier est plus petite ou plus grande que 1024
//Si plus petite, le buffer prend la taille du fichier
//Sinon le buffer a une taille de 1024
var buffer []byte
fileStat, errStat := file.Stat()
if errStat != nil {
return false
}
if fileStat.Size() < 1024 {
buffer = make([]byte, fileStat.Size())
} else {
buffer = make([]byte, 1024)
}
currentSize := int64(0)
// Copy file
return copyRandomFile(currentSize, fileStat.Size(), buffer, file)
}
func copyRandomFile(currentSize int64, fileSize int64, buffer []byte, file *os.File) bool {
// Retrieving file
src := crypto.NewCryptoSource()
for currentSize < fileSize {
random := rand.New(src)
buffer = []byte(strconv.FormatInt(int64(random.Uint64()), 10))
length, err := file.WriteAt(buffer, currentSize)
if err != nil {
return false
}
currentSize += int64(length)
check := fileSize - currentSize
if 1024 > check && check > 0 {
buffer = make([]byte, check)
}
}
return true
}

View File

@ -0,0 +1,29 @@
package crypto
import (
"crypto/rand"
"encoding/binary"
"log"
)
// Source : https://programming.guide/go/crypto-rand-int.html
type CryptoSource struct{}
func NewCryptoSource() *CryptoSource {
return &CryptoSource{}
}
func (s CryptoSource) Int63() int64 {
return int64(s.Uint64() & ^uint64(1<<63))
}
func (s CryptoSource) Seed(seed int64) {}
func (s CryptoSource) Uint64() (v uint64) {
err := binary.Read(rand.Reader, binary.BigEndian, &v)
if err != nil {
log.Fatal(err)
}
return v
}

View File

@ -1,8 +1,8 @@
{ {
"multicastNetworkInterface" : "wlp1s0", "multicastNetworkInterface" : "Loopback Pseudo-Interface 1",
"multicastAddress" : "224.66.66.1:15502", "multicastAddress" : "224.66.66.1:15502",
"multicastSecond" : 30, "multicastSecond" : 30,
"domain" : "lightcontainerSB01", "domain" : "lightcontainerSB01",
"unicastPort" : 58000, "unicastPort" : 58000,
"storagePath" : "/home/benjamin/sbe" "storagePath" : "C:\\Users\\ledou\\Documents\\sbe"
} }