secretive/SecretKit/Common/OpenSSH/OpenSSHReader.swift

26 lines
669 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)
}
2020-03-22 02:28:08 +00:00
public func readNextChunk() -> Data {
2020-03-04 07:14:38 +00:00
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
}
}