stream

package
v3.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxBlobSize       = 2097152 // 2mb, or 2 * 2^20
	BlobHashSize      = sha512.Size384
	BlobHashHexLength = BlobHashSize * 2 // in hex, each byte is 2 chars
)

Variables

View Source
var ErrBlobEmpty = errors.New("blob is empty")
View Source
var ErrBlobTooBig = errors.Newf("blob must be at most %d bytes", MaxBlobSize)

Functions

func DecryptBlob

func DecryptBlob(b Blob, key, iv []byte) ([]byte, error)

DecryptBlob decrypts a blob

func NullIV

func NullIV() []byte

NullIV returns an IV of 0s

Types

type Blob

type Blob []byte

func NewBlob

func NewBlob(data, key, iv []byte) (Blob, error)

func (Blob) Hash

func (b Blob) Hash() []byte

Hash returns a hash of the blob data

func (Blob) HashHex

func (b Blob) HashHex() string

HashHex returns the blob hash as a hex string

func (Blob) Plaintext

func (b Blob) Plaintext(key, iv []byte) ([]byte, error)

func (Blob) Size

func (b Blob) Size() int

func (Blob) ValidForSend

func (b Blob) ValidForSend() error

ValidForSend returns true if the blob size is within the limits

type BlobInfo

type BlobInfo struct {
	Length   int    `json:"length"`
	BlobNum  int    `json:"blob_num"`
	BlobHash []byte `json:"-"`
	IV       []byte `json:"-"`
}

BlobInfo is the stream descriptor info for a single blob in a stream Encoding to and from JSON is customized to match existing behavior (see json.go in package)

func (BlobInfo) Hash

func (bi BlobInfo) Hash() []byte

Hash returns the hash of the blob info for calculating the stream hash

func (BlobInfo) MarshalJSON

func (bi BlobInfo) MarshalJSON() ([]byte, error)

func (*BlobInfo) UnmarshalJSON

func (bi *BlobInfo) UnmarshalJSON(b []byte) error

type BlobInfoAlias

type BlobInfoAlias BlobInfo

type Encoder

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

Encoder reads bytes from a source and returns blobs of the stream

func NewEncoder

func NewEncoder(src io.Reader) *Encoder

NewEncoder creates a new stream encoder

func NewEncoderFromSD

func NewEncoderFromSD(src io.Reader, sdBlob *SDBlob) *Encoder

NewEncoderFromSD creates a new encoder that reuses cryptographic material from an sd blob This can be used to reconstruct a stream exactly from a file NOTE: this will assume that all blobs except the last one are at max length. in theory this is not required, but in practice this is always true. if this is false, streams may not match exactly

func NewEncoderWithIVs

func NewEncoderWithIVs(src io.Reader, key []byte, ivs [][]byte) *Encoder

NewEncoderWithIVs creates a new encoder that uses preset cryptographic material

func (*Encoder) Next

func (e *Encoder) Next() (Blob, error)

Next reads the next chunk of data, encodes it into a blob, and adds it to the stream When the source is fully consumed, Next() makes sure the stream is terminated (i.e. the sd blob ends with an empty terminating blob) and returns io.EOF

func (*Encoder) SDBlob

func (e *Encoder) SDBlob() *SDBlob

SDBlob returns the sd blob so far

func (*Encoder) SourceHash

func (e *Encoder) SourceHash() []byte

SourceHash returns a hash of the bytes read from source

func (*Encoder) SourceLen

func (e *Encoder) SourceLen() int

SourceLen returns the number of bytes read from source

func (*Encoder) SourceSizeHint

func (e *Encoder) SourceSizeHint(size int) *Encoder

SourceSizeHint sets a hint about the total size of the source This helps allocate RAM more efficiently. If the hint is wrong, it still works fine but there will be a small performance penalty.

func (*Encoder) Stream

func (e *Encoder) Stream() (Stream, error)

Stream creates the whole stream in one call

type JSONBlobInfo

type JSONBlobInfo struct {
	BlobInfoAlias
	BlobHash string `json:"blob_hash,omitempty"`
	IV       string `json:"iv"`
}

type JSONSDBlob

type JSONSDBlob struct {
	StreamName string `json:"stream_name"`
	SDBlobAlias
	Key               string `json:"key"`
	SuggestedFileName string `json:"suggested_file_name"`
	StreamHash        string `json:"stream_hash"`
}

type SDBlob

type SDBlob struct {
	StreamName        string     `json:"-"`
	BlobInfos         []BlobInfo `json:"blobs"`
	StreamType        string     `json:"stream_type"`
	Key               []byte     `json:"-"`
	SuggestedFileName string     `json:"-"`
	StreamHash        []byte     `json:"-"`
}

SDBlob contains information about the rest of the blobs in the stream Encoding to and from JSON is customized to match existing behavior (see json.go in package)

func (*SDBlob) FromBlob

func (s *SDBlob) FromBlob(b Blob) error

FromBlob unmarshals a data Blob that should contain SDBlob data

func (SDBlob) Hash

func (s SDBlob) Hash() []byte

Hash returns a hash of the SD blob data

func (SDBlob) HashHex

func (s SDBlob) HashHex() string

HashHex returns the SD blob hash as a hex string

func (SDBlob) IsValid

func (s SDBlob) IsValid() bool

IsValid returns true if the set StreamHash matches the current hash of the stream data

func (SDBlob) MarshalJSON

func (s SDBlob) MarshalJSON() ([]byte, error)

func (SDBlob) ToBlob

func (s SDBlob) ToBlob() Blob

ToBlob converts the SDBlob to a normal data Blob

func (SDBlob) ToJson

func (s SDBlob) ToJson() string

ToJson returns the SD blob hash as JSON

func (*SDBlob) UnmarshalJSON

func (s *SDBlob) UnmarshalJSON(b []byte) error

type SDBlobAlias

type SDBlobAlias SDBlob

type Stream

type Stream []Blob

func New

func New(src io.Reader) (Stream, error)

New creates a new Stream from a stream of bytes.

func (Stream) Data deprecated

func (s Stream) Data() ([]byte, error)

Data returns the file data that a stream encapsulates.

Deprecated: use Decode() instead. It's a more accurate name. Data() will be removed in the future.

func (Stream) Decode

func (s Stream) Decode() ([]byte, error)

Decode returns the file data that a stream encapsulates

TODO: this should use io.Writer instead of returning bytes

Jump to

Keyboard shortcuts

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