Utilities for TheraForge ToolBox: a small cross-cut of helpers used by other frameworks and apps — notably a modern logging façade (OTFLogger), a set of file-system/Data conveniences, and thin wrappers around SwiftSodium for common crypto workflows.
Version: 2.0.0
Minimum Platforms: iOS 16+, watchOS 9+
Release 2.0.0
- Platform Support
- Minimum iOS 16+ / watchOS 9+
- Note: Sodium-backed crypto helpers are available on iOS only.
- Logging
- New
OTFLoggerAPI (per-file categories; nativeOSLog.Loggerunder the hood) - Request/response helpers with duration; request body logged using
.private - Dev-only crash breadcrumbs utility
- Legacy
OTFError,OTFLog,OTFDebug,OTFFaultmarked deprecated (still available)
- New
- Documentation & Tests
- Expanded
///doc comments across modules - Added Swift Testing examples (logger, data/file, SwiftSodium)
- Expanded
Release 1.0.2-beta
- OSLog Update
- Improved logging of all events
- Added Crash Logging
Release 1.0.1-beta
- OSLog Update
- Updated
OSLogto support iOS 14.0+ - Added the watchOS target
- Updated
- What’s New in 2.0.0
- Requirements
- Installation
- Logging
- Crypto (SwiftSodium) — iOS only
- Data & File Helpers
- Unit Tests
- License
- Modern logging API via
OTFLoggerwith per-file categories and nativeOSLog.Logger. - Deprecated legacy shims (
OTFError,OTFLog,OTFDebug,OTFFault) — still available. - Readable network logging helpers for requests/responses (duration, status, headers; request body is private by default).
- Crash breadcrumb utility (development only).
- Platform bump: iOS 16+, watchOS 9+.
- Docs & examples refreshed, plus example tests using Apple’s Swift Testing.
- iOS 16+
- watchOS 9+
- Swift 5.9+ (or newer)
Platform note: Sodium-backed crypto helpers and some related extensions are iOS-only.
Package Manager: CocoaPods only (v2.0.0)
Minimum Platforms: iOS 16+, watchOS 9+
-
Ensure CocoaPods is installed and up to date:
sudo gem install cocoapods pod --version
-
Add OTFUtilities to your
Podfile.iOS-only example
platform :ios, '16.0' target 'YourApp' do use_frameworks! pod 'OTFUtilities', '~> 2.0.0' end
iOS + watchOS example
abstract_target 'YourApp' do pod 'OTFUtilities', '~> 2.0.0' target 'YourApp-iOS' do platform :ios, '16.0' use_frameworks! end target 'YourApp-watchOS' do platform :watchos, '9.0' use_frameworks! end end
-
Install the pod:
pod repo update # optional, but recommended pod install -
Open the generated
.xcworkspaceand import the module where needed:import OTFUtilities
Note: Swift Package Manager is not supported for this release.
import OTFUtilities
// The category is derived from the current file name.
let log = OTFLogger.logger()
log.debug("App bootstrapped")
log.info("User signed in")
log.warning("Cache miss for key: \(key, privacy: .public)")
log.error("Request failed: \(requestID, privacy: .public)")
log.critical("Unrecoverable state")You have full access to Apple’s Logger interpolation and privacy controls:
let email = "user@example.com"
let token = "abcd…"
let log = OTFLogger.logger()
log.notice("Login for \(email, privacy: .private(mask: .hash)) succeeded")
log.debug("Using token: \(token, privacy: .private)")OTFLogger.logger() categorizes logs by the calling file automatically (derived from #fileID). For files with heavy logging, define a scoped constant:
private let log = OTFLogger.logger() // category == "MyFile"Need an explicit category? Use:
let paymentsLog = OTFLogger.logger(category: "Payments")The following legacy helpers still exist but are deprecated. Replace them with the shown equivalents:
| Legacy | Replacement (OTFLogger) |
|---|---|
OTFError(...) |
OTFLogger.logger().error("…") |
OTFLog(...) |
OTFLogger.logger().info("…") |
OTFDebug(...) |
OTFLogger.logger().debug("…") |
OTFFault(...) |
OTFLogger.logger().critical("…") |
Helpers to log a request and then the corresponding response including duration:
let req: URLRequest = …
logRequest(req)
URLSession.shared.dataTask(with: req) { data, resp, err in
logResponse(req, response: resp, data: data, error: err)
}.resume()- The request body (if any) is logged with
.privateprivacy. - Response duration is auto-computed between
logRequestandlogResponse.
Enable lightweight breadcrumbs for uncaught exceptions and common POSIX signals:
setupCrashLogging()Warning: These handlers are intended for development only. Use a proper crash reporter in production.
The Sodium-backed crypto utilities are compiled on iOS only.
let crypto = SwiftSodium()
// Master key from password + email
let masterKey = try crypto.generateMasterKey(password: "your-password",
email: "user@example.com")
// Storage keys derived from master
let defaultStorageKey = try crypto.defaultStorageKey(from: masterKey)
let confidentialStorageKey = try crypto.confidentialStorageKey(from: masterKey)
// Derive a sub-key (index defaults to 1)
let fileKey = try crypto.deriveKey(from: defaultStorageKey)let dataBytes: Bytes = Array("document bytes".utf8)
// BLAKE2b keyed (32-byte)
let keyedHash = try crypto.genericHash(message: dataBytes, key: fileKey)
// BLAKE2b unkeyed (32-byte)
let unkeyedHash = try crypto.genericHash(message: dataBytes)let box = crypto.sodium.box
let keyPair = box.keyPair()!
// Encrypt key bytes for recipient (anonymous sender)
let encrypted: Bytes = try crypto.encryptSealedBox(bytes: masterKey,
recipientPublicKey: keyPair.publicKey)
// Decrypt using recipient key pair
let decrypted: Bytes = try crypto.decryptSealedBox(bytes: encrypted,
recipientPublicKey: keyPair.publicKey,
recipientSecretKey: keyPair.secretKey)// Create a push stream and encrypt a single chunk
let push = try crypto.makePushStream(secretKey: fileKey)
let cipherChunk: Bytes = try crypto.pushChunk(push, message: Array(fileData))
// Combine header + chunk for transport
let encryptedWithHeader: [UInt8] = [push.header(), cipherChunk].flatMap { $0 }
// Later, decrypt the chunk
let (header, encrypted) = encryptedWithHeader.splitFile()
let (plainChunk, tag) = try crypto.pullChunk(secretKey: fileKey,
header: header,
ciphertext: encrypted)Security note: Prefer random nonces unless you have a strong reason not to.
// UInt8[] → hex
let hex = [0xDE, 0xAD, 0xBE, 0xEF].bytesToHex(spacing: ":")
// hex → Data (throws on invalid input)
let data = try crypto.hexStringToData("DEADBEEF")// Write to Documents
let blob = Data("hello".utf8)
let url = try FileManager.writeToDocuments(filename: "hello.bin", data: blob)
// Read from Documents
let loaded = try FileManager.readFromDocuments(filename: "hello.bin")
// Delete from Documents
try FileManager.deleteFile(filename: "hello.bin")
// Check if a file exists
let exists = FileManager.fileExistsInDocuments(filename: "hello.bin")These helpers validate filenames and throw descriptive errors when something goes wrong.
The project includes example tests using Apple’s Swift Testing covering:
- Logger basics and caching behavior
- Data + FileManager helpers
- Sodium crypto flows (sealed box, secret stream, hashing, hex)
Run with Xcode’s Test action on a toolchain that supports Swift Testing.
This project is made available under the terms of a modified BSD license. See the LICENSE file.