Code Monkey home page Code Monkey logo

shamir-2's Introduction

shamir

Fast, secure, pure python implementation of Shamir's secret sharing algorithm.

https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing

why to use it

Shamir's secret sharing is useful for providing an "N of M" layer. For example, password recovery security questions could be cryptographically imeplemented with this algorithm. Security question answers are put through a key-derivation-function (KDF) or hash and each one is used to encrypt a different secret. Then, the answer to any say 5 of 11 security questions would be enouch to recover the secret.

how to use it

secret, shares = make_random_secret(3, 5)
# generate shares such that 3 of 5 can recover the secret
secret = recover_secret(shares)

Shamir's secret sharing algorithm operates on integer X, Y points, and the secret it stores is a random integer. To be useful, it must be combined with other algorithms. Here's a high level example:

# encrypt, decrypt, hash, and kdf are external functions

def two_of_three_encrypt(plaintext, pw0, pw1, pw2):
    'given a plaintext, secure it so that any 2 may access in the future'
    secret, shares = shamir.make_random_shares(2, 3)
    def encrypt_share(share, pw):
        return encrypt(key=kdf(pw), plaintext=repr(share))
    return (
        encrypt(key=hash(hex(secret)), plaintext=plaintext)) + tuple(
        [encrypt(key=kef(pw), plaintext=repr(share))
         for pw, share in zip((pw0, pw1, pw2), shares)])

def two_of_three_decrypt(encrypted, pwA, pwB):
    'recover the plaintext given 2 of the 3 passwords used to secure'
    ciphertext, shares = encrypted[0], encrypted[1:]
    keyA, keyB = kdf(pwA), kdf(pwB)
    decrypted_shares = []
    for share in shares:
        for key in (keyA, keyB):
            try:
                decrypted_shares.append(
                    decrypt(key=keyA, ciphertext=share))
            except Exception:
                pass
    if len(decrypted_shares) < 2:
        raise ValueError('bad password')
    return decrypt(
        key=hash(hex(shamir.recover_secret(decrypted_shares))),
        ciphertext=ciphertext)

shamir-2's People

Contributors

kurtbrose avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.