A Swift implementation of Shamir's Secret Sharing that is fully compatible with the JavaScript implementation by Joseph Werle.
This library allows you to split secrets into multiple shares and reconstruct them using a configurable threshold, using a finite field in GF(2^8) with 128-bit padding.
Tested on Swift 6.2 (swift-6.2-RELEASE)
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)
- 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
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 build -t shamirs-swift .
docker run -it shamirs-swift swift run ShamirsSecretSharingCLI --help# 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# From hex shares
swift run ShamirsSecretSharingCLI combine 801a4a3ab36c3cdfb4544b9cbd8b73f5a7f 802a63a4324f56b6f5c4eb27c39853a67cb 8032158752f240ad2b21c14506e3ab1a555
# From base64 shares
swift run ShamirsSecretSharingCLI combine CAGqOqs2w9+0VEucvYtz9afwIDGj6kyST1a2b1xOsnOGfMs= CAKjpDJPVrb1xOsnw5hTpnzL --base64swift run ShamirsSecretSharingCLI testimport 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"This Swift implementation is designed to work seamlessly with web applications using the JavaScript version:
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);# Use the same base64 shares from JavaScript
swift run ShamirsSecretSharingCLI combine <base64-share-1> <base64-share-2> <base64-share-3> --base64Run the test suite to verify compatibility:
swift testRun compatibility tests with the CLI:
swift run ShamirsSecretSharingCLI test- Swift 5.9+
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
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
This project is licensed under the MIT License - see the LICENSE file for details.
- Joseph Werle - Original JavaScript implementation (shamirs-secret-sharing)
- Adi Shamir - Inventor of Shamir's Secret Sharing algorithm
- shamirs-secret-sharing - Original JavaScript implementation
- Shamir's Secret Sharing on Wikipedia
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.