Skip to content

Commit 7c280e1

Browse files
authored
feat: add md5 calculation for package uploads (#865)
* fix: refactor Jamf Protect functions * feat: add MD5 hash calculation for package uploads
1 parent 377ae53 commit 7c280e1

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

sdk/jamfpro/shared_helpers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package jamfpro
55

66
import (
7+
"crypto/md5"
78
"encoding/hex"
89
"fmt"
910
"io"
@@ -63,3 +64,19 @@ func CalculateSHA3_512(filePath string) (string, error) {
6364

6465
return hex.EncodeToString(hash.Sum(nil)), nil
6566
}
67+
68+
// CalculateMD5 calculates the MD5 hash of the supplied file in the path.
69+
func CalculateMD5(filePath string) (string, error) {
70+
file, err := os.Open(filePath)
71+
if err != nil {
72+
return "", fmt.Errorf("failed to open file for MD5 calculation: %v", err)
73+
}
74+
defer file.Close()
75+
76+
hash := md5.New()
77+
if _, err := io.Copy(hash, file); err != nil {
78+
return "", fmt.Errorf("failed to calculate MD5: %v", err)
79+
}
80+
81+
return hex.EncodeToString(hash.Sum(nil)), nil
82+
}

sdk/jamfpro/util_package_uploader.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ and verifies the uploaded package’s SHA3_512 hash.
1515
1616
Steps:
1717
1. Calculate the SHA3_512 of the local file before uploading (initialHash).
18-
2. Create package metadata in Jamf Pro (Package name, etc.).
19-
3. Upload the actual file to Jamf Pro.
20-
4. Poll Jamf Pro until the uploaded package’s SHA3_512 hash is present or until max retries are reached.
21-
5. Compare Jamf Pro’s hash with the initially calculated hash to ensure data integrity.
18+
2. Calculate the MD5 hash of the file for metadata.
19+
3. Create package metadata in Jamf Pro (Package name, etc.).
20+
4. Upload the actual file to Jamf Pro.
21+
5. Poll Jamf Pro until the uploaded package’s SHA3_512 hash is present or until max retries are reached.
22+
6. Compare Jamf Pro’s hash with the initially calculated hash to ensure data integrity.
2223
2324
Arguments:
2425
- filePath: The path to the local package file to be uploaded.
@@ -34,13 +35,20 @@ Usage:
3435
- Call DoPackageUpload with the path of your package and that resource data.
3536
*/
3637
func (c *Client) DoPackageUpload(filePath string, packageData *ResourcePackage) (*ResponsePackageCreatedAndUpdated, error) {
38+
pkgName := filepath.Base(filePath)
39+
3740
initialHash, err := CalculateSHA3_512(filePath)
3841
if err != nil {
3942
return nil, fmt.Errorf("failed to calculate initial SHA3_512: %v", err)
4043
}
4144

42-
pkgName := filepath.Base(filePath)
45+
md5Hash, err := CalculateMD5(filePath)
46+
if err != nil {
47+
return nil, fmt.Errorf("failed to calculate MD5: %v", err)
48+
}
49+
4350
packageData.FileName = pkgName
51+
packageData.MD5 = md5Hash
4452
metadataResponse, err := c.CreatePackage(*packageData)
4553
if err != nil {
4654
return nil, fmt.Errorf("failed to create package metadata in Jamf Pro: %v", err)

0 commit comments

Comments
 (0)