|
1 | | -# js2pysecrets |
2 | | - |
3 | | -[](https://codecov.io/gh/poing/JS2PySecrets) |
4 | 1 | [](https://github.com/poing/JS2PySecrets/actions/workflows/main.yml) |
| 2 | +[](https://codecov.io/gh/poing/JS2PySecrets) |
| 3 | +[](https://badge.fury.io/py/js2pysecrets) |
5 | 4 | [](https://squidfunk.github.io/mkdocs-material/) |
6 | | - |
7 | 5 | [](https://poing.github.io/JS2PySecrets/) |
8 | 6 |
|
| 7 | +# About |
| 8 | + |
| 9 | +`js2pysecrets` is a port of the [`secrets.js-grempe`](https://github.com/grempe/secrets.js) JavaScript package to Python. |
| 10 | + |
| 11 | +This package allows for cross-platform compatible shares, *generated using [Shamir's Secret Sharing](http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing)*, to seamlessly interoperate between JavaScript and Python. |
| 12 | + |
| 13 | +Function names and arguments used in the JavaScript package have been maintained for consistency and maintainability. |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +*For additional details, see the [documentation](https://poing.github.io/JS2PySecrets/).* |
| 18 | + |
| 19 | + |
| 20 | +## Installation and Usage |
| 21 | + |
| 22 | +Install the PyPI package: |
| 23 | + |
| 24 | +``` |
| 25 | +pip install js2pysecrets |
| 26 | +``` |
| 27 | + |
| 28 | +Import the library: |
| 29 | + |
| 30 | +``` |
| 31 | +import js2pysecrets as secrets |
| 32 | +``` |
| 33 | + |
| 34 | +### Examples |
| 35 | + |
| 36 | +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: |
| 37 | + |
| 38 | +```python |
| 39 | +import js2pysecrets as secrets |
| 40 | + |
| 41 | +# generate a 512-bit key |
| 42 | +key = secrets.random(512) |
| 43 | +print(key) # => key is a hex string |
| 44 | + |
| 45 | +# split into 10 shares with a threshold of 5 |
| 46 | +shares = secrets.share(key, 10, 5) |
| 47 | +print(shares) # => ['801xxx...xxx','802xxx...xxx', ... ,'809xxx...xxx','810xxx...xxx'] |
| 48 | + |
| 49 | +# combine 4 shares |
| 50 | +comb = secrets.combine(shares[:4]) |
| 51 | +print(comb == key) # => False |
| 52 | + |
| 53 | +# combine 5 shares |
| 54 | +comb = secrets.combine(shares[:5]) |
| 55 | +print(comb == key) # => True |
| 56 | + |
| 57 | +# combine ALL shares |
| 58 | +comb = secrets.combine(shares) |
| 59 | +print(comb == key) # => True |
| 60 | + |
| 61 | +# create another share with id 8 |
| 62 | +new_share = secrets.newShare(8, shares) |
| 63 | +print(new_share) # => '808xxx...xxx' |
| 64 | + |
| 65 | +# reconstruct using 4 original shares and the new share: |
| 66 | +comb = secrets.combine(shares[:4] + [new_share]) |
| 67 | +print(comb == key) # => True |
| 68 | +``` |
9 | 69 |
|
| 70 | +--- |
10 | 71 |
|
11 | 72 | This is a `Python` implementation of [Shamir's threshold secret sharing scheme](http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing), based **and compatible with** the `JavaScript` fork of `secrets.js` [*maintained by `grempe`*](https://github.com/grempe/secrets.js). Which is orginally based on the code created by `amper5and` on Github. The [original secrets.js can be found there](https://github.com/amper5and/secrets.js/). |
12 | 73 |
|
|
0 commit comments