[英]UInt8 Array Swift to const char* C
I have the following function 我有以下功能
public func encryptWithRSAKey(_ data: String, rsaKeyRef: SecKey, padding: SecPadding) -> [UInt8]? {
let blockSize = SecKeyGetBlockSize(rsaKeyRef)
var messageEncrypted = [UInt8](repeating: 0, count: blockSize)
var messageEncryptedSize = blockSize
var status: OSStatus!
status = SecKeyEncrypt(rsaKeyRef, SecPadding.OAEP, data, data.count, &messageEncrypted, &messageEncryptedSize)
if status != noErr {
print("\(logClassName): Encryption Error!")
}
return messageEncrypted
}
the output is encryptedMessage = [9,43,128...] 128 length 输出为加密消息= [9,43,128 ...] 128长度
and I have to send the result into a C function 我必须将结果发送到C函数
void STDCALL CpProxyAvOpenhomeOrgCredentials1BeginSet(THandle aHandle, const char* aId, const char* aUserName, const char* aPassword, uint32_t aPasswordLen, OhNetCallbackAsync aCallback, void* aPtr)
{
CpProxyAvOpenhomeOrgCredentials1C* proxyC = reinterpret_cast<CpProxyAvOpenhomeOrgCredentials1C*>(aHandle);
ASSERT(proxyC != NULL);
Brh buf_aId(aId);
Brh buf_aUserName(aUserName);
Brh buf_aPassword;
buf_aPassword.Set((const TByte*)aPassword, aPasswordLen);
FunctorAsync functor = MakeFunctorAsync(aPtr, (OhNetFunctorAsync)aCallback);
proxyC->BeginSet(buf_aId, buf_aUserName, buf_aPassword, functor);
}
What is the way for encoding messageEncrypted in swift to const char* aPassword in C 将消息编码的方式是什么?在C中快速加密为const char * aPassword
AC function AC功能
void cfunc(const char* aPassword, uint32_t aPasswordLen);
is imported to Swift as 导入为Swift作为
public func cfunc(_ aPassword: UnsafePointer<Int8>!, _ aPasswordLen: UInt32)
For an array, withUnsafeBufferPointer()
gives you a pointer to the (contiguous) element storage. 对于数组,
withUnsafeBufferPointer()
为您提供一个指向(连续)元素存储的指针。 Due to the different signedness (Int8 vs UInt8), the pointer must be "rebound". 由于签名不同(Int8与UInt8),指针必须为“反弹”。 Example:
例:
let encryptedMessage: [UInt8] = [9, 43, 128]
encryptedMessage.withUnsafeBufferPointer {
$0.baseAddress!.withMemoryRebound(to: Int8.self, capacity: encryptedMessage.count) {
cfunc($0, UInt32(encryptedMessage.count))
}
}
I prefer this approach: 我更喜欢这种方法:
If I have: 如果我有:
let uint8ArrayVar:[UInt8]= [0,3,4,5]
If i want to sent it throught my c function I just do: 如果我想通过我的c函数发送它,我就这样做:
cfunc(uint8ArrayVar.map { Int8(bitPattern: $0) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.