aboutsummaryrefslogtreecommitdiff
path: root/object
diff options
context:
space:
mode:
Diffstat (limited to 'object')
-rw-r--r--object/id/algorithm.go1
-rw-r--r--object/id/algorithm_parse.go8
-rw-r--r--object/id/errors.go3
-rw-r--r--object/id/object_id_ops.go42
-rw-r--r--object/id/object_id_parse.go49
5 files changed, 101 insertions, 2 deletions
diff --git a/object/id/algorithm.go b/object/id/algorithm.go
index 1b02cbc4..efe5f4c5 100644
--- a/object/id/algorithm.go
+++ b/object/id/algorithm.go
@@ -17,6 +17,7 @@ const (
)
// SupportedAlgorithms returns all object ID algorithms supported by furgit.
+//
// Labels: Mut-No.
func SupportedAlgorithms() []Algorithm {
return supportedAlgorithms
diff --git a/object/id/algorithm_parse.go b/object/id/algorithm_parse.go
index 7349288f..63db32dc 100644
--- a/object/id/algorithm_parse.go
+++ b/object/id/algorithm_parse.go
@@ -6,3 +6,11 @@ func ParseAlgorithm(s string) (Algorithm, bool) {
return algo, ok
}
+
+// ParseSignatureHeaderName parses one canonical signature header name
+// such as "gpgsig" or "gpgsig-sha256" to its respective algorithm.
+func ParseSignatureHeaderName(s string) (Algorithm, bool) {
+ algo, ok := algorithmBySignatureHeaderName[s]
+
+ return algo, ok
+}
diff --git a/object/id/errors.go b/object/id/errors.go
index 11c471ed..5f9a80af 100644
--- a/object/id/errors.go
+++ b/object/id/errors.go
@@ -7,6 +7,5 @@ var (
ErrInvalidAlgorithm = errors.New("objectid: invalid algorithm")
// ErrInvalidObjectID indicates malformed object ID data.
- // ErrInvalidObjectID = errors.New("objectid: invalid object id")
- // TODO
+ ErrInvalidObjectID = errors.New("objectid: invalid object id")
)
diff --git a/object/id/object_id_ops.go b/object/id/object_id_ops.go
new file mode 100644
index 00000000..1ca0c6df
--- /dev/null
+++ b/object/id/object_id_ops.go
@@ -0,0 +1,42 @@
+package id
+
+import (
+ "bytes"
+ "encoding/hex"
+)
+
+// Algorithm returns the object ID's hash algorithm.
+func (id ObjectID) Algorithm() Algorithm {
+ return id.algo
+}
+
+// Bytes returns a copy of the object ID bytes.
+func (id ObjectID) Bytes() []byte {
+ size := id.Algorithm().Size()
+
+ return append([]byte(nil), id.data[:size]...)
+}
+
+// RawBytes returns a direct byte slice view of the object ID bytes.
+//
+// Prefer [ObjectID.Bytes] except for when it is a performance bottleneck.
+//
+// Labels: Mut-No.
+func (id *ObjectID) RawBytes() []byte {
+ size := id.Algorithm().Size()
+
+ return id.data[:size:size]
+}
+
+// Compare lexicographically compares two object IDs
+// by their canonical byte representation.
+func (id ObjectID) Compare(other ObjectID) int {
+ return bytes.Compare(id.RawBytes(), other.RawBytes())
+}
+
+// String returns the canonical hex representation.
+func (id ObjectID) String() string {
+ size := id.Algorithm().Size()
+
+ return hex.EncodeToString(id.data[:size])
+}
diff --git a/object/id/object_id_parse.go b/object/id/object_id_parse.go
new file mode 100644
index 00000000..b8e7da68
--- /dev/null
+++ b/object/id/object_id_parse.go
@@ -0,0 +1,49 @@
+package id
+
+import (
+ "encoding/hex"
+ "fmt"
+)
+
+// FromBytes builds an object ID from raw bytes for the specified algorithm.
+func FromBytes(algo Algorithm, b []byte) (ObjectID, error) {
+ var id ObjectID
+ if algo.Size() == 0 {
+ return id, ErrInvalidAlgorithm
+ }
+
+ if len(b) != algo.Size() {
+ return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), algo.Size())
+ }
+
+ copy(id.data[:], b)
+ id.algo = algo
+
+ return id, nil
+}
+
+// FromHex parses an object ID from hex for the specified algorithm.
+func FromHex(algo Algorithm, s string) (ObjectID, error) {
+ var id ObjectID
+ if algo.Size() == 0 {
+ return id, ErrInvalidAlgorithm
+ }
+
+ if len(s)%2 != 0 {
+ return id, fmt.Errorf("%w: odd hex length %d", ErrInvalidObjectID, len(s))
+ }
+
+ if len(s) != algo.HexLen() {
+ return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), algo.HexLen())
+ }
+
+ decoded, err := hex.DecodeString(s)
+ if err != nil {
+ return id, fmt.Errorf("%w: decode: %w", ErrInvalidObjectID, err)
+ }
+
+ copy(id.data[:], decoded)
+ id.algo = algo
+
+ return id, nil
+}