crypto

package
v0.0.0-...-53feb6c Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2016 License: GPL-2.0 Imports: 14 Imported by: 2

Documentation

Overview

Package crypto offers some functions that are often used in our code.

hash.go provides some utilities regarding the hashing of bytes, files or stream. The most common way to use it is to call: `HashStream(sha256.New(),stream)`. It will stream the input into the hash function by chunks and output the final hash.

schnorr.go provides some crypto-shortcuts: Schnorr signature and a Hash-function. See https://en.wikipedia.org/wiki/Schnorr_signature

It provides a way to sign a message using a private key and to verify the signature using the public counter part.

Index

Constants

View Source
const DefaultChunkSize = 1024

DefaultChunkSize is the size of the chunk used to read a stream

Variables

This section is empty.

Functions

func CheckLocalProofs

func CheckLocalProofs(newHash HashFunc, root HashID, leaves []HashID, proofs []Proof) bool

CheckLocalProofs does something unknonw

func Hash

func Hash(hash h.Hash, buff []byte) ([]byte, error)

Hash simply returns the Hash of the slice of bytes given

func HashArgs

func HashArgs(hash h.Hash, args ...encoding.BinaryMarshaler) ([]byte, error)

HashArgs takes all args and returns the hash. Every arg has to implement BinaryMarshaler or will be added using fmt.Sprint

func HashArgsSuite

func HashArgsSuite(suite abstract.Suite, args ...encoding.BinaryMarshaler) ([]byte, error)

HashArgsSuite makes a new hash from the suite and calls HashArgs

func HashBytes

func HashBytes(hash h.Hash, b []byte) ([]byte, error)

HashBytes returns the hash of the bytes

func HashFile

func HashFile(hash h.Hash, file string) ([]byte, error)

HashFile will hash the file using the streaming approach with DefaultChunkSize size of chunks

func HashFileChunk

func HashFileChunk(hash h.Hash, file string, chunkSize int) ([]byte, error)

HashFileChunk is similar to HashFile but using a chunkSize size of chunks for reading.

func HashFileSuite

func HashFileSuite(suite abstract.Suite, file string) ([]byte, error)

HashFileSuite returns the hash of a file using the hashing function of the suite given.

func HashStream

func HashStream(hash h.Hash, stream io.Reader) ([]byte, error)

HashStream returns the hash of the stream reading from it chunk by chunk of size DefaultChunkSize

func HashStreamChunk

func HashStreamChunk(hash h.Hash, stream io.Reader, chunkSize int) ([]byte, error)

HashStreamChunk will hash the stream using chunks of size chunkSize

func HashStreamSuite

func HashStreamSuite(suite abstract.Suite, stream io.Reader) ([]byte, error)

HashStreamSuite will hash the stream using the hashing function of the suite

func MerkleGet

func MerkleGet(suite abstract.Suite, root []byte, path MerklePath,
	ctx HashGet) ([]byte, error)

MerkleGet - Retrieves an object in a Merkle tree, validating the entire path in the process. Returns a slice of a buffer obtained from HashGet.Get(), which might be shared and should be considered read-only.

func PrintProofs

func PrintProofs(proofs []Proof)

PrintProofs prints out all proofs

func ProofTree

func ProofTree(newHash func() gohash.Hash, leaves []HashID) (HashID, []Proof)

ProofTree - Generates a Merkle proof tree for the given list of leaves, yielding one output proof per leaf.

func PubHex

func PubHex(suite abstract.Suite, point abstract.Point) (string, error)

PubHex converts a Public point to a hexadecimal representation

func ReadPub64

func ReadPub64(suite abstract.Suite, r io.Reader) (abstract.Point, error)

ReadPub64 a public point to a base64 representation

func ReadPubHex

func ReadPubHex(suite abstract.Suite, s string) (abstract.Point, error)

ReadPubHex reads a hexadecimal representation of a public point and convert it to the right struct

func ReadSecret64

func ReadSecret64(suite abstract.Suite, r io.Reader) (abstract.Secret, error)

ReadSecret64 takes a Base64-encoded secret and returns that secret, optionally an error

func ReadSecretHex

func ReadSecretHex(suite abstract.Suite, str string) (abstract.Secret, error)

ReadSecretHex reads a secret in hexadecimal from string

func SecretHex

func SecretHex(suite abstract.Suite, secret abstract.Secret) (string, error)

SecretHex encodes a secret to hexadecimal

func VerifySchnorr

func VerifySchnorr(suite abstract.Suite, public abstract.Point, msg []byte, sig SchnorrSig) error

VerifySchnorr verifies a given Schnorr signature. It returns nil iff the given signature is valid.

func WritePub64

func WritePub64(suite abstract.Suite, w io.Writer, point abstract.Point) error

WritePub64 writes a public point to a base64 representation

func WriteSecret64

func WriteSecret64(suite abstract.Suite, w io.Writer, secret abstract.Secret) error

WriteSecret64 converts a secret key to a Base64-string

Types

type ByHashID

type ByHashID []HashID

ByHashID is for sorting arrays of HashIds

func (ByHashID) Len

func (h ByHashID) Len() int

Len returns the length of the byhashid

func (ByHashID) Less

func (h ByHashID) Less(i, j int) bool

Less checks if the first is less than the second

func (ByHashID) Swap

func (h ByHashID) Swap(i, j int)

Swap takes two hashes and inverts them

type HashFunc

type HashFunc func() gohash.Hash

HashFunc exports a hashfunction

type HashGet

type HashGet interface {

	// Get lookups and returns the binary blob for a given content HashId.
	// Checks and returns an error if the hash doesn't match the content;
	// the caller doesn't need to check this correspondence.
	Get(id HashID) ([]byte, error)
}

HashGet is the context for looking up content blobs by self-certifying HashId. Implementations can be either local (same-node) or remote (cross-node).

type HashID

type HashID []byte

HashID is the Cryptographic hash content-IDs

func (HashID) Bit

func (id HashID) Bit(i uint) int

Bit returns if the given bit is set or not

func (*HashID) Level

func (id *HashID) Level() int

Level finds the skip-chain level of an ID

func (HashID) String

func (id HashID) String() string

String converts the HashID to a string, convenience for map[string] because map[HashID] is not possible.

type HashMap

type HashMap map[string][]byte

HashMap is a simple local-only, map-based implementation of HashGet interface

func (HashMap) Get

func (m HashMap) Get(id HashID) ([]byte, error)

Get returns an element from the hashmap

func (HashMap) Put

func (m HashMap) Put(id HashID, data []byte)

Put adds an element to the hashmap

type LevelProof

type LevelProof []HashID

LevelProof is used for the Big Merkle Tree (computed from server commits) A []LevelProof from root to server is sufficient proof

type MerklePath

type MerklePath struct {
	Ptr []int // Offsets of hash-pointers at each intermediate level
	Ofs int   // Offset of relevant object in last-level blob
	Len int   // Length of relevant object in last-level blob
}

MerklePath represents a downward path from a (root) node in a Merkle tree to a given (interior or leaf) descendant node, including all the data necessary to validate and extract the descendant. It is assumed the caller has a valid hash-pointer to the root/starting node, and that all nodes in the path can be retrieved via self-certifying hash-ID.

type Proof

type Proof []HashID

Proof is used for Local Merkle Trees (computed based on messages from clients) One Proof sufficient for one leaf in a Local Merkle Tree

func (Proof) Calc

func (p Proof) Calc(newHash HashFunc, leaf []byte) []byte

Calc - Given a Proof and the hash of the leaf, compute the hash of the root. If the Proof is of length 0, simply returns leaf.

func (Proof) Check

func (p Proof) Check(newHash HashFunc, root, leaf []byte) bool

Check a purported Proof against given root and leaf hashes.

func (*Proof) PrintProof

func (p *Proof) PrintProof(proofNumber int)

PrintProof prints the proof

type SchnorrSig

type SchnorrSig struct {
	Challenge abstract.Secret
	Response  abstract.Secret
}

SchnorrSig is a signature created using the Schnorr Signature scheme.

func SignSchnorr

func SignSchnorr(suite abstract.Suite, private abstract.Secret, msg []byte) (SchnorrSig, error)

SignSchnorr creates a Schnorr signature from a msg and a private key

func (*SchnorrSig) MarshalBinary

func (ss *SchnorrSig) MarshalBinary() ([]byte, error)

MarshalBinary is used for example in hashing

Jump to

Keyboard shortcuts

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