Skip to content

Commit f4eae19

Browse files
committed
digest: register SHA1 digest algorithm
This algorithm is used by Git commit SHAs, and opens up the digest API to work with these references. Signed-off-by: Hidde Beydals <[email protected]>
1 parent a72badf commit f4eae19

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

internal/digest/digest.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package digest
1818

1919
import (
20+
"crypto"
21+
_ "crypto/sha1"
2022
_ "crypto/sha256"
2123
_ "crypto/sha512"
2224
"fmt"
@@ -25,8 +27,19 @@ import (
2527
_ "github.com/opencontainers/go-digest/blake3"
2628
)
2729

28-
// Canonical is the primary digest algorithm used to calculate checksums.
29-
const Canonical = digest.SHA256
30+
const (
31+
SHA1 digest.Algorithm = "sha1"
32+
)
33+
34+
var (
35+
// Canonical is the primary digest algorithm used to calculate checksums.
36+
Canonical = digest.SHA256
37+
)
38+
39+
func init() {
40+
// Register SHA-1 algorithm for support of e.g. Git commit SHAs.
41+
digest.RegisterAlgorithm(SHA1, crypto.SHA1)
42+
}
3043

3144
// AlgorithmForName returns the digest algorithm for the given name, or an
3245
// error of type digest.ErrDigestUnsupported if the algorithm is unavailable.

internal/digest/writer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func benchmarkMultiDigesterWrite(b *testing.B, algos []digest.Algorithm, pSize i
102102
func BenchmarkMultiDigester_Write(b *testing.B) {
103103
const pSize = 1024 * 2
104104

105+
b.Run("sha1", func(b *testing.B) {
106+
benchmarkMultiDigesterWrite(b, []digest.Algorithm{SHA1}, pSize)
107+
})
108+
105109
b.Run("sha256", func(b *testing.B) {
106110
benchmarkMultiDigesterWrite(b, []digest.Algorithm{digest.SHA256}, pSize)
107111
})

0 commit comments

Comments
 (0)