Skip to content

Commit

Permalink
roblox/bootstrapper: offload md5 check for /util/
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Nov 3, 2023
1 parent fb8ee52 commit 08452aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
19 changes: 2 additions & 17 deletions roblox/bootstrapper/deploy.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
package bootstrapper

import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"

"github.com/vinegarhq/vinegar/util"
)

func (p *Package) Verify(src string) error {
log.Printf("Verifying Package %s (%s)", p.Name, p.Checksum)

pkgFile, err := os.Open(src)
if err != nil {
return err
}
defer pkgFile.Close()

hash := md5.New()
if _, err := io.Copy(hash, pkgFile); err != nil {
return err
}

if p.Checksum != hex.EncodeToString(hash.Sum(nil)) {
return fmt.Errorf("package %s (%s) is corrupted", p.Name, src)
if err := util.VerifyFileMD5(src, p.Checksum); err != nil {
return fmt.Errorf("verify package %s: %w", p.Name, err)
}

return nil
Expand Down
29 changes: 29 additions & 0 deletions util/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
)

func VerifyFileMD5(name string, sum string) error {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()

h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return err
}
fsum := hex.EncodeToString(h.Sum(nil))

if sum != fsum {
return fmt.Errorf("file %s checksum mismatch: %s != %s", name, sum, fsum)
}

return nil
}

0 comments on commit 08452aa

Please sign in to comment.