@@ -11,35 +11,42 @@ import CryptoSwift
1111import Foundation
1212import SwiftBase32
1313
14- extension Data {
15- public init ? ( base64UrlEncoded base64String: String , options: Data . Base64DecodingOptions = [ ] ) {
14+ public extension Data {
15+ init ? ( base64UrlEncoded base64String: String , options: Data . Base64DecodingOptions = [ ] ) {
1616 self . init ( base64Encoded: base64String. replacingOccurrences ( of: " - " , with: " + " ) . replacingOccurrences ( of: " _ " , with: " / " ) , options: options)
1717 }
1818
19- public func base64UrlEncodedString( options: Data . Base64EncodingOptions = [ ] ) -> String {
19+ func base64UrlEncodedString( options: Data . Base64EncodingOptions = [ ] ) -> String {
2020 return base64EncodedString ( options: options) . replacingOccurrences ( of: " + " , with: " - " ) . replacingOccurrences ( of: " / " , with: " _ " )
2121 }
2222}
2323
24- extension FixedWidthInteger {
25- public func byteArray( ) -> [ UInt8 ] {
24+ public extension FixedWidthInteger {
25+ func byteArray( ) -> [ UInt8 ] {
2626 return withUnsafeBytes ( of: self , { [ UInt8] ( $0) } )
2727 }
2828}
2929
30- extension InputStream {
31- public func read( maxLength: Int ) throws -> [ UInt8 ] ? {
30+ public enum InputStreamError : Error {
31+ case readOperationFailed
32+ }
33+
34+ public extension InputStream {
35+ func read( maxLength: Int ) throws -> [ UInt8 ] ? {
3236 var buffer = [ UInt8] ( repeating: 0x00 , count: maxLength)
3337 let length = read ( & buffer, maxLength: maxLength)
34- if length == 0 {
38+ switch length {
39+ case _ where length > 0 :
40+ assert ( length <= buffer. count)
41+ buffer. removeSubrange ( length... )
42+ return buffer
43+ case 0 :
3544 return nil
45+ case _ where length < 0 :
46+ throw streamError ?? InputStreamError . readOperationFailed
47+ default :
48+ fatalError ( )
3649 }
37- guard length > 0 else {
38- throw streamError ?? CryptoError . ioError
39- }
40- assert ( length <= buffer. count)
41- buffer. removeSubrange ( length... )
42- return buffer
4350 }
4451}
4552
@@ -137,13 +144,13 @@ public class Cryptor {
137144
138145 // MARK: - File Header Encryption and Decryption
139146
140- internal func createHeader( ) throws -> FileHeader {
147+ func createHeader( ) throws -> FileHeader {
141148 let nonce = try cryptoSupport. createRandomBytes ( size: kCCBlockSizeAES128)
142149 let contentKey = try cryptoSupport. createRandomBytes ( size: kCCKeySizeAES256)
143150 return FileHeader ( nonce: nonce, contentKey: contentKey)
144151 }
145152
146- internal func encryptHeader( _ header: FileHeader ) throws -> [ UInt8 ] {
153+ func encryptHeader( _ header: FileHeader ) throws -> [ UInt8 ] {
147154 let cleartext = [ UInt8] ( repeating: 0xFF , count: Cryptor . fileHeaderLegacyPayloadSize) + header. contentKey
148155 let ciphertext = try AesCtr . compute ( key: masterkey. aesMasterKey, iv: header. nonce, data: cleartext)
149156 let toBeAuthenticated = header. nonce + ciphertext
@@ -152,7 +159,7 @@ public class Cryptor {
152159 return header. nonce + ciphertext + mac
153160 }
154161
155- internal func decryptHeader( _ header: [ UInt8 ] ) throws -> FileHeader {
162+ func decryptHeader( _ header: [ UInt8 ] ) throws -> FileHeader {
156163 // decompose header:
157164 let beginOfMAC = header. count - Int( CC_SHA256_DIGEST_LENGTH)
158165 let nonce = [ UInt8] ( header [ 0 ..< kCCBlockSizeAES128] )
@@ -208,7 +215,7 @@ public class Cryptor {
208215 try encryptContent ( from: cleartextStream, to: ciphertextStream, cleartextSize: cleartextSize)
209216 }
210217
211- internal func encryptContent( from cleartextStream: InputStream , to ciphertextStream: OutputStream , cleartextSize: Int ? ) throws {
218+ func encryptContent( from cleartextStream: InputStream , to ciphertextStream: OutputStream , cleartextSize: Int ? ) throws {
212219 // create progress:
213220 let progress : Progress
214221 if let cleartextSize = cleartextSize {
@@ -269,7 +276,7 @@ public class Cryptor {
269276 try decryptContent ( from: ciphertextStream, to: cleartextStream, ciphertextSize: ciphertextSize)
270277 }
271278
272- internal func decryptContent( from ciphertextStream: InputStream , to cleartextStream: OutputStream , ciphertextSize: Int ? ) throws {
279+ func decryptContent( from ciphertextStream: InputStream , to cleartextStream: OutputStream , ciphertextSize: Int ? ) throws {
273280 // create progress:
274281 let progress : Progress
275282 if let ciphertextSize = ciphertextSize, let cleartextSize = try ? calculateCleartextSize ( ciphertextSize - Cryptor. fileHeaderSize) {
@@ -297,7 +304,7 @@ public class Cryptor {
297304 }
298305 }
299306
300- internal func encryptSingleChunk( _ chunk: [ UInt8 ] , chunkNumber: UInt64 , headerNonce: [ UInt8 ] , fileKey: [ UInt8 ] ) throws -> [ UInt8 ] {
307+ func encryptSingleChunk( _ chunk: [ UInt8 ] , chunkNumber: UInt64 , headerNonce: [ UInt8 ] , fileKey: [ UInt8 ] ) throws -> [ UInt8 ] {
301308 let chunkNonce = try cryptoSupport. createRandomBytes ( size: kCCBlockSizeAES128)
302309 let ciphertext = try AesCtr . compute ( key: fileKey, iv: chunkNonce, data: chunk)
303310 let toBeAuthenticated = headerNonce + chunkNumber. bigEndian. byteArray ( ) + chunkNonce + ciphertext
@@ -306,7 +313,7 @@ public class Cryptor {
306313 return chunkNonce + ciphertext + mac
307314 }
308315
309- internal func decryptSingleChunk( _ chunk: [ UInt8 ] , chunkNumber: UInt64 , headerNonce: [ UInt8 ] , fileKey: [ UInt8 ] ) throws -> [ UInt8 ] {
316+ func decryptSingleChunk( _ chunk: [ UInt8 ] , chunkNumber: UInt64 , headerNonce: [ UInt8 ] , fileKey: [ UInt8 ] ) throws -> [ UInt8 ] {
310317 assert ( chunk. count >= kCCBlockSizeAES128 + Int( CC_SHA256_DIGEST_LENGTH) , " ciphertext chunk must at least contain nonce + mac " )
311318
312319 // decompose chunk:
0 commit comments