github.com/aavshr/aws-sdk-go@v1.41.3/service/s3/s3crypto/hash_io.go (about)

     1  package s3crypto
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"hash"
     6  	"io"
     7  )
     8  
     9  // lengthReader returns the content length
    10  type lengthReader interface {
    11  	GetContentLength() int64
    12  }
    13  
    14  type contentLengthReader struct {
    15  	contentLength int64
    16  	body          io.Reader
    17  }
    18  
    19  func newContentLengthReader(f io.Reader) *contentLengthReader {
    20  	return &contentLengthReader{body: f}
    21  }
    22  
    23  func (r *contentLengthReader) Read(b []byte) (int, error) {
    24  	n, err := r.body.Read(b)
    25  	if err != nil && err != io.EOF {
    26  		return n, err
    27  	}
    28  	r.contentLength += int64(n)
    29  	return n, err
    30  }
    31  
    32  func (r *contentLengthReader) GetContentLength() int64 {
    33  	return r.contentLength
    34  }
    35  
    36  type sha256Writer struct {
    37  	sha256 []byte
    38  	hash   hash.Hash
    39  	out    io.Writer
    40  }
    41  
    42  func newSHA256Writer(f io.Writer) *sha256Writer {
    43  	return &sha256Writer{hash: sha256.New(), out: f}
    44  }
    45  func (r *sha256Writer) Write(b []byte) (int, error) {
    46  	r.hash.Write(b)
    47  	return r.out.Write(b)
    48  }
    49  
    50  func (r *sha256Writer) GetValue() []byte {
    51  	return r.hash.Sum(nil)
    52  }