This open-source Swift library offers a comprehensive collection of cryptographic algorithms. These ciphers can be structured into chains, facilitating the seamless flow of output from one cipher stream to another. This architecture enables concurrent tasks, such as encrypting data while writing the encrypted result to a file. Integrate "StreamKit" into your projects to efficiently utilize these cryptographic functionalities.
In the Xcode press File
-> Add Packages
-> In the search field insert https://github.com/iharkatkavets/StreamKit.git
.
It might require to setup GitHub Account
in the Xcode
First import the library
import StreamKit
let fileURL: URL = ...
guard let fileInputStream = FileInputStream(with: fileURL) else {
return
}
try fileInputStream.open()
let fileContent: Data = try fileInputStream.readToEnd()
// or read chunk by chunk
let tmpBufferLen = 1<<16 // 65KB buffer
var tmpBuffer = Array<UInt8>(repeating: 0, count: tmpBufferLen)
while fileInputStream.hasBytesAvailable {
let readLen = fileInputStream.read(&tmpBuffer, maxLength: tmpBufferLen)
// process tmpBuffer
}
inputFileStream.close()
let fileURL: URL = ...
guard let fileOutputStream = FileOutputStream(with: fileURL) else {
return
}
try fileOutputStream.open()
// write string to a file
try fileOutputStream.write("Hello world", ofEncoding: .utf8)
// or write data to a file
try fileOutputStream.write(Data([0,1,2,3]))
// or write some buffer to a file
let tmpBufferLen = 1<<16 // 65KB buffer
var tmpBuffer = Array<UInt8>(repeating: 0, count: tmpBufferLen)
try compressingStream.write(tmpBuffer, length: readLen)
try fileOutputStream.close()
...
let encryptingStream = AesOutputStream(writingTo: fileOutputStream,
key: key,
iv: iv)
try encryptingStream.open()
try encryptingStream.write("Hello world", ofEncoding: .utf8)
try encryptingStream.close()
...
...
let decryptingStream = AesInputStream(readingFrom: inputFileStream,
key: key,
iv: iv)
try decryptingStream.open()
let tmpBufferLen = 1<<16 // 65KB buffer
var tmpBuffer = Array<UInt8>(repeating: 0, count: tmpBufferLen)
while decryptingStream.hasBytesAvailable {
let readLen = try decryptingStream.read(&tmpBuffer, maxLength: tmpBufferLen)
// process buffer
}
decryptingStream.close()
...
...
let encryptingStream = Salsa20OutputStream(writingTo: fileOutputStream,
key: key,
iv: iv)
try encryptingStream.open()
try encryptingStream.write("Hello world", ofEncoding: .utf8)
try encryptingStream.close()
...
...
let decryptingStream = Salsa20InputStream(readingFrom: inputFileStream,
key: key,
iv: iv)
try decryptingStream.open()
let decryptedData = try decryptingStream.readToEnd()
decryptingStream.close()
...
...
let encryptingStream = ChaCha20OutputStream(writingTo: fileOutputStream,
key: key,
iv: iv)
try encryptingStream.open()
try encryptingStream.write("Hello world", ofEncoding: .utf8)
try encryptingStream.close()
...
...
let decryptingStream = ChaCha20InputStream(readingFrom: inputFileStream,
key: key,
iv: iv)
try decryptingStream.open()
let decryptedData = try decryptingStream.readToEnd()
decryptingStream.close()
...
...
let encryptingStream = TwoFishOutputStream(writingTo: fileOutputStream,
key: key,
iv: iv)
try encryptingStream.open()
try encryptingStream.write("Hello world", ofEncoding: .utf8)
try encryptingStream.close()
...
...
let decryptingStream = TwoFishInputStream(readingFrom: inputFileStream,
key: key,
iv: iv)
try decryptingStream.open()
let tmpBufferLen = 1<<16 // 65KB buffer
var tmpBuffer = Array<UInt8>(repeating: 0, count: tmpBufferLen)
while decryptingStream.hasBytesAvailable {
let readLen = try decompressingStream.read(&tmpBuffer, maxLength: tmpBufferLen)
// process or store bytes in `tmpBuffer`
}
decryptingStream.close()
...
For example it's possible to encrypt some data and simulteniously write the encrypted result to a file.
let secureFileURL: URL = ...
let fileHandle = try! FileHandle(forWritingTo: secureFileURL)
let outputFileStream = FileOutputStream(with: fileHandle)
try outputFileStream.open()
let encryptingStream = Salsa20OutputStream(writingTo: outputFileStream,
key: key,
iv: iv)
try encryptingStream.open()
let tmpBufferLen = 1<<16
var tmpBuffer = Array<UInt8>(repeating: 0, count: tmpBufferLen)
while inputFileStream.hasBytesAvailable {
let readLen = inputFileStream.read(&tmpBuffer, maxLength: tmpBufferLen)
try encryptingStream.write(tmpBuffer, length: readLen)
}
try encryptingStream.close()
try outputFileStream.close()
Another example demonstrate reading the encrypted file
let inputFileStream = FileInputStream(withFileHandle: try! FileHandle(forReadingFrom: secureFileURL))
try inputFileStream.open()
let bufferingStream = BufferOutputStream()
try bufferingStream.open()
let decryptingStream = Salsa20InputStream(readingFrom: inputFileStream,
key: key,
iv: iv)
try decryptingStream.open()
let tmpBufferLen = 1<<16 // 65KB buffer
var tmpBuffer = Array<UInt8>(repeating: 0, count: tmpBufferLen)
while decryptingStream.hasBytesAvailable {
let readLen = try decryptingStream.read(&tmpBuffer, maxLength: tmpBufferLen)
try bufferingStream.write(tmpBuffer, length: readLen)
}
decryptingStream.close()
try bufferingStream.close()
inputFileStream.close()