secretive/SecretAgentKit/OpenSSHReader.swift

26 lines
676 B
Swift
Raw Normal View History

2020-03-04 07:14:38 +00:00
import Foundation
public class OpenSSHReader {
var remaining: Data
public init(data: Data) {
remaining = Data(data)
}
public func readNextChunk() throws -> Data {
let lengthRange = 0..<(UInt32.bitWidth/8)
let lengthChunk = remaining[lengthRange]
remaining.removeSubrange(lengthRange)
let littleEndianLength = lengthChunk.withUnsafeBytes { pointer in
return pointer.load(as: UInt32.self)
}
let length = Int(littleEndianLength.bigEndian)
let dataRange = 0..<length
let ret = Data(remaining[dataRange])
remaining.removeSubrange(dataRange)
return ret
}
}