【发布时间】:2021-06-04 15:52:41
【问题描述】:
我有一个需要在我的 Swift 代码中使用的 C 库和一个函数,该函数需要一个 32 字节长的 UnsafeMutablePointer<UInt8> 从现有私钥 (crypto_sign_public_key) 生成公钥。这是我正在调用的 C 函数文档的链接:https://monocypher.org/manual/sign。我在 C 和指针方面没有太多经验,但我已经阅读了一些教程并且有一些似乎可以完美运行的东西。
除此之外,我想看看是否有比我更有经验的人能够说出这段代码是否“安全”和/或是否会产生未定义的行为?
func generatePublicKey(from privateKey: [UInt8]) -> [UInt8] {
/// Create an empty pointer that is 32 bytes long. The resulting public key will be written to the memory the pointer is pointing to.
let publicKeyPointer = UnsafeMutablePointer<UInt8>.allocate(capacity: 32)
/// This is using the 25519 curve and the Blake2b hash method.
/// Sign the private key and in turn, create the public key.
crypto_sign_public_key(publicKeyPointer, privateKey)
/// Go over the 32 bytes of the pointer and save it's locations as a "buffer"
let buffer = UnsafeMutableBufferPointer(start: publicKeyPointer, count: 32)
/// Copy the data being pointed to, into an actual array.
let publicKey = Array(buffer)
/// Deallocate the memory being used by the pointer.
publicKeyPointer.deallocate()
return publicKey
}
谢谢!
【问题讨论】:
标签: c swift pointers unsafemutablepointer