shamir

package
v0.0.0-...-269064b Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: BSD-3-Clause Imports: 4 Imported by: 2

Documentation

Overview

Package shamir provides a simple implementation for the Shamir's Secret Sharing algorithm.

Shamir's Secret Sharing is a cryptographic algorithm created by Adi Shamir. It is a form of secret sharing, where a secret is divided into several unique parts (shares). To reconstruct the original secret, a minimum number (threshold) of parts is required. In the threshold scheme this number is less than the total number of parts. Otherwise all participants are needed to reconstruct the original secret.

Use 'Split' to obtain the shares of a given secret.

secret := []byte("super-secure-secret")
shares, err := Split(secret, 5, 3)

Use 'Combine' to restore the original secret from a list of shares.

secret, err := Combine(shares)

More information: https://cs.jhu.edu/~sdoshi/crypto/papers/shamirturing.pdf

Based on the original implementation by Hashicorp: https://www.hashicorp.com/

Index

Examples

Constants

View Source
const (
	// ShareOverhead is the byte size overhead of each share when using
	// Split on a secret. This is caused by appending a one byte tag to
	// the share.
	ShareOverhead = 1
)

Variables

This section is empty.

Functions

func Combine

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

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

Example
parts := [][]byte{[]byte("part-1"), []byte("part-2"), []byte("part-3")}
restored, err := Combine(parts)
if err != nil {
	panic(err)
}
fmt.Printf("restored secret: %x", restored)
Output:

func Split

func Split(secret []byte, parts, threshold int) ([][]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("super-secure-secret")
parts, err := Split(secret, 5, 3)
if err != nil {
	panic(err)
}
fmt.Printf("secret splitted on %d parts", len(parts))
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳