Managed to use Filesystem.download()
to save binary files to the file system. What’s my best approach to load the binary data, encrypt it and feed it to a PDF view?
Should I read it as binary into webview with fetch()
and decrypt it there? Then feed the data as inline URL to an iframe
turning PDF viewer?
Or should I create my own plugin with a readEncrypted(path, key)
function to decrypt it natively?
Can I use a native PDF viewer component that can 1) use only part of my webview to keep menu bar and 2) pass the data directly without writing it plain unencrypted to file system?
Currently testing decryption by extending iOS FileSystem plugin with function:
public func readFileDecrypt(at fileUrl: URL, key: String, iv: String) throws -> String {
print("readFileDecrypt() \(key) \(iv)")
fileUrl.startAccessingSecurityScopedResource()
let encryptedData = try Data(contentsOf: fileUrl)
fileUrl.stopAccessingSecurityScopedResource()
// Decode the base64-encoded key and IV
guard let keyData = Data(base64Encoded: key), keyData.count == 32,
let ivData = Data(base64Encoded: iv), ivData.count == 16 else {
throw NSError(domain: "InvalidKeyOrIV", code: 0, userInfo: ["description": "Key or IV is of incorrect length or not valid base64"])
}
let decryptedData = try decryptAES256CBC(data: encryptedData, key: keyData, iv: ivData)
//print("readFileDecrypt() decryptedData: \(decryptedData.base64EncodedString())")
return decryptedData.base64EncodedString()
}
In webview I test an iFrame
with data URL ("data:application/pdf;base64,"+data
), but that seems to display a single PDF page only, other pages are missing.