如何在 Swift Data 对象中写入新数据



我正在尝试使用适用于iOS的Debenu(Foxit(Quick PDF库将PKCS#7数字签名嵌入到PDF中。根据他们提供的示例代码,我必须在 PDF 文件字节数组中编写签名(十六进制格式(。下面是用 C# 编写的示例代码:

public int Sign(string pdfLocation, byte[] hexSignature, int signaturePlaceholderLength, int signaturePlaceholderStartPosition)
{
if (hexSignature.Length < signaturePlaceholderLength)
{
// Write the signature into the placeholder
using (BinaryWriter writer = new BinaryWriter(new FileStream(pdfLocation, FileMode.Open)))
{
writer.BaseStream.Seek(signaturePlaceholderStartPosition, SeekOrigin.Begin);
writer.BaseStream.Write(hexSignature, 0, hexSignature.Length);
}
}
else
{
AddLog("Error: digital signature is larger than the placeholder size");
}
}

我在 Swift 中编写相同的算法时遇到问题,因为我无法弄清楚如何将签名写入我的 PDFData对象:

func sign(pdfDocument: Data, hexSignature: String, signaturePlaceholderLength: Int, signaturePlaceholderStartPosition: Int) {
if hexSignature.count < signaturePlaceholderLength {
// how can I add hexSignature inside pdfDocument on signaturePlaceholderStartPosition?
}
}

我看到了Data.write函数,但它将数据的内容写入某个位置。

还有Data.insert在指定位置插入UInt8,但我的签名是String(十六进制(。

我试图搜索Swift insert String inside Data object,但找不到任何有用的东西。

那么,我是否误解了Data对象以及如何在 Swift 上使用它们?我是否必须将我的签名转换为Data可以处理的内容?或者我必须使用一些帮助程序类在Data对象中插入新数据?

你为什么不把你的十六进制签名改成一个[UInt8]呢? 因为是你在 C# 中所做的

let hexSignatureBytes: [UInt8] = Array(hexSignature.utf8)

最新更新