2017-12-30 4 views
0

저는 CryptoSwift (Framework, Cocoapods가로드 됨)로 데이터를 암호화하고 해독합니다 (데이터 당 약 15MB-20MB).UIProgressView에 대한 데이터의 암호화/복호화를 관찰하는 방법은 무엇입니까?

내 질문에 어떻게 진행률을 관찰하고 진행률 표시 줄 (UIProgressView)에 표시 할 수 있습니까?

암호화/암호 해독의 진행 방법을 얻는 (그리고 업데이트하는) 방법을 모르겠습니다.

func aesEncrypt(withKey key: String, iv: String) throws -> Data { 
     let data = self 
     let encrypted = try! AES(key: key.bytes, blockMode: .CBC(iv: iv.bytes), padding: .pkcs7).encrypt([UInt8](data)) 
     let encryptedData: Data = Data(encrypted) 
     return encryptedData.base64EncodedData() 
    } 

이것이 데이터 암호화 방법입니다. (데이터 확장).

답변

1

수 없습니다. 적어도이 API로는 할 수 없습니다. 블록별로 암호화/해독을 시도해보십시오 (운 좋게 AES는 블록 암호 임). 각 블록의 진행률 막대를 업데이트하십시오.

참조 CryptoSwift AES 문서에서 "증분 업데이트는"아이디어를 얻을 수 있습니다 :

do { 
    var encryptor = try AES(key: "passwordpassword", iv: "drowssapdrowssap").makeEncryptor() 

    var ciphertext = Array<UInt8>() 
    // aggregate partial results 
    ciphertext += try encryptor.update(withBytes: Array("Nullam quis risus ".utf8)) 
    ciphertext += try encryptor.update(withBytes: Array("eget urna mollis ".utf8)) 
    ciphertext += try encryptor.update(withBytes: Array("ornare vel eu leo.".utf8)) 
    // finish at the end 
    ciphertext += try encryptor.finish() 

    print(ciphertext.toHexString()) 
} catch { 
    print(error) 
} 

https://github.com/krzyzanowskim/CryptoSwift#aes

그냥 (바람직하게는 백그라운드 스레드/큐) 루프에서 update(withBytes:)를 사용하고 진행 상황을 업데이트 각 반복에서 바 (주 스레드에서이 작업을 수행하는 것을 잊지 마세요). 청크 크기가 작을수록 진행률 업데이트가 점진적으로 이루어집니다. 더 정교한 접근 방식은 InputStreamOutputStream을 사용하는 것입니다

let data = ... // your data goes here 
let chunkSize = 64 * 1024 
var chunkStart = 0 
while chunkStart < data.length { 
    let chunk = data.subdata(in: chunkStart..<min(chunkStart + chunkSize, data.length)) 
    ciphertext += try encryptor.update(withBytes: chunk.bytes) 
    ... // update the progress bar here (don't forget to dispatch it to the main thread) 
    chunkStart += chunkSize 
} 

, 암호화하는 동안 메모리에 데이터를 보유 할 필요가 있는데,이 경우에 : 여기

는 하나의 덩어리로 데이터 청크를 열거 할 수있는 방법 . 나는이 가능성을 독자의 연습으로 탐구하고있다. (한 번에 수십 메가 바이트로 작업하는 한 메모리가 부족하지는 않습니다.)

+0

대단히 감사합니다! 곧 이걸 시험해 볼거야. Btw; 데이터는 문자열이 아니라 이미지입니다. 그래서 이미지를 블록으로 나눌 것입니다, 어떻게하면 될까요? –

+0

@ T.Meyer 내 대답을 업데이트했습니다 –

+0

당신은 위대합니다! 고마워요! –

관련 문제