summaryrefslogtreecommitdiff
path: root/vendor/git.iim.gay/grufwub/go-hashenc
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2021-09-12 10:10:24 +0100
committerLibravatar GitHub <noreply@github.com>2021-09-12 10:10:24 +0100
commitf6492d12d948507021bbe934de94e87e20464c01 (patch)
tree6705d6ef6f3c4d70f3b3ebc77c2960d8e508cf37 /vendor/git.iim.gay/grufwub/go-hashenc
parentMerge pull request #213 from superseriousbusiness/alpine+node_upstep (diff)
parentfix keys used to access storage items (diff)
downloadgotosocial-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')
-rw-r--r--vendor/git.iim.gay/grufwub/go-hashenc/README.md1
-rw-r--r--vendor/git.iim.gay/grufwub/go-hashenc/enc.go42
-rw-r--r--vendor/git.iim.gay/grufwub/go-hashenc/hash.go136
-rw-r--r--vendor/git.iim.gay/grufwub/go-hashenc/hashenc.go58
4 files changed, 237 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-hashenc/README.md b/vendor/git.iim.gay/grufwub/go-hashenc/README.md
new file mode 100644
index 000000000..e885d0bb2
--- /dev/null
+++ b/vendor/git.iim.gay/grufwub/go-hashenc/README.md
@@ -0,0 +1 @@
+HashEncoder provides a means of quickly hash-summing and encoding data \ No newline at end of file
diff --git a/vendor/git.iim.gay/grufwub/go-hashenc/enc.go b/vendor/git.iim.gay/grufwub/go-hashenc/enc.go
new file mode 100644
index 000000000..39b342843
--- /dev/null
+++ b/vendor/git.iim.gay/grufwub/go-hashenc/enc.go
@@ -0,0 +1,42 @@
+package hashenc
+
+import (
+ "encoding/base32"
+ "encoding/base64"
+ "encoding/hex"
+)
+
+// Encoder defines an interface for encoding binary data
+type Encoder interface {
+ // Encode encodes the data at src into dst
+ Encode(dst []byte, src []byte)
+
+ // EncodedLen returns the encoded length for input data of supplied length
+ EncodedLen(int) int
+}
+
+// Base32 returns a new base32 Encoder
+func Base32() Encoder {
+ return base32.StdEncoding.WithPadding(base64.NoPadding)
+}
+
+// Base64 returns a new base64 Encoder
+func Base64() Encoder {
+ return base64.URLEncoding.WithPadding(base64.NoPadding)
+}
+
+// Hex returns a new hex Encoder
+func Hex() Encoder {
+ return &hexEncoder{}
+}
+
+// hexEncoder simply provides an empty receiver to satisfy Encoder
+type hexEncoder struct{}
+
+func (*hexEncoder) Encode(dst []byte, src []byte) {
+ hex.Encode(dst, src)
+}
+
+func (*hexEncoder) EncodedLen(len int) int {
+ return hex.EncodedLen(len)
+}
diff --git a/vendor/git.iim.gay/grufwub/go-hashenc/hash.go b/vendor/git.iim.gay/grufwub/go-hashenc/hash.go
new file mode 100644
index 000000000..d291fa2f2
--- /dev/null
+++ b/vendor/git.iim.gay/grufwub/go-hashenc/hash.go
@@ -0,0 +1,136 @@
+package hashenc
+
+import (
+ "crypto/md5"
+ "crypto/sha1"
+ "crypto/sha256"
+ "crypto/sha512"
+ "hash"
+ "sync"
+)
+
+// Hash defines a pooled hash.Hash implementation
+type Hash interface {
+ // Hash ensures we implement the base hash.Hash implementation
+ hash.Hash
+
+ // Release resets the Hash and places it back in the pool
+ Release()
+}
+
+// poolHash is our Hash implementation, providing a hash.Hash and a pool to return to
+type poolHash struct {
+ hash.Hash
+ pool *sync.Pool
+}
+
+func (h *poolHash) Release() {
+ h.Reset()
+ h.pool.Put(h)
+}
+
+// SHA512Pool defines a pool of SHA512 hashes
+type SHA512Pool interface {
+ // SHA512 returns a Hash implementing the SHA512 hashing algorithm
+ SHA512() Hash
+}
+
+// NewSHA512Pool returns a new SHA512Pool implementation
+func NewSHA512Pool() SHA512Pool {
+ p := &sha512Pool{}
+ p.New = func() interface{} {
+ return &poolHash{
+ Hash: sha512.New(),
+ pool: &p.Pool,
+ }
+ }
+ return p
+}
+
+// sha512Pool is our SHA512Pool implementation, simply wrapping sync.Pool
+type sha512Pool struct {
+ sync.Pool
+}
+
+func (p *sha512Pool) SHA512() Hash {
+ return p.Get().(Hash)
+}
+
+// SHA256Pool defines a pool of SHA256 hashes
+type SHA256Pool interface {
+ // SHA256 returns a Hash implementing the SHA256 hashing algorithm
+ SHA256() Hash
+}
+
+// NewSHA256Pool returns a new SHA256Pool implementation
+func NewSHA256Pool() SHA256Pool {
+ p := &sha256Pool{}
+ p.New = func() interface{} {
+ return &poolHash{
+ Hash: sha256.New(),
+ pool: &p.Pool,
+ }
+ }
+ return p
+}
+
+// sha256Pool is our SHA256Pool implementation, simply wrapping sync.Pool
+type sha256Pool struct {
+ sync.Pool
+}
+
+func (p *sha256Pool) SHA256() Hash {
+ return p.Get().(Hash)
+}
+
+// SHA1Pool defines a pool of SHA1 hashes
+type SHA1Pool interface {
+ SHA1() Hash
+}
+
+// NewSHA1Pool returns a new SHA1Pool implementation
+func NewSHA1Pool() SHA1Pool {
+ p := &sha1Pool{}
+ p.New = func() interface{} {
+ return &poolHash{
+ Hash: sha1.New(),
+ pool: &p.Pool,
+ }
+ }
+ return p
+}
+
+// sha1Pool is our SHA1Pool implementation, simply wrapping sync.Pool
+type sha1Pool struct {
+ sync.Pool
+}
+
+func (p *sha1Pool) SHA1() Hash {
+ return p.Get().(Hash)
+}
+
+// MD5Pool defines a pool of MD5 hashes
+type MD5Pool interface {
+ MD5() Hash
+}
+
+// NewMD5Pool returns a new MD5 implementation
+func NewMD5Pool() MD5Pool {
+ p := &md5Pool{}
+ p.New = func() interface{} {
+ return &poolHash{
+ Hash: md5.New(),
+ pool: &p.Pool,
+ }
+ }
+ return p
+}
+
+// md5Pool is our MD5Pool implementation, simply wrapping sync.Pool
+type md5Pool struct {
+ sync.Pool
+}
+
+func (p *md5Pool) MD5() Hash {
+ return p.Get().(Hash)
+}
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
+}