Skip to content

Commit 05a5ff2

Browse files
committed
Accept legacy hyphenated SHA-1/256/512 algorithm names
1 parent 407a166 commit 05a5ff2

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

util/digest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func ComputeFileChecksum(filePath, algorithm string) (string, error) {
2121
switch strings.ToUpper(algorithm) {
2222
case "MD5":
2323
hasher = md5.New()
24-
case "SHA1":
24+
case "SHA1", "SHA-1":
2525
hasher = sha1.New()
26-
case "SHA256":
26+
case "SHA256", "SHA-256":
2727
hasher = sha256.New()
2828
case "SHA384", "SHA-384":
2929
hasher = sha512.New384()
30-
case "SHA512":
30+
case "SHA512", "SHA-512":
3131
hasher = sha512.New()
3232
case "SHA3-256":
3333
hasher = sha3.New256()

util/digest_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ var _ = Describe("Digest", func() {
5555
})
5656
})
5757

58+
Context("with legacy hyphenated SHA-256 algorithm name", func() {
59+
It("should compute the SHA-256 digest of the file", func() {
60+
const testFileContent = "test file content"
61+
os.WriteFile(testFile.Name(), []byte(testFileContent), 0644)
62+
digest, err := util.ComputeFileChecksum(testFilePath, "SHA-256")
63+
testutil.ExpectNoErrorAndResult(err, digest, "60f5237ed4049f0382661ef009d2bc42e48c3ceb3edb6600f7024e7ab3b838f3")
64+
})
65+
})
66+
67+
Context("with legacy hyphenated SHA-1 algorithm name", func() {
68+
It("should compute the SHA-1 digest of the file", func() {
69+
const testFileContent = "test file content"
70+
os.WriteFile(testFile.Name(), []byte(testFileContent), 0644)
71+
digest, err := util.ComputeFileChecksum(testFilePath, "SHA-1")
72+
testutil.ExpectNoErrorAndResult(err, digest, "9032bbc224ed8b39183cb93b9a7447727ce67f9d")
73+
})
74+
})
75+
76+
Context("with legacy hyphenated SHA-512 algorithm name", func() {
77+
It("should compute the SHA-512 digest of the file", func() {
78+
const testFileContent = "test file content"
79+
os.WriteFile(testFile.Name(), []byte(testFileContent), 0644)
80+
digest, err := util.ComputeFileChecksum(testFilePath, "SHA-512")
81+
testutil.ExpectNoErrorAndResult(err, digest, "6543084c6981d2d3e44d295d20adee4e5fa3fc1bb2b7d3ac80ca456b0141dfd75657ba40ea5ae398824b3a871c8b7762c8bdbcbc252fa7f358d33d90ee455d86")
82+
})
83+
})
84+
5885
Context("with SHA3-256 algorithm", func() {
5986
It("should compute the SHA3-256 digest of the file", func() {
6087
const testFileContent = "test file content"

0 commit comments

Comments
 (0)