shamir

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 3 Imported by: 0

README

shamir

This library implements the Shamir's Secret Sharing algorithm in Golang

Usage

package main

import (
    "bytes"
    "crypto/rand"

    "github.com/mxmauro/shamir"
)

func main() {
    // Create a random secret
    secret := make([]byte, 16)
    _, _ = rand.Read(secret)

    // Split it in five parts. Three parts are required in order to recreate it.
    secretParts, err := shamir.Split(secret, 5, 3)
    if err != nil {
        // unable to split secret
    }

    // In this example we will take three parts at random and unsorted
    selectedParts := make([][]byte, 3)
    selectedParts[0] = secretParts[1]
    selectedParts[1] = secretParts[4]
    selectedParts[2] = secretParts[2]

    // Rebuild secret
    var recoveredSecret []byte
    recoveredSecret, err = shamir.Combine(selectedParts)
	if err != nil {
		// unable to recombine secret
	}

	if !bytes.Equal(secret, recoveredSecret) {
		// secrets and recovered secret mismatch
	}
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combine

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

Combine recreates a secret if enough parts are provided

func Split

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

Split divides a secret into several parts to be shared

Types

This section is empty.

Jump to

Keyboard shortcuts

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