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:
- File β Add Package Dependencies
- Enter:
https://github.com/CharlZKP/shamirs-secret-sharing-swift - 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 --base64Combine shares to recover secret
# From hex shares
swift run ShamirsSecretSharingCLI combine 801a4a3ab36c3cdfb4544b9cbd8b73f5a7f 802a63a4324f56b6f5c4eb27c39853a67cb 8032158752f240ad2b21c14506e3ab1a555
# From base64 shares
swift run ShamirsSecretSharingCLI combine CAGqOqs2w9+0VEucvYtz9afwIDGj6kyST1a2b1xOsnOGfMs= CAKjpDJPVrb1xOsnw5hTpnzL --base64Run compatibility tests
swift run ShamirsSecretSharingCLI testSwift 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 testRun 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:
- All tests pass:
swift test - Compatibility tests pass:
swift run ShamirsSecretSharingCLI test - Code follows Swift conventions
- 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