Documentation
¶
Index ¶
- Constants
- func AddCodecParser(codeTypeName string, parser CodecParser) error
- func WithCompressionMode(mode int) func(o *ZlibCodecOptions)
- func WithPasswordTag(tag string) func(*EncrypterCodecOptions)
- type BatchID
- type Codec
- func NewEncrypter(retriever PasswordRetriever, opts ...func(*EncrypterCodecOptions)) (Codec, error)
- func NewEncrypterWithParent(parent Codec, retriever PasswordRetriever, ...) (Codec, error)
- func NewNoOp() (Codec, error)
- func NewNoOpWithParent(parent Codec) (Codec, error)
- func NewReverser() (Codec, error)
- func NewReverserWithParent(parent Codec) (Codec, error)
- func NewZlib(opts ...func(*ZlibCodecOptions)) (Codec, error)
- func NewZlibWithParent(parent Codec, opts ...func(*ZlibCodecOptions)) (Codec, error)
- func ParseEncrypter(params []byte, parent Codec, opts map[string]any) (Codec, error)
- func ParseNoOp(_ []byte, parent Codec, _ map[string]any) (Codec, error)
- func ParseReverser(_ []byte, parent Codec, _ map[string]any) (Codec, error)
- func ParseZlib(params []byte, parent Codec, _ map[string]any) (Codec, error)
- type CodecParser
- type EncrypterCodecOptions
- type Meta
- type Package
- type PasswordRetriever
- type Pipeline
- type PipelineElement
- type SegmentID
- type ZlibCodecOptions
Examples ¶
Constants ¶
const ( NoCompression = zlib.NoCompression BestSpeed = zlib.BestSpeed BestCompression = zlib.BestCompression DefaultCompression = zlib.DefaultCompression HuffmanOnly = zlib.HuffmanOnly )
Variables ¶
This section is empty.
Functions ¶
func AddCodecParser ¶
func AddCodecParser(codeTypeName string, parser CodecParser) error
AddCodecParser is used to register additional Codecs
func WithCompressionMode ¶
func WithCompressionMode(mode int) func(o *ZlibCodecOptions)
WithCompressionMode sets the compression mode to be used
func WithPasswordTag ¶
func WithPasswordTag(tag string) func(*EncrypterCodecOptions)
WithPasswordTag allows specific tags to be specified for password retrieval
Types ¶
type BatchID ¶
type BatchID []byte
BatchID is an identifier used by the creator of a Package to disambiguate generation, allowing successful reconstruction
type Codec ¶
type Codec interface {
Pipeline() Pipeline
Encode(b []byte) ([]byte, error)
Decode(b []byte) ([]byte, error)
}
Codec provides the ability to step-wise transform a byte slice into a target output, and rollback that change to recover the original information
The sequence of steps is defined by the Pipeline, which will at minimum comprise this Codec instance, but it is expected this will also contain parent Codecs if these are specified within the Codec implementation.
Encode will process in sequence from the outermost parent Codec, returning after applying the transformation of this Codec, and Rollback works in the reverse order
func NewEncrypter ¶
func NewEncrypter(retriever PasswordRetriever, opts ...func(*EncrypterCodecOptions)) (Codec, error)
NewEncrypter returns a Codec that creates an encrypted copy of its input
Example ¶
data := []byte("Hello World")
retriever := func(tag string) ([]byte, error) {
fmt.Println(tag)
return []byte("abcdef0123456789abcdef0123456789"), nil
}
c, _ := NewEncrypter(retriever, WithPasswordTag("dummy"))
generated, err := c.Encode(data)
if err != nil {
fmt.Println(err)
}
rollback, err := c.Decode(generated)
if err != nil {
fmt.Println(err)
}
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: dummy dummy true [{"name":"encryptCodec","params":"{\"ptag\":\"dummy\"}"}]
Example (Nonce) ¶
data := []byte("Hello World")
retriever := func(_ string) ([]byte, error) {
return []byte("abcdef0123456789abcdef0123456789"), nil
}
c, _ := NewEncrypter(retriever, WithPasswordTag("dummy"))
gen1, _ := c.Encode(data)
gen2, _ := c.Encode(data)
fmt.Println(bytes.Equal(gen1, gen2))
roll1, _ := c.Decode(gen1)
roll2, _ := c.Decode(gen2)
fmt.Println(bytes.Equal(data, roll1))
fmt.Println(bytes.Equal(data, roll2))
Output: false true true
Example (Pipeline) ¶
data := []byte("Hello World")
retriever := func(tag string) ([]byte, error) {
return []byte("abcdef0123456789abcdef0123456789"), nil
}
reverser, _ := NewReverser()
c, _ := NewEncrypterWithParent(reverser, retriever, WithPasswordTag("dummy"))
generated, _ := c.Encode(data)
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: true [{"name":"reverserCodec","params":""},{"name":"encryptCodec","params":"{\"ptag\":\"dummy\"}"}]
func NewEncrypterWithParent ¶
func NewEncrypterWithParent(parent Codec, retriever PasswordRetriever, opts ...func(*EncrypterCodecOptions)) (Codec, error)
NewEncrypterWithParent returns a Codec that has at least one precursor codec, which will return encrypted data
func NewNoOp ¶
NewNoOp returns a Codec that creates a copy of its input
Example ¶
data := []byte("Hello World")
c, _ := NewNoOp()
generated, _ := c.Encode(data)
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: true [{"name":"noOpCodec","params":""}]
func NewNoOpWithParent ¶
NewNoOpWithParent returns a Codec that has at least one precursor codec
func NewReverser ¶
NewReverser returns a Codec that creates a copy with its contents reversed
Example ¶
data := []byte("Hello World")
c, _ := NewReverser()
generated, _ := c.Encode(data)
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
Output: true
func NewReverserWithParent ¶
NewNoOpWithParent returns a Codec that has at least one precursor codec. These will be executed in advance of this Code instance, resulting in an output that is their output reversed
Example ¶
data := []byte("Hello World")
parent, _ := NewReverser()
c, _ := NewReverserWithParent(parent)
generated, _ := c.Encode(data)
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: true [{"name":"reverserCodec","params":""},{"name":"reverserCodec","params":""}]
Example (Pipeline) ¶
data := []byte("Hello World")
reverser, _ := NewReverser()
noOp, _ := NewNoOpWithParent(reverser)
c, _ := NewReverserWithParent(noOp)
generated, _ := c.Encode(data)
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: true [{"name":"reverserCodec","params":""},{"name":"noOpCodec","params":""},{"name":"reverserCodec","params":""}]
func NewZlib ¶
func NewZlib(opts ...func(*ZlibCodecOptions)) (Codec, error)
NewZlib returns a Codec that creates a compressed copy of its input
Example ¶
data := []byte("Hello World")
c, _ := NewZlib()
generated, _ := c.Encode(data)
fmt.Println(hex.EncodeToString(generated))
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: 789cf248cdc9c95708cf2fca4901040000ffff180b041d true [{"name":"zlibCodec","params":"{\"mode\":-1}"}]
Example (Mode) ¶
data := []byte("Hello World")
c, _ := NewZlib(WithCompressionMode(BestSpeed))
generated, _ := c.Encode(data)
fmt.Println(hex.EncodeToString(generated))
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: 7801000b00f4ff48656c6c6f20576f726c64010000ffff180b041d true [{"name":"zlibCodec","params":"{\"mode\":1}"}]
func NewZlibWithParent ¶
func NewZlibWithParent(parent Codec, opts ...func(*ZlibCodecOptions)) (Codec, error)
NewZlibWithParent returns a Codec that has at least one precursor codec, which will return compressed data
Example (Mode) ¶
data := []byte("Hello World")
parent, _ := NewReverser()
c, _ := NewZlibWithParent(parent, WithCompressionMode(BestSpeed))
generated, _ := c.Encode(data)
fmt.Println(hex.EncodeToString(generated))
rollback, _ := c.Decode(generated)
fmt.Println(bytes.Equal(data, rollback))
fmt.Println(c.Pipeline())
Output: 7801000b00f4ff646c726f57206f6c6c6548010000ffff195b041d true [{"name":"reverserCodec","params":""},{"name":"zlibCodec","params":"{\"mode\":1}"}]
func ParseEncrypter ¶
ParseEncrypter creates a Encypter Codec with specified params and parent
func ParseReverser ¶
ParseReverser creates a Reverser Codec with specified parent (params are ignored)
type CodecParser ¶
CodecParser is the type of all Codec parsers, creating a new instance of Codec based on the specified parameters and parent (if specffied)
type EncrypterCodecOptions ¶
type EncrypterCodecOptions struct {
PasswordTag string `json:"ptag"`
}
EncryptCodecOptions allows compression to be configured
func (*EncrypterCodecOptions) String ¶
func (e *EncrypterCodecOptions) String() string
String returns a stringised representation of the options
type Meta ¶
type Meta struct {
// Version identifies the version of data as it is able to be deserialised, allowing correct unpacking
Version string
// BatchID allows identification of the overall processing
BatchID []byte
// SegmentID identifies what this specific piece of data is within the overall processing
SegmentID []byte
// Nonce ensures that this Package is uniquely identifiable across all processing
Nonce []byte
// CreationUTCTimestamp describes the UTC time when the Package was created
CreationUTCTimestamp time.Time
// Pipeline describes the Codec used to create the data being passed
Pipeline []byte
// ChainHash is SHA-512(priorChainHash || SHA-512(data)), linking this segment
// to its predecessor to detect tampering and incorrect reassembly order
ChainHash []byte
}
Meta describes what the data is, and how it is packaged
type Package ¶
type Package struct {
// Meta provides information on what the data represents and how it can be recovered
Meta *Meta
// Data is the information being transferred
Data []byte
// Signature stores the signature of the creator, which should be verified by the recipient against tampering
Signature []byte
}
Package captures all the necessary details that describe the underlying data and allow it to be unpackaged successfully
func CreatePackage ¶
func CreatePackage(b []byte, c Codec, batchID BatchID, segmentID SegmentID, privKey ed25519.PrivateKey, priorChainHash []byte) (*Package, error)
CreatePackage returns a Package that can be serialised to a byte slice for transmission.
Package instances contain metadata that describe what the data represents, and sufficient information to allow unpacking to the original data. Package instances are signed using the specific privKey to allow recipients to verify that it has not been modified during transmission.
The priorChainHash parameter enables chained hash verification across an orderd sequence of related Packages. It should be nil for the first Package, and the ChainHash from the preceding Package for the next Package in the sequence. The resulting ChainHash is stored in the Package's Meta and included in the Package signature. This provides a guarantee that the consumption of Packages is correctly ordered, and further restricts the ability to tamper with any one Package in the sequence.
func UnpackPackage ¶
UnpackPackage reconstructs a Package from its serialised byte slice representation and verifies its signature.
The input bytes are expected to be from a prior call to Package.Bytes(). The public key must correspond to the private key used when the Package was created. Returns an error if the bytes cannot be parsed, the version is unsupported, or signature verification fails.
type PasswordRetriever ¶
PasswordRetriever is a function that can return the appropriate password for the specified tag The password length must be 32 bytes (i.e. for AES-256)
type Pipeline ¶
type Pipeline []PipelineElement
Pipeline is an ordered sequence of PipelineElements
func ParsePipeline ¶
ParsePipeline attempts to deserialise the bytes to a Pipeline of PipelineElements
type PipelineElement ¶
PipelineElement captures the non-secret details of a given Codec in a processing Pipeline
type SegmentID ¶
type SegmentID []byte
SegmentID is an identifier for the specific data being packaged, allowing successful reconstruction regardless of transmission sequence
type ZlibCodecOptions ¶
type ZlibCodecOptions struct {
Compression int `json:"mode"`
}
ZlibCodecOptions allows compression to be configured
func (*ZlibCodecOptions) String ¶
func (z *ZlibCodecOptions) String() string
String returns a stringised representation of the options