diff options
| author | 2026-04-02 07:34:09 +0000 | |
|---|---|---|
| committer | 2026-04-02 07:34:09 +0000 | |
| commit | f805d0ba947e0aa39ca8040cf71b7e0fad3de919 (patch) | |
| tree | b683eb327f94a02a17c406631089fa1f8bc28b9a /object/id/algorithm_ops.go | |
| parent | object/id: Add algorithm (diff) | |
| signature | No signature | |
object/id: Add algorithm stuff
Diffstat (limited to 'object/id/algorithm_ops.go')
| -rw-r--r-- | object/id/algorithm_ops.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/object/id/algorithm_ops.go b/object/id/algorithm_ops.go new file mode 100644 index 00000000..b746dc5f --- /dev/null +++ b/object/id/algorithm_ops.go @@ -0,0 +1,60 @@ +package id + +import "hash" + +// EmptyTree returns the object ID of +// an empty tree ("tree 0\x00") +// for this algorithm. +func (algo Algorithm) EmptyTree() ObjectID { + return algo.info().emptyTree +} + +// HexLen returns the encoded hexadecimal length. +func (algo Algorithm) HexLen() int { + return algo.Size() * 2 +} + +// Size returns the hash size in bytes. +func (algo Algorithm) Size() int { + return algo.info().size +} + +// New returns a new hash.Hash for this algorithm. +func (algo Algorithm) New() (hash.Hash, error) { + newFn := algo.info().new + if newFn == nil { + return nil, ErrInvalidAlgorithm + } + + return newFn(), nil +} + +// PackHashID returns the Git pack/rev hash-id encoding for this algorithm. +// +// Unknown algorithms return 0. +func (algo Algorithm) PackHashID() uint32 { + return algo.info().packHashID +} + +// SignatureHeaderName returns the signature header name for this algorithm. +func (algo Algorithm) SignatureHeaderName() string { + return algo.info().signatureHeaderName +} + +// String returns the canonical algorithm name. +func (algo Algorithm) String() string { + return algo.info().name +} + +// Sum computes an object ID from raw data using the selected algorithm. +func (algo Algorithm) Sum(data []byte) ObjectID { + return algo.info().sum(data) +} + +// Zero returns the all-zero object ID for this algorithm. +func (algo Algorithm) Zero() ObjectID { + return ObjectID{ + algo: algo, + data: [maxObjectIDSize]byte{}, + } +} |
