CryptoSwift, converting objective-c code to swift -
i need convert objective-c code swift using cryptoswift
. i'm not sure how use functions like: bzero
, getcstring
, malloc
in swift.
+(nsdata*)encryptdata:(nsdata*)data { static nsstring *key = @"bitcave012345678"; char keyptr[kcckeysizeaes128+1]; bzero(keyptr, sizeof(keyptr)); [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding]; nsuinteger datalength = [data length]; size_t buffersize = datalength + kccblocksizeaes128; void *buffer = malloc(buffersize); size_t numbytesencrypted = 0; cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmaes128, kccoptionecbmode, keyptr,kcckeysizeaes128,null,[data bytes],datalength, buffer, buffersize, &numbytesencrypted); if(cryptstatus == kccsuccess) { return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted]; } free(buffer); return nil; }
does has idea how satisfy arguments requiring pointers? trying using unsafemutablepointers
, tried code below, know wrong:
var key: nsstring = "bitcave012345678" var keyptr: array<character> = array<character>(count: 17, repeatedvalue: "0") bzero(&keyptr, 17*sizeof(character)) key.getcstring(&keyptr, maxlength: 17*sizeof(character), encoding: nsutf8stringencoding)
swift 2.0
it not necessary use bzero, getcstring, malloc, here example not:
add security.framework project
add #import bridging header.
// operation either 'kccencrypt' or 'kccdecrypt' func testcrypt(data data:nsdata, keydata:nsdata, operation:int) -> nsdata? { let keybytes = unsafepointer<uint8>(keydata.bytes) let datalength = int(data.length) let databytes = unsafepointer<uint8>(data.bytes) let cryptdata: nsmutabledata! = nsmutabledata(length: int(datalength) + kccblocksizeaes128) let cryptpointer = unsafemutablepointer<uint8>(cryptdata.mutablebytes) let cryptlength = size_t(cryptdata.length) let keylength = size_t(kcckeysizeaes128) let algoritm: ccalgorithm = uint32(kccalgorithmaes128) let options: ccoptions = uint32(kccoptionecbmode + kccoptionpkcs7padding) var numbytesencrypted :size_t = 0 let cryptstatus = cccrypt(ccoperation(operation), algoritm, options, keybytes, keylength, nil, databytes, datalength, cryptpointer, cryptlength, &numbytesencrypted) if uint32(cryptstatus) == uint32(kccsuccess) { cryptdata.length = int(numbytesencrypted) } else { print("error: \(cryptstatus)") } return cryptdata; }
example usage:
let keydata = "12345678901234567890123456789012".datausingencoding(nsutf8stringencoding) let messagedata = "don´t try read text. top secret stuff".datausingencoding(nsutf8stringencoding) let encrypted = testcrypt(data:messagedata!, keydata:keydata!, operation:kccencrypt)
here version in swift arrays
of uint8 no nsdata
objects:
func testcrypt(data data:[uint8], keydata:[uint8], operation:int) -> [uint8]? { let keybytes = unsafemutablepointer<uint8>(keydata) let datalength = data.count let databytes = unsafemutablepointer<uint8>(data) var cryptdata = [uint8](count:data.count+kccblocksizeaes128, repeatedvalue:0) let cryptpointer = unsafemutablepointer<uint8>(cryptdata) let cryptlength = size_t(cryptdata.count) let keylength = size_t(kcckeysizeaes128) let algoritm: ccalgorithm = uint32(kccalgorithmaes128) let options: ccoptions = uint32(kccoptionecbmode + kccoptionpkcs7padding) var numbytesencrypted :size_t = 0 let cryptstatus = cccrypt(ccoperation(operation), algoritm, options, keybytes, keylength, nil, databytes, datalength, cryptpointer, cryptlength, &numbytesencrypted) if uint32(cryptstatus) == uint32(kccsuccess) { cryptdata.removerange(numbytesencrypted..<cryptdata.count) } else { print("error: \(cryptstatus)") } return cryptdata; }
example usage:
let keydata = array("12345678901234567890123456789012".utf8) let messagedata = array("don´t try read text. top secret stuff".utf8) let encrypted = testcrypt(data:messagedata, keydata:keydata, operation:kccencrypt)
Comments
Post a Comment