Skip to content

Commit

Permalink
Optimize for large files
Browse files Browse the repository at this point in the history
  • Loading branch information
JMOrbegoso committed Aug 14, 2023
1 parent 3390a62 commit 50b8e5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
22 changes: 18 additions & 4 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -200,13 +201,26 @@ const (
)

func checkChecksumFile(fileFullPath string) ChecksumFileResult {
fileContent, err := os.ReadFile(fileFullPath)
file, err := os.Open(fileFullPath)
if err != nil {
panic(err)
}

fileChecksum := sha512.Sum512(fileContent)
fileChecksumHex := hex.EncodeToString(fileChecksum[:])
defer file.Close()

// Create a new SHA512 hash object
hash := sha512.New()

// Copy the file content to the hash object
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}

// Get the checksum as a byte slice
fileChecksum := hash.Sum(nil)

// Convert the checksum to a hexadecimal string
hexFileChecksum := hex.EncodeToString(fileChecksum)

// Checksum file
_, err = os.Stat(fileFullPath + ".sha512")
Expand All @@ -222,7 +236,7 @@ func checkChecksumFile(fileFullPath string) ChecksumFileResult {
return Invalid
}

if strings.EqualFold(fileChecksumHex, checksumFileContentString) {
if strings.EqualFold(hexFileChecksum, checksumFileContentString) {
return Match
} else {
return NotMatch
Expand Down
29 changes: 22 additions & 7 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -199,11 +200,13 @@ const (
)

func createChecksumFile(fileFullPath string) ChecksumFileCreationResult {
fileContent, err := os.ReadFile(fileFullPath)
file, err := os.Open(fileFullPath)
if err != nil {
panic(err)
}

defer file.Close()

// Checksum file
_, err = os.Stat(fileFullPath + ".sha512")
if err == nil {
Expand All @@ -221,20 +224,32 @@ func createChecksumFile(fileFullPath string) ChecksumFileCreationResult {
}
}

fileChecksum := sha512.Sum512(fileContent)
fileChecksumHex := hex.EncodeToString(fileChecksum[:])
// Create a new SHA512 hash object
hash := sha512.New()

// Copy the file content to the hash object
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}

// Get the checksum as a byte slice
fileChecksum := hash.Sum(nil)

// Convert the checksum to a hexadecimal string
hexFileChecksum := hex.EncodeToString(fileChecksum)

// Create checksum file
checksumFile, err := os.Create(fileFullPath + ".sha512")
if err != nil {
return ErrorCreating
}

_, err = checksumFile.WriteString(fileChecksumHex)
if err != nil {
defer checksumFile.Close()

// Write the file checksum on the checksum file
if _, err := checksumFile.WriteString(hexFileChecksum); err != nil {
return ErrorCreating
}

checksumFile.Close()

return Created
}

0 comments on commit 50b8e5b

Please sign in to comment.