gpt4 book ai didi

swift - 如何将文件转换为 Array 以在 cryptoSwift 中加密?

转载 作者:行者123 更新时间:2023-11-30 12:36:06 26 4
gpt4 key购买 nike

我正在尝试使用 Cryptoswift 框架在 Swift 中加密文件。我做到了,但是对于像 mp4、mp3 这样有点重的文件,速度非常慢。如果我以错误的方式实现或者算法是这样的,我真的不知道发生了什么。

这是我的代码。

do {
// write until all is written
let ex = "a"
func writeTo(stream: OutputStream, bytes: Array<UInt8>) {
var writtenCount = 0
while stream.hasSpaceAvailable && writtenCount < bytes.count {
writtenCount += stream.write(bytes, maxLength: bytes.count)
}
}
let path = "somewhere"
let aes = try AES(key: key, iv: iv)
var encryptor = aes.makeEncryptor()

// prepare streams
//let data = Data(bytes: (0..<100).map { $0 })
let inputStream = InputStream(fileAtPath: path)
let outputStream = OutputStream(toFileAtPath: "somewhere", append: false)
inputStream?.open()
outputStream?.open()

var buffer = Array<UInt8>(repeating: 0, count: 2)

// encrypt input stream data and write encrypted result to output stream
while (inputStream?.hasBytesAvailable)! {
let readCount = inputStream?.read(&buffer, maxLength: buffer.count)
if (readCount! > 0) {
try encryptor.update(withBytes: buffer[0..<readCount!]) { (bytes) in
writeTo(stream: outputStream!, bytes: bytes)
}
}
}

// finalize encryption
try encryptor.finish { (bytes) in
writeTo(stream: outputStream!, bytes: bytes)
}

if let ciphertext = outputStream?.property(forKey: Stream.PropertyKey(rawValue: Stream.PropertyKey.dataWrittenToMemoryStreamKey.rawValue)) as? Data {
print("Encrypted stream data: \(ciphertext.toHexString())")
}

} catch {
print(error)
}

最佳答案

我想说,尝试使用发布版本重新测试。假设您正在查看调试构建,预计 Swift 代码将明显变慢

关于swift - 如何将文件转换为 Array<UInt8> 以在 cryptoSwift 中加密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42861489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com