crypto

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2015 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PublicKeySize = ed25519.PublicKeySize
	SecretKeySize = ed25519.PrivateKeySize
	SignatureSize = ed25519.SignatureSize
)
View Source
const (
	HashSize = 32
)
View Source
const (
	SegmentSize = 64 // number of bytes that are hashed to form each base leaf of the Merkle tree
)

Variables

View Source
var (
	ErrNilInput         = errors.New("cannot use nil input")
	ErrInvalidSignature = errors.New("invalid signature")
)
View Source
var (
	ErrInsufficientLen = errors.New("supplied ciphertext is not long enough to contain a nonce")
)

Functions

func CalculateLeaves added in v0.3.3

func CalculateLeaves(fileSize uint64) (numSegments uint64)

Calculates the number of leaves in the file when building a Merkle tree.

func GenerateSignatureKeys

func GenerateSignatureKeys() (sk SecretKey, pk PublicKey, err error)

GenerateKeyPair creates a public-secret keypair that can be used to sign and verify messages.

func NewHash

func NewHash() hash.Hash

NewHash returns a blake2b 256bit hasher.

func NewTree

func NewTree() tree

NewTree returns a tree object that can be used to get the merkle root of a dataset.

func VerifyHash

func VerifyHash(data Hash, pk PublicKey, sig Signature) (err error)

VerifyHash uses a public key and input data to verify a signature.

func VerifySegment

func VerifySegment(base [SegmentSize]byte, hashSet []Hash, numSegments, proofIndex uint64, root Hash) bool

VerifySegment will verify that a segment, given the proof, is a part of a merkle root.

Types

type Ciphertext added in v0.3.3

type Ciphertext []byte

type Hash

type Hash [HashSize]byte

func BuildReaderProof

func BuildReaderProof(r io.Reader, proofIndex uint64) (base [SegmentSize]byte, hashSet []Hash, err error)

BuildReaderProof will build a storage proof when given a reader.

func HashAll

func HashAll(objs ...interface{}) Hash

HashAll takes a set of objects as input, encodes them all using the encoding package, and then hashes the result.

func HashBytes

func HashBytes(data []byte) Hash

HashBytes takes a byte slice and returns the result.

func HashObject

func HashObject(obj interface{}) Hash

HashObject takes an object as input, encodes it using the encoding package, and then hashes the result.

func MerkleRoot

func MerkleRoot(leaves [][]byte) (h Hash)

MerkleRoot calculates the "root hash" formed by repeatedly concatenating and hashing a binary tree of hashes. If the number of leaves is not a power of 2, the orphan hash(es) are not rehashed. Examples:

     ┌───┴──┐       ┌────┴───┐         ┌─────┴─────┐
  ┌──┴──┐   │    ┌──┴──┐     │      ┌──┴──┐     ┌──┴──┐
┌─┴─┐ ┌─┴─┐ │  ┌─┴─┐ ┌─┴─┐ ┌─┴─┐  ┌─┴─┐ ┌─┴─┐ ┌─┴─┐   │
   (5-leaf)         (6-leaf)             (7-leaf)

func ReaderMerkleRoot

func ReaderMerkleRoot(r io.Reader) (h Hash, err error)

ReaderMerkleRoot returns the merkle root of a reader.

type HashSlice

type HashSlice []Hash

HashSlice is used for sorting

func (HashSlice) Len

func (hs HashSlice) Len() int

These functions implement sort.Interface, allowing hashes to be sorted.

func (HashSlice) Less

func (hs HashSlice) Less(i, j int) bool

func (HashSlice) Swap

func (hs HashSlice) Swap(i, j int)

type PublicKey

type PublicKey [ed25519.PublicKeySize]byte

type SecretKey

type SecretKey [ed25519.PrivateKeySize]byte

type Signature

type Signature [ed25519.SignatureSize]byte

func SignHash

func SignHash(data Hash, sk SecretKey) (sig Signature, err error)

SignHash signs a message using a secret key.

type TwofishKey

type TwofishKey [32]byte

func GenerateTwofishKey

func GenerateTwofishKey() (key TwofishKey, err error)

GenerateEncryptionKey produces a key that can be used for encrypting and decrypting files.

func (TwofishKey) DecryptBytes

func (key TwofishKey) DecryptBytes(ct Ciphertext) ([]byte, error)

DecryptBytes decrypts the ciphertext created by EncryptBytes. The nonce is expected to be the first 12 bytes of the ciphertext.

func (TwofishKey) EncryptBytes

func (key TwofishKey) EncryptBytes(plaintext []byte) (Ciphertext, error)

EncryptBytes encrypts a []byte using the key. EncryptBytes uses GCM and prepends the nonce (12 bytes) to the ciphertext.

func (TwofishKey) NewCipher added in v0.3.3

func (key TwofishKey) NewCipher() cipher.Block

NewCipher creates a new Twofish cipher from the key.

func (TwofishKey) NewReader added in v0.3.3

func (key TwofishKey) NewReader(r io.Reader) io.Reader

NewReader returns a reader that encrypts or decrypts its input stream.

func (TwofishKey) NewWriter added in v0.3.3

func (key TwofishKey) NewWriter(w io.Writer) io.Writer

NewWriter returns a writer that encrypts or decrypts its input stream.

Jump to

Keyboard shortcuts

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