一段 object c 代码想转 Swift ,有什么方法吗?
- (NSData *)AES256EncryptWithKey:(NSString *)key { NSUInteger dataLength = [self length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); NSMutableData *data = [[NSData randomDataWithLength:AES_256_IV_SIZE] mutableCopy]; //IV size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [key cStringUsingEncoding:NSUTF8StringEncoding], [key length], [data bytes], [self bytes], dataLength, buffer, bufferSize, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *message = [NSData dataWithBytes:buffer length:numBytesEncrypted]; //Message /* Do HMac */ NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; NSData *hmac = [message doHmacWithKeyData:keyData]; /* IV+Message+HMac */ [data appendData:message]; [data appendData:hmac]; free(buffer); return data; } free(buffer); return nil; } - (NSData *)doHmacWithKeyData:(NSData *)salt { NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; CCHmac(kCCHmacAlgSHA256, salt.bytes, salt.length, self.bytes, self.length, macOut.mutableBytes); return macOut; }