// Local simulation only. Creates an isolated key file; never touches Keychain. import CryptoKit import Foundation let arguments = Array(CommandLine.arguments.dropFirst()) let usage = "Usage: swift sparkle_test_key.swift \n" if arguments == ["--help"] || arguments == ["-h"] { print(usage, terminator: "") exit(0) } guard arguments.count == 1 else { fputs(usage, stderr) exit(2) } do { let directory = URL(fileURLWithPath: arguments[0], isDirectory: true) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700]) let privateURL = directory.appendingPathComponent("private.key") let key: Curve25519.Signing.PrivateKey if FileManager.default.fileExists(atPath: privateURL.path) { let encoded = try String(contentsOf: privateURL, encoding: .utf8) guard let data = Data(base64Encoded: encoded) else { fputs("Invalid test key file\n", stderr) exit(1) } key = try Curve25519.Signing.PrivateKey(rawRepresentation: data) } else { key = Curve25519.Signing.PrivateKey() guard FileManager.default.createFile(atPath: privateURL.path, contents: key.rawRepresentation.base64EncodedData(), attributes: [.posixPermissions: 0o600]) else { fputs("Could not write test key file\n", stderr) exit(1) } } try key.publicKey.rawRepresentation.base64EncodedString().write( to: directory.appendingPathComponent("public.key"), atomically: true, encoding: .utf8) } catch { // Do not print private key contents in diagnostics. fputs("Could not create or read test key files\n", stderr) exit(1) }