Skip to main content

Shamir's Secret Sharing - A port of secrets.js-grempe to Python, allowing cross-platform compatible shares between JavaScript and Python.

Project description

CI codecov PyPI version Built with Material for MkDocs JS2PySecrets Documentation

About

js2pysecrets is a port of the secrets.js-grempe JavaScript package to Python.

This package allows for cross-platform compatible shares, generated using Shamir's Secret Sharing, to seamlessly interoperate between JavaScript and Python.

Function names and arguments used in the JavaScript package have been maintained for consistency and maintainability.

The functionality is essentially the same as the JavaScript package, with an exception around random number generation. Python doesn't have to adapt to different environments for random number generation like the JavaScript does.

For additional details, see the documentation.

Installation and Usage

Install the PyPI package:

pip install js2pysecrets

Import the library:

import js2pysecrets as secrets

Examples

Divide a 512-bit key, expressed in hexadecimal form, into 10 shares, requiring that any 5 of them are necessary to reconstruct the original key:

import js2pysecrets as secrets

# generate a 512-bit key
key = secrets.random(512) 
print(key) # => key is a hex string

# split into 10 shares with a threshold of 5
shares = secrets.share(key, 10, 5)
print(shares) # => ['801xxx...xxx','802xxx...xxx', ... ,'809xxx...xxx','80axxx...xxx']

# combine 4 shares
comb = secrets.combine(shares[:4])
print(comb == key) # => False

# combine 5 shares
comb = secrets.combine(shares[:5])
print(comb == key) # => True

# combine ALL shares
comb = secrets.combine(shares)
print(comb == key) # => True

# create another share with id 8
new_share = secrets.newShare(8, shares)
print(new_share) # => '808xxx...xxx'

# reconstruct using 4 original shares and the new share:
comb = secrets.combine(shares[:4] + [new_share])
print(comb == key) # => True

Divide a password containing a mix of numbers, letters, and other characters, requiring that any 3 shares must be present to reconstruct the original password:

import js2pysecrets as secrets

pw = "<<PassWord123>>"

# convert the text into a hex string
pwHex = secrets.str2hex(pw)
print(pwHex) # => hex string

# split into 5 shares, with a threshold of 3
shares = secrets.share(pwHex, 5, 3)
print(shares) # => ['801xxx...xxx','802xxx...xxx', ... ,'804xxx...xxx','805xxx...xxx']

# combine 2 shares:
comb = secrets.combine(shares[:2])

# convert back to UTF string:
comb = secrets.hex2str(comb)
print(comb == pw) # => False

# combine 3 shares:
comb = secrets.combine([shares[1], shares[3], shares[4]])

# convert back to UTF string:
comb = secrets.hex2str(comb)
print(comb == pw) # => True

License

js2pysecrets is released under the MIT License. See the LICENSE file.

Development and Testing

Read the CONTRIBUTING.md file.

To Do

  • Restructure and clean-up the tests
  • Documentation

Changelog

  • 0.1.x

    • Cleaned up the code an some tests
  • 0.0.x

    • Documentation, documentation, documentation...
    • Configured automatic release to PyPI
    • Converted secrets.js[^1] to Python
    • Disabled the tests_win GitHub action, #24
    • Moved docs to use Material for MkDocs
    • Converted secrets.js-grempe Jasmine tests to pytest versions
    • Added package.json as a stub
    • Built Node.js wrapper for testing
    • Enable CodeCov
    • Started with the Python Project Template

[^1]: secrets.js-grempe and secrets.js are basically the same. The difference is the execution environment, JavaScript or Node.js.

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

js2pysecrets-0.1.1.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

js2pysecrets-0.1.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: js2pysecrets-0.1.1.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for js2pysecrets-0.1.1.tar.gz
Algorithm Hash digest
SHA256 118612020f6eaafd329e9d2600732b30c99e4779f470f87517716eeb032a8415
MD5 029dc0913c783dc9fbb6750762971a77
BLAKE2b-256 e033921e5d593cb6d8f77158fdf533890355c68002ed24dd129cf357b6431eac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: js2pysecrets-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for js2pysecrets-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0a99a1a42497f10a5667b1f4409b81a24e5e627aa6db83cdacede36f48bed450
MD5 28c0845fdaa887e5a31e54537c9b10f0
BLAKE2b-256 a61209572a1cb066d1395012d04e887226aa200af385ae710c04a9ca10fd04c4

See more details on using hashes here.

Supported by

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