cert

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: Apache-2.0 Imports: 6 Imported by: 1

README

Certificates

A certificate consists of

  • a tree.
  • a signature on the tree root hash valid under some public key.
  • an optional delegation that links that public key to root public key.

Read More

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lookup

func Lookup(path [][]byte, node Node) []byte
Example
package main

import (
	"fmt"

	cert "github.com/aviate-labs/certificate-go"
)

func main() {
	fmt.Println(string(cert.Lookup(path("a", "x"), tree)))
	fmt.Println(string(cert.Lookup(path("a", "y"), tree)))
	fmt.Println(string(cert.Lookup(path("b"), tree)))
	fmt.Println(string(cert.Lookup(path("d"), tree)))
}

func path(p ...string) [][]byte {
	var path [][]byte
	for _, p := range p {
		path = append(path, []byte(p))
	}
	return path
}
Output:

hello
world
good
morning

func Serialize

func Serialize(node Node) ([]byte, error)
Example
package main

import (
	"fmt"

	cert "github.com/aviate-labs/certificate-go"
)

var tree = cert.Fork{
	LeftTree: cert.Fork{
		LeftTree: cert.Labeled{
			Label: []byte("a"),
			Tree: cert.Fork{
				LeftTree: cert.Fork{
					LeftTree: cert.Labeled{
						Label: []byte("x"),
						Tree:  cert.Leaf("hello"),
					},
					RightTree: cert.Empty{},
				},
				RightTree: cert.Labeled{
					Label: []byte("y"),
					Tree:  cert.Leaf("world"),
				},
			},
		},
		RightTree: cert.Labeled{
			Label: []byte("b"),
			Tree:  cert.Leaf("good"),
		},
	},
	RightTree: cert.Fork{
		LeftTree: cert.Labeled{
			Label: []byte("c"),
			Tree:  cert.Empty{},
		},
		RightTree: cert.Labeled{
			Label: []byte("d"),
			Tree:  cert.Leaf("morning"),
		},
	},
}

func main() {
	b, _ := cert.Serialize(tree)
	fmt.Printf("%x", b)
}
Output:

8301830183024161830183018302417882034568656c6c6f810083024179820345776f726c6483024162820344676f6f648301830241638100830241648203476d6f726e696e67

Types

type Certificate

type Certificate struct {
	Tree       HashTree
	Signature  []byte
	Delegation *Delegation
}

type Delegation

type Delegation struct {
	SubnetId principal.Principal
	// The nested certificate typically does not itself again contain a
	// delegation, although there is no reason why agents should enforce that
	// property.
	Certificate Certificate
}

type Empty

type Empty struct{}

func (Empty) Reconstruct

func (e Empty) Reconstruct() [32]byte

func (Empty) String added in v0.0.2

func (e Empty) String() string

type Fork

type Fork struct {
	LeftTree  Node
	RightTree Node
}

func (Fork) Reconstruct

func (f Fork) Reconstruct() [32]byte

func (Fork) String added in v0.0.2

func (f Fork) String() string

type HashTree

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

func NewHashTree

func NewHashTree(root Node) HashTree

func (HashTree) Digest

func (t HashTree) Digest() [32]byte

type Label

type Label []byte

func (Label) String

func (l Label) String() string

type LabelResult

type LabelResult string
const (
	Absent   LabelResult = "absent"
	Continue LabelResult = "continue"
	Found    LabelResult = "found"
	Unknown  LabelResult = "unknown"
)

type Labeled

type Labeled struct {
	Label Label
	Tree  Node
}

func (Labeled) Reconstruct

func (l Labeled) Reconstruct() [32]byte

func (Labeled) String added in v0.0.2

func (l Labeled) String() string

type Leaf

type Leaf []byte

func (Leaf) Reconstruct

func (l Leaf) Reconstruct() [32]byte

func (Leaf) String added in v0.0.2

func (l Leaf) String() string

type Node

type Node interface {
	Reconstruct() [32]byte
	fmt.Stringer
}

func Deserialize

func Deserialize(data []byte) (Node, error)
Example
package main

import (
	"encoding/hex"
	"fmt"

	cert "github.com/aviate-labs/certificate-go"
)

func main() {
	data, _ := hex.DecodeString("8301830183024161830183018302417882034568656c6c6f810083024179820345776f726c6483024162820344676f6f648301830241638100830241648203476d6f726e696e67")
	fmt.Println(cert.Deserialize(data))
}
Output:

{{a:{{x:hello|∅}|y:world}|b:good}|{c:∅|d:morning}} <nil>

func DeserializeNode added in v0.0.2

func DeserializeNode(s []interface{}) (Node, error)

type Pruned

type Pruned [32]byte
Example
package main

import (
	"encoding/hex"
	"fmt"

	cert "github.com/aviate-labs/certificate-go"
)

var pruned = cert.Fork{
	LeftTree: cert.Fork{
		LeftTree: cert.Labeled{
			Label: []byte("a"),
			Tree: cert.Fork{
				LeftTree: cert.Pruned(h2b("1B4FEFF9BEF8131788B0C9DC6DBAD6E81E524249C879E9F10F71CE3749F5A638")),
				RightTree: cert.Labeled{
					Label: []byte("y"),
					Tree:  cert.Leaf("world"),
				},
			},
		},
		RightTree: cert.Labeled{
			Label: []byte("b"),
			Tree:  cert.Pruned(h2b("7B32AC0C6BA8CE35AC82C255FC7906F7FC130DAB2A090F80FE12F9C2CAE83BA6")),
		},
	},
	RightTree: cert.Fork{
		LeftTree: cert.Pruned(h2b("EC8324B8A1F1AC16BD2E806EDBA78006479C9877FED4EB464A25485465AF601D")),
		RightTree: cert.Labeled{
			Label: []byte("d"),
			Tree:  cert.Leaf("morning"),
		},
	},
}

func main() {
	fmt.Printf("%X", pruned.Reconstruct())
}

func h2b(s string) [32]byte {
	var bs [32]byte
	b, _ := hex.DecodeString(s)
	copy(bs[:], b)
	return bs
}
Output:

EB5C5B2195E62D996B84C9BCC8259D19A83786A2F59E0878CEC84C811F669AA0

func (Pruned) Reconstruct

func (p Pruned) Reconstruct() [32]byte

func (Pruned) String added in v0.0.2

func (p Pruned) String() string

Jump to

Keyboard shortcuts

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