Contents

CharlZKP/shamir-secret-sharing-swift

swift implementation of the shamir-secret-sharing lib

πŸ”— JavaScript Compatibility

This Swift implementation is designed to be 100% compatible with the JavaScript version:

  • βœ… Can reconstruct secrets from JavaScript-generated shares
  • βœ… JavaScript can reconstruct secrets from Swift-generated shares
  • βœ… Identical algorithms and data formats
  • βœ… Cross-platform secret sharing

Original JavaScript Library: shamirs-secret-sharing by Joseph Werle (MIT License)

πŸš€ Features

  • Cross-platform compatibility with JavaScript implementation
  • Multiple output formats: Hex and Base64
  • Command-line interface for easy usage
  • Swift Package Manager support
  • Comprehensive test suite with compatibility tests
  • Docker support for containerized usage

πŸ“¦ Installation

Swift Package Manager

Add this to your Package.swift:

dependencies: [
    .package(url: "https://github.com/CharlZKP/shamirs-secret-sharing-swift.git", from: "1.0.0")
]

Or add it through Xcode:

  1. File β†’ Add Package Dependencies
  2. Enter: https://github.com/CharlZKP/shamirs-secret-sharing-swift
  3. Select version and add to your target

Docker

docker build -t shamirs-swift .
docker run -it shamirs-swift swift run ShamirsSecretSharingCLI --help

πŸ›  Usage

Command Line Interface

Split a secret into shares
# Hex output (default)
swift run ShamirsSecretSharingCLI split "my secret" 5 3

# Base64 output (compatible with web applications)
swift run ShamirsSecretSharingCLI split "my secret" 5 3 --base64
Combine shares to recover secret
# From hex shares
swift run ShamirsSecretSharingCLI combine 801a4a3ab36c3cdfb4544b9cbd8b73f5a7f 802a63a4324f56b6f5c4eb27c39853a67cb 8032158752f240ad2b21c14506e3ab1a555

# From base64 shares
swift run ShamirsSecretSharingCLI combine CAGqOqs2w9+0VEucvYtz9afwIDGj6kyST1a2b1xOsnOGfMs= CAKjpDJPVrb1xOsnw5hTpnzL --base64
Run compatibility tests
swift run ShamirsSecretSharingCLI test

Swift Library Usage

import ShamirsSecretSharing

// Split a secret
let secret = "my secret key"
let shares = try ShamirsSecretSharing.split(secret, shares: 5, threshold: 3)

// Convert to hex strings for storage/transmission
let hexShares = shares.map { $0.toString(encoding: .hex) }

// Convert to base64 strings (web-friendly)
let base64Shares = shares.map { $0.toBase64() }

// Reconstruct from hex shares
let recovered = ShamirsSecretSharing.combineToString(hexShares)
print(recovered) // "my secret key"

// Reconstruct from base64 shares
let base64Buffers = try base64Shares.map { try Buffer(fromBase64: $0) }
let recoveredFromBase64 = ShamirsSecretSharing.combine(base64Buffers)
print(recoveredFromBase64.toString()) // "my secret key"

🌐 Web Application Integration

This Swift implementation is designed to work seamlessly with web applications using the JavaScript version:

JavaScript (Browser/Node.js)

import sss from 'shamirs-secret-sharing';

// Split secret
const secret = Buffer.from('my secret');
const shares = sss.split(secret, { shares: 5, threshold: 3 });

// Convert to base64 for easy transmission
const base64Shares = shares.map(s => s.toString('base64'));
console.log(base64Shares);

Swift (Server/CLI)

# Use the same base64 shares from JavaScript
swift run ShamirsSecretSharingCLI combine <base64-share-1> <base64-share-2> <base64-share-3> --base64

πŸ§ͺ Testing

Run the test suite to verify compatibility:

swift test

Run compatibility tests with the CLI:

swift run ShamirsSecretSharingCLI test

πŸ“‹ Requirements

  • Swift 5.9+
  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+

🀝 Contributing

Contributions are welcome! Please ensure:

  1. All tests pass: swift test
  2. Compatibility tests pass: swift run ShamirsSecretSharingCLI test
  3. Code follows Swift conventions
  4. New features include tests

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Joseph Werle - Original JavaScript implementation (shamirs-secret-sharing)
  • Adi Shamir - Inventor of Shamir's Secret Sharing algorithm

πŸ“š Algorithm Details

This implementation uses:

  • Finite Field: GF(2^8)
  • Polynomial Degree: threshold - 1
  • Padding: 128 bits
  • Encoding: Compatible hex/base64 formats
  • Maximum Shares: 255

The algorithm ensures that any threshold number of shares can reconstruct the original secret, but threshold - 1 shares reveal no information about the secret.

Package Metadata

Repository: CharlZKP/shamir-secret-sharing-swift

Stars: 1

Forks: 0

Open issues: 0

Default branch: master

Primary language: swift

License: MIT

README: README.md