diff options
Diffstat (limited to 'object/id/object_format_details.go')
| -rw-r--r-- | object/id/object_format_details.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/object/id/object_format_details.go b/object/id/object_format_details.go new file mode 100644 index 00000000..d0278602 --- /dev/null +++ b/object/id/object_format_details.go @@ -0,0 +1,69 @@ +package id + +import ( + "crypto/sha1" //#nosec:G505 + "crypto/sha256" + "hash" +) + +type objectFormatDetails struct { + name string + size int + sum func([]byte) ObjectID + new func() hash.Hash +} + +func (objectFormat ObjectFormat) details() objectFormatDetails { + return objectFormatTable[objectFormat] +} + +//nolint:gochecknoglobals +var objectFormatTable = [...]objectFormatDetails{ + ObjectFormatUnknown: {}, //nolint:exhaustruct + ObjectFormatSHA1: { + name: "sha1", + size: sha1.Size, + sum: func(data []byte) ObjectID { + sum := sha1.Sum(data) //#nosec G401 + + var id ObjectID + copy(id.data[:], sum[:]) + id.objectFormat = ObjectFormatSHA1 + + return id + }, + new: sha1.New, + }, + ObjectFormatSHA256: { + name: "sha256", + size: sha256.Size, + sum: func(data []byte) ObjectID { + sum := sha256.Sum256(data) + + var id ObjectID + copy(id.data[:], sum[:]) + id.objectFormat = ObjectFormatSHA256 + + return id + }, + new: sha256.New, + }, +} + +// maxObjectIDSize MUST be >= the largest supported object format size. +const maxObjectIDSize = sha256.Size + +var ( + //nolint:gochecknoglobals + objectFormatByName = map[string]ObjectFormat{} + //nolint:gochecknoglobals + supportedObjectFormats []ObjectFormat +) + +func init() { //nolint:gochecknoinits + for objectFormat := ObjectFormatUnknown + 1; int(objectFormat) < len(objectFormatTable); objectFormat++ { + info := &objectFormatTable[objectFormat] + objectFormatByName[info.name] = objectFormat + supportedObjectFormats = append(supportedObjectFormats, objectFormat) + } +} |
