Skip to main content

Simple implementation of Shamir's Secret Sharing scheme

Project description

🔐 Shamir Secret Sharing — Python Implementation

PyPI Python License: MIT

A clean and minimal Python implementation of Shamir’s Secret Sharing Scheme (SSS)
a cryptographic method to divide a secret into multiple parts (shares),
so that only a threshold number of them are required to reconstruct the original secret.


🚀 Features

  • Pure Python, minimal dependencies (pycryptodome)
  • Generate any number of secure shares
  • Reconstruct the secret using only the threshold shares
  • Human-readable export/import formats (JSON + Base64)
  • Easy-to-use API for both sharing and recovery

📦 Installation

Install the package from PyPI:

pip install shamir-lbodlev

Importing the Package

from Shamir import Shamir

Quick Start Example

1. Split a Secret into Shares

from Shamir import Shamir

# Create shares for a secret message
shamir = Shamir(secret=b"My top secret message", n=5, k=3)
# total of 5 shares with 3 threshold(minimal amount of shares to reconstruct the message)

# Export parameters and shares
shamir.export_public("public.json")
shamir.export_shares("share{}.dat")  # Creates share1.dat, share2.dat, ...

This produces:

  • public.json → contains public data (prime number p, total shares n, threshold k)
  • share1.dat, share2.dat, ... → Base64-encoded shares

2. Reconstruct the Secret

from Shamir import Shamir

# Initialize recovery instance
recoverer = Shamir()
recoverer.load_public("public.json")

# Load any 3 of the generated shares
recoverer.load_shares("share{}.dat", indexes=[1, 3, 5])

# Recover the secret
secret = recoverer.recover()
print(secret.decode())

API Reference

class Shamir(secret: bytes | None = None, n: int | None = None, k: int | None = None) Initializes a new instance of the Shamir scheme.

Parameter Type Description
secret bytes, optional The secret to share (as bytes).
n int, optional Total number of shares to generate.
k int, optional Minimum number of shares needed to reconstruct the secret.

Raises ValueError if k > n.


recover() -> bytes

Reconstructs and returns the shared secret (in bytes). Must be called after loading public parameters and shares.


export_public(filename: str) -> None

Exports public data (p, n, k) in JSON format. Required for secret reconstruction.


shamir.export_public("public.json")

export_shares(template: str) -> None

Exports all generated shares using a filename template. The share index is automatically inserted into the template.

shamir.export_shares("share{}.dat")
# Produces: share1.dat, share2.dat, share3.dat, ...

load_public(filename: str) -> None Loads the public parameters from a previously exported JSON file.

shamir.load_public("public.json")

load_shares(template: str, indexes: list[int]) -> None

Loads share files according to a template and list of indexes. If more than k shares are given, only the first k are used.

shamir.load_shares("share{}.dat", indexes=[1, 2, 5])

Internal Methods

The following methods are used internally and should not be called directly:

  • __generate_coefs()
  • __generate_shares()
  • __generate_random_point()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shamir_lbodlev-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shamir_lbodlev-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file shamir_lbodlev-0.1.1.tar.gz.

File metadata

  • Download URL: shamir_lbodlev-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for shamir_lbodlev-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d202bdb85bd609ec50c001b88a97fcfe494bfee831f8082470f4d3fc6ac872e7
MD5 29468bfcce6fc6369a0c7f212fdbdd55
BLAKE2b-256 20176ee4937560827a04503af392b7206351a59a160ad4adff6b250c2331b00e

See more details on using hashes here.

File details

Details for the file shamir_lbodlev-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: shamir_lbodlev-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for shamir_lbodlev-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f14def7a343eb6a65196ae016169301866309082e98af0b86a1b96a1e4f35b4
MD5 9f61e599351fe0fbba6225cef586be24
BLAKE2b-256 4669ab624e10cd1277aa8b3c13d1d48e21108d6610ab0aaa5897ae111543beb9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page