multikeypair

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2021 License: MIT Imports: 5 Imported by: 0

README

go-multikeypair

Go Reference Go Go Report Card build matrix Slack

A multiformats-inspired module for encoding cryptographic keypairs.

Install

At a shell within your go module:

go get github.com/proofzero/go-multikeypair

Build Instructions

go build

Testing

go test

Usage

Pseudo-golang for excercising the Encode and Decode API for a given hardcoded keypair of a given crypto algorithm:

// Keys:
private := []byte("Wn3Sf5Ke/3:PA:Tm{KCf59Wg6j%/g*#d")
public := []byte("cv-sB6?r*RW8vP5TuMSv_wvw#dV4nUP!@y%u@pmK!P-S2gYVLve!PfdC#kew5Q7U")

// Cypher:
code := multikeypair.ED_25519
name := multikeypair.Codes[ED_25519]

// Encode:
mk, err := multikeypair.Encode(private, public, code)
if err != nil {
    panic(err)
}

// Decode:
kp, err := multikeypair.Decode(mk)
if err != nil {
    panic(err)
}

Documentation is inline with code as comments. See tests in keypair_test.go.

Contribute

We would appreciate your help to make this a useful utility. For code contributions, please send a pull request. First outlining your proposed change in an issue or discussion thread to get feedback from other developers is a good idea for anything but small changes. Other ways to contribute include:

  • making a feature request
  • reporting a bug
  • writing a test
  • adding some documentation
  • providing feedback on the project direction
  • reporting your experience using it

For most things we will use GitHub issues and discussions, but feel free to join the project Matrix room to chat or ask questions.

Documentation

Index

Constants

View Source
const (
	MIN_KEY_LENGTH = 2
	MAX_KEY_LENGTH = 256
)

Minimum and maximum key lengths in bytes.

View Source
const (
	IDENTITY = uint64(0x00)
	ED_25519 = uint64(0x11)
	BIP_32   = uint64(0x22)
	DSA      = uint64(0x33)
	RSA      = uint64(0x44)
)

Support ciphers. Accepting PRs for more!

Variables

View Source
var (
	ErrUnknownCode         = errors.New("unknown multikeypair code")
	ErrTooShort            = errors.New("multikeypair too short. must be >= 2 bytes")
	ErrTooLong             = errors.New("multikeypair too long. must be < 129 bytes")
	ErrInvalidMultikeypair = errors.New("input isn't valid multikeypair")
	ErrVarintBufferShort   = errors.New("uvarint: buffer too small")
	ErrVarintTooLong       = errors.New("uvarint: varint too big (max 64bit)")
)

Keypair-specific errors this module exports.

View Source
var Codes = map[uint64]string{
	IDENTITY: "identity",
	ED_25519: "ed25519",
	BIP_32:   "bip32",
	DSA:      "dsa",
	RSA:      "rsa",
}

Codes is a mapping from cipher code to name.

View Source
var Names = map[string]uint64{
	"identity": IDENTITY,
	"ed25519":  ED_25519,
	"bip32":    BIP_32,
	"dsa":      DSA,
	"res":      RSA,
}

Names is a mapping from cipher name to code.

Functions

func PackCode

func PackCode(code uint64) []byte

PackCode packs a cipher code as varint.

func UnpackCode

func UnpackCode(buf []byte) (uint64, error)

UnpackCode unpacks a varint cipher code.

Types

type Keypair

type Keypair struct {
	// Cipher identification code.
	Code uint64
	// Human-readable cipher name.
	Name string
	// Raw public key bytes.
	Public []byte
	// Length in bytes of public key.
	PublicLength int
	// Raw private key bytes.
	Private []byte
	// Length in bytes of private key.
	PrivateLength int
}

Keypair is a public/private keypair unpacked into a struct for easy access.

func Decode

func Decode(m Multikeypair) (Keypair, error)

Decode unpacks a multikeypair into a Keypair struct.

func KeypairFromB58

func KeypairFromB58(s string) (Keypair, error)

KeypairFromB58 parses a base58-encoded hex string into a Keypair.

func (Keypair) Encode

func (k Keypair) Encode() (Multikeypair, error)

Encode a Keypair struct into a Multikeypair.

type Multikeypair

type Multikeypair []byte

Multikeypair is a byte slice with the following form: [length] (24-bit length prefix)

[code length]<code> (16-bit length prefix, uvarint code)
[private key length]<private key> (16-bit length prefix)
[public key length]<public key> (16-bit length prefix)

func Encode

func Encode(private []byte, public []byte, code uint64) (Multikeypair, error)

Encode a keypair into a Multikeypair, specifying the keypair type using an integer code.

func EncodeName

func EncodeName(private []byte, public []byte, name string) (Multikeypair, error)

EncodeName encodes a keypair into a Multikeypair, specifying the keypair type using a string name instead of an integer code.

func MultikeypairFromB58

func MultikeypairFromB58(s string) (Multikeypair, error)

MultikeypairFromB58 parses a base58-encoded hex string into a Multikeypair.

func (Multikeypair) B58String

func (m Multikeypair) B58String() string

B58String generates a base58-encoded version of a Multikeypair.

func (Multikeypair) Decode

func (m Multikeypair) Decode() (Keypair, error)

Decode unpacks a multikeypair into a Keypair struct.

Jump to

Keyboard shortcuts

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