checksum

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

type Algorithm

type Algorithm string
const (
	MD4         Algorithm = "md4"
	MD5         Algorithm = "md5"
	SHA1        Algorithm = "sha1"
	SHA224      Algorithm = "sha224"
	SHA256      Algorithm = "sha256"
	SHA384      Algorithm = "sha384"
	SHA512      Algorithm = "sha512"
	SHA512_224  Algorithm = "sha512224"
	SHA512_256  Algorithm = "sha512256"
	SHA3_224    Algorithm = "sha3224"
	SHA3_256    Algorithm = "sha3256"
	SHA3_384    Algorithm = "sha3384"
	SHA3_512    Algorithm = "sha3512"
	RIPEMD160   Algorithm = "ripemd160"
	BLAKE2S_256 Algorithm = "blake2s256"
	BLAKE2B_256 Algorithm = "blake2b256"
	BLAKE2B_384 Algorithm = "blake2b384"
	BLAKE2B_512 Algorithm = "blake2b512"
	ADLER32     Algorithm = "adler32"
	CRC32       Algorithm = "crc32"
	CRC64       Algorithm = "crc64"
	FNV         Algorithm = "fnv"
	FNV1        Algorithm = "fnv1"
	FNV1A       Algorithm = "fnv1a"
)

func GetAlgorithm

func GetAlgorithm(name string) (algo Algorithm, ok bool)

type DeferTrailerReader

type DeferTrailerReader struct {
	// contains filtered or unexported fields
}

DeferTrailerReader is io.Reader that concatenates body and trailer readers and substitutes trailer values to request just after body data was drawn out. This is suitable when trailer values are unknown before the whole body was fully read. For example -- get the checksum of huge body without copying it to an intermediate buffer.

Example
package main

import (
	"crypto"
	"io"
	"net/http"
	"strings"

	"github.com/bdragon300/tusgo/checksum"
)

func main() {
	req, err := http.NewRequest(http.MethodPost, "http://example.com", nil)
	if err != nil {
		panic(err)
	}

	b64hash := checksum.NewHashBase64ReadWriter(crypto.SHA1.New(), "sha1 ")
	body := io.TeeReader(strings.NewReader("Hello world!"), b64hash)
	trailers := map[string]io.Reader{"Checksum": body}
	req.Body = io.NopCloser(checksum.NewDeferTrailerReader(body, trailers, req))

	// Request will contain header "Trailer: Checksum"
	// and an HTTP trailer "Checksum: sha1 00hq6RNueFa8QiEjhep5cJRHWAI="
	response, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()
}
Output:

func NewDeferTrailerReader

func NewDeferTrailerReader(body io.Reader, trailers map[string]io.Reader, request *http.Request) *DeferTrailerReader

NewDeferTrailerReader constructs a new DeferTrailerReader object. Receives a body data reader, trailers and their readers to be sent, and a request object

func (DeferTrailerReader) Read

func (h DeferTrailerReader) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes of request body into p. After the body reader has fully drawn out, it sequentially gets given trailers data from their readers and assigns it to the request. The function returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Returns io.EOF error if all result has read and no more data available.

type HashBase64ReadWriter

type HashBase64ReadWriter struct {
	hash.Hash
	// contains filtered or unexported fields
}

HashBase64ReadWriter an io.Reader that wraps a hash.Hash, it feeds the prefix + hash in base64 format

func NewHashBase64ReadWriter

func NewHashBase64ReadWriter(h hash.Hash, prefix string) *HashBase64ReadWriter

NewHashBase64ReadWriter constructs a new HashBase64ReadWriter. Receives a hash object to wrap and a prefix to prepend hash result string

Example
package main

import (
	"crypto"
	"fmt"
	"io"

	"github.com/bdragon300/tusgo/checksum"
)

func main() {
	data := []byte("Hello world!")
	rw := checksum.NewHashBase64ReadWriter(crypto.SHA1.New(), "sha1 ")
	if _, err := rw.Write(data); err != nil {
		panic(err)
	}

	sum, err := io.ReadAll(rw)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", sum)
}
Output:

sha1 00hq6RNueFa8QiEjhep5cJRHWAI=

func (*HashBase64ReadWriter) Read

func (h *HashBase64ReadWriter) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes of base64 hash sum into p. It invokes Sum calculation on the first call. The function returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Returns io.EOF error if all result has read and no more data available.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL