shamir

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MPL-2.0 Imports: 5 Imported by: 6

README

Shamir's Secret Sharing

Build Status Test Coverage Documentation

Implementation of the Shamir's Secret Sharing in golang.

This package:

  • supports splitting and recombining of byte arrays;
  • supports splitting and recombining using io.Writer and io.Reader interfaces;
  • is compatible with gfsplit and gfcombine from libgfshare.

Based on github.com/hashicorp/vault from HashiCorp.

Contributing and license

This library is licences under Mozilla Public License, version 2.0. For information about how to contribute to this project, see CONTRIBUTING

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combine

func Combine(parts map[byte][]byte) ([]byte, error)

Combine is used to reverse a Split and reconstruct a secret once a `threshold` number of parts are available.

Example
parts := map[byte][]byte{
	71: {209, 38, 210, 117, 87, 218, 213, 140, 119, 77, 90},
	79: {27, 174, 140, 114, 4, 236, 44, 189, 215, 25, 201},
}
secret, err := Combine(parts)
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to recombine secret: %v\n", err)
}
fmt.Println(string(secret))
Output:

Hello world

func NewReader

func NewReader(readers map[byte]io.Reader) (io.Reader, error)
Example
readers := map[byte]io.Reader{
	71: bytes.NewBuffer([]byte{209, 38, 210, 117, 87, 218, 213, 140, 119, 77, 90}),
	79: bytes.NewBuffer([]byte{27, 174, 140, 114, 4, 236, 44, 189, 215, 25, 201}),
}

reader, err := NewReader(readers)
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to create secret reader: %v\n", err)
}
secret := make([]byte, 11)
if _, err := reader.Read(secret); err != nil {
	fmt.Fprintf(os.Stderr, "failed to read secret: %v\n", err)
}
fmt.Println(string(secret))
Output:

Hello world

func NewWriter

func NewWriter(parts, threshold int, factory func(x byte) (io.Writer, error)) (io.Writer, error)
Example
secret := []byte("Hello world")
writers := make(map[byte]*bytes.Buffer, 3)
writer, err := NewWriter(3, 2, func(x byte) (io.Writer, error) {
	writers[x] = &bytes.Buffer{}
	return writers[x], nil
})
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to create secret writer: %v\n", err)
}
if _, err := writer.Write(secret); err != nil {
	fmt.Fprintf(os.Stderr, "failed to write secret: %v\n", err)
}
fmt.Println(len(writers))
for _, w := range writers {
	fmt.Println(w.Len())
}
// Output
// 3
// 11
// 11
// 11
Output:

func Split

func Split(secret []byte, parts, threshold int) (map[byte][]byte, error)

Split takes an arbitrarily long secret and generates a `parts` number of shares, `threshold` of which are required to reconstruct the secret. The parts and threshold must be at least 2, and less than 256. The returned shares are each one byte longer than the secret as they attach a tag used to reconstruct the secret.

Example
secret := []byte("Hello world")
parts, err := Split(secret, 3, 2)
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to split secret: %v\n", err)
}
fmt.Println(len(parts))
for _, part := range parts {
	fmt.Println(len(part))
}
Output:

3
11
11
11

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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