Skip to content

Commit

Permalink
re-add GetBOMChecksumAlgorithm function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophie Wigmore authored and sophiewigmore committed Aug 27, 2021
1 parent 641e164 commit 80303bf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package packit

import (
"fmt"
"strings"
"time"
)

Expand Down Expand Up @@ -48,6 +50,21 @@ func (a algorithm) alg() algorithm {
return a
}

// GetBOMChecksumAlgorithm takes in an algorithm string, and reasonably tries
// to figure out the equivalent CycloneDX-supported algorithm field name.
// It returns an error if no reasonable supported format is found.
// Supported formats:
// { 'MD5'| 'SHA-1'| 'SHA-256'| 'SHA-384'| 'SHA-512'| 'SHA3-256'| 'SHA3-384'| 'SHA3-512'| 'BLAKE2b-256'| 'BLAKE2b-384'| 'BLAKE2b-512'| 'BLAKE3'}
func GetBOMChecksumAlgorithm(alg string) (algorithm, error) {
for _, a := range []algorithm{SHA256, SHA1, SHA384, SHA512, SHA3256, SHA3384, SHA3512, BLAKE2B256, BLAKE2B384, BLAKE2B512, BLAKE3, MD5} {
if strings.EqualFold(string(a), alg) || strings.EqualFold(strings.ReplaceAll(string(a), "-", ""), alg) {
return a, nil
}
}

return "", fmt.Errorf("failed to get supported BOM checksum algorithm: %s is not valid", alg)
}

const (
SHA256 algorithm = "SHA-256"
SHA1 algorithm = "SHA-1"
Expand Down
11 changes: 10 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ cache = true

context("when there are bom entries in the build metadata", func() {
it("persists a build.toml", func() {
algorithm512, err := packit.GetBOMChecksumAlgorithm("sha512")
Expect(err).ToNot(HaveOccurred())

packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
return packit.BuildResult{
Build: packit.BuildMetadata{
Expand All @@ -344,7 +347,7 @@ cache = true
},
Source: packit.BOMSource{
Checksum: packit.BOMChecksum{
Algorithm: packit.SHA512,
Algorithm: algorithm512,
Hash: "some-source-sha",
},
},
Expand Down Expand Up @@ -1067,5 +1070,11 @@ api = "0.4"
})
})
})
context("when the attempted BOM checksum algorithm is not supported", func() {
it("persists a build.toml", func() {
_, err := packit.GetBOMChecksumAlgorithm("RANDOM-ALG")
Expect(err).To(MatchError("failed to get supported BOM checksum algorithm: RANDOM-ALG is not valid"))
})
})
})
}

0 comments on commit 80303bf

Please sign in to comment.