import Foundation import CryptoKit // For the moment, only supports ecdsa-sha2-nistp256 keys public struct OpenSSHKeyWriter { public init() { } public func data(secret: SecretType) -> Data { lengthAndData(of: Constants.curveType.data(using: .utf8)!) + lengthAndData(of: Constants.curveIdentifier.data(using: .utf8)!) + lengthAndData(of: secret.publicKey) } public func openSSHString(secret: SecretType) -> String { "\(Constants.curveType) \(data(secret: secret).base64EncodedString())" } public func openSSHFingerprint(secret: SecretType) -> String { Insecure.MD5.hash(data: data(secret: secret)) .compactMap { String($0, radix: 16, uppercase: false) } .joined(separator: ":") } } extension OpenSSHKeyWriter { public func lengthAndData(of data: Data) -> Data { let rawLength = UInt32(data.count) var endian = rawLength.bigEndian return Data(bytes: &endian, count: UInt32.bitWidth/8) + data } public func readData() {} } extension OpenSSHKeyWriter { public enum Constants { public static let curveIdentifier = "nistp256" public static let curveType = "ecdsa-sha2-nistp256" } }