diff options
author | 2021-09-12 10:10:24 +0100 | |
---|---|---|
committer | 2021-09-12 10:10:24 +0100 | |
commit | f6492d12d948507021bbe934de94e87e20464c01 (patch) | |
tree | 6705d6ef6f3c4d70f3b3ebc77c2960d8e508cf37 /vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go | |
parent | Merge pull request #213 from superseriousbusiness/alpine+node_upstep (diff) | |
parent | fix keys used to access storage items (diff) | |
download | gotosocial-f6492d12d948507021bbe934de94e87e20464c01.tar.xz |
Merge pull request #214 from NyaaaWhatsUpDoc/improvement/update-storage-library
add git.iim.gay/grufwub/go-store for storage backend, replacing blob.Storage
Diffstat (limited to 'vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go b/vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go new file mode 100644 index 000000000..59ec8cff5 --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go @@ -0,0 +1,58 @@ +package hashenc + +import ( + "hash" + + "git.iim.gay/grufwub/go-bytes" +) + +// HashEncoder defines an interface for calculating encoded hash sums of binary data +type HashEncoder interface { + // EncodeSum calculates the hash sum of src and encodes (at most) Size() into dst + EncodeSum(dst []byte, src []byte) + + // EncodedSum calculates the encoded hash sum of src and returns data in a newly allocated bytes.Bytes + EncodedSum(src []byte) bytes.Bytes + + // Size returns the expected length of encoded hashes + Size() int +} + +// New returns a new HashEncoder instance based on supplied hash.Hash and Encoder supplying functions +func New(hash hash.Hash, enc Encoder) HashEncoder { + hashSize := hash.Size() + return &henc{ + hash: hash, + hbuf: make([]byte, hashSize), + enc: enc, + size: enc.EncodedLen(hashSize), + } +} + +// henc is the HashEncoder implementation +type henc struct { + hash hash.Hash + hbuf []byte + enc Encoder + size int +} + +func (henc *henc) EncodeSum(dst []byte, src []byte) { + // Hash supplied bytes + henc.hash.Reset() + henc.hash.Write(src) + henc.hbuf = henc.hash.Sum(henc.hbuf[:0]) + + // Encode the hashsum and return a copy + henc.enc.Encode(dst, henc.hbuf) +} + +func (henc *henc) EncodedSum(src []byte) bytes.Bytes { + dst := make([]byte, henc.size) + henc.EncodeSum(dst, src) + return bytes.ToBytes(dst) +} + +func (henc *henc) Size() int { + return henc.size +} |