Git Product home page Git Product logo

swiftysss's Introduction

About

A pure Swift implementation of Shamir's Secret Sharing scheme. SwiftySSS is a library that exposes an API to split secret data buffers into a number of different shares. With the possession of some or all of these shares, the original secret can be restored. You can see demo app in the example project below.

Based on SecretShare.swift

Example

How To Split

let message = Data([UInt8]("Hello World!".utf8))
let secret = try! Secret(data: message, threshold: 3, shares: 5)
let shares = try! secret.split()
        
shares.forEach { share in
    print(share.description)
}
//print
//1-8d736120eef1ca0b2b58971c
//2-1b46510ea3e09d828f9a217c
//3-de505c42223100e6d6aed241
//4-cb1f8a6d9544593ab4d53807
//5-0e0987211495c45eede1cb3a

How To Combine

let share1 = "1-8d736120eef1ca0b2b58971c"
let share2 = "2-1b46510ea3e09d828f9a217c"
let share3 = "3-de505c42223100e6d6aed241"
let share4 = "4-cb1f8a6d9544593ab4d53807"
let share5 = "5-0e0987211495c45eede1cb3a"

let sharesStings = [share1, share2, share3, share4, share5]
let sharesObjects = sharesStings.compactMap { try? Secret.Share(string: $0) }
let someShares = [Secret.Share](sharesObjects[1...3])

let secretData = try!  Secret.combine(shares: someShares)
let secret = String(data: secretData, encoding: .utf8)

print(secret)
//print
//Hello World!

How To Split With Custom Representation

let message = Data([UInt8]("Hello World!".utf8))
let secret = try! Secret(data: message, threshold: 3, shares: 5)
let shares = try! secret.split()

let customRepresentationClosure: ((UInt8, Data) -> String) = { (point, bytes) -> String in
    return "\(point)+\(Data(bytes).hexEncodedString())"
}        
shares.forEach { share in
    print(share.description(closure: customRepresentationClosure))
}
//print
//1+8d736120eef1ca0b2b58971c
//2+1b46510ea3e09d828f9a217c
//3+de505c42223100e6d6aed241
//4+cb1f8a6d9544593ab4d53807
//5+0e0987211495c45eede1cb3a

How To Combine Custom Representation

let share1 = "1+8d736120eef1ca0b2b58971c"
let share2 = "2+1b46510ea3e09d828f9a217c"
let share3 = "3+de505c42223100e6d6aed241"
let share4 = "4+cb1f8a6d9544593ab4d53807"
let share5 = "5+0e0987211495c45eede1cb3a"

let sharesStings = [share1, share2, share3, share4, share5]

let customRepresentationClosure: (Any) throws -> (point: UInt8, bytes: Data) = { (value) -> (point: UInt8, bytes: Data) in
    guard let stringValue = value as? String else {
        throw "Invalid String Representation"
    }
    let components = stringValue.components(separatedBy: "+")
    guard let pointComponent = components.first,
        let point = UInt8(pointComponent)  else {
        throw "Invalid String Representation"
    }
    guard let bytesComponent = components.last,
        let bytes = Data(hex: bytesComponent)  else {
        throw "Invalid String Representation"
    }
    return (point, bytes)
}

let sharesObjects = sharesStings.compactMap { try? Secret.Share(closure: customRepresentationClosure, value: $0) }
let someShares = [Secret.Share](sharesObjects[1...3])

let secretData = try!  Secret.combine(shares: someShares)
let secret = String(data: secretData, encoding: .utf8)

print(secret)
//print
//Hello World!

Installation

CocoaPods

pod 'SwiftySSS', '~> 0.1'

Contributing and License

Distributed under the MIT license. See LICENSE for more information.

swiftysss's People

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.