1 // To use those functions below 2 // You need to import CommonCrypto.h in your bridging header. 3 // #import <CommonCryp to/CommonC rypto.h> 4 5 func 6 DataCrypte dByAES( operation: CCOperatio n, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionP KCS7Paddin g ), _ iv: NSData? = nil ) -> ( CCCryptorS tatus, NSData ) { 7 var wKeyLength = 0 8 if key.length < kCCKeySize AES128 { assert( false ) } 9 else if key.length < kCCKeySize AES192 { wKeyLength = kCCKeySize AES128 } 10 else if key.length < kCCKeySize AES256 { wKeyLength = kCCKeySize AES192 } 11 else { wKeyLength = kCCKeySize AES256 } 12 13 if iv != nil { assert( iv!.length == kCCBlockSi zeAES128 ) } 14 15 var wLength = UInt( ( ( p.length + kCCBlockSi zeAES128 - 1 ) / kCCBlockSi zeAES128 ) * kCCBlockSi zeAES128 ) 16 let v = NSMutableD ata( length: Int( wLength ) )! 17 let s: CCCryptorS tatus = CCCrypt( 18 operation 19 , CCAlgorith m( kCCAlgorit hmAES ) 20 , options 21 , key.bytes 22 , UInt( wKeyLength ) 23 , iv != nil ? iv!.bytes : nil 24 , p.bytes 25 , UInt( p.length ) 26 , v.mutableByt es 27 , wLength 28 , &wLength 29 ) 30 v.length = Int( wLength ) 31 return ( s, v ) 32 } 33 34 func 35 DataEncryp tedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionP KCS7Paddin g ), _ iv: NSData? = nil ) -> ( CCCryptorS tatus, NSData ) { 36 return DataCrypte dByAES( CCOperatio n( kCCEncrypt ), p, key, options, iv ) 37 } 38 39 func 40 DataDecryp tedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionP KCS7Paddin g ), _ iv: NSData? = nil ) -> ( CCCryptorS tatus, NSData ) { 41 return DataCrypte dByAES( CCOperatio n( kCCDecrypt ), p, key, options, iv ) 42 } 43 44 func 45 DataCrypte dByBlowfis h( operation: CCOperatio n, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionP KCS7Paddin g ), _ iv: NSData? = nil ) -> ( CCCryptorS tatus, NSData ) { 46 if iv != nil { assert( iv!.length == kCCBlockSi zeBlowfish ) } 47 48 var wLength = UInt( ( ( p.length + kCCBlockSi zeBlowfish - 1 ) / kCCBlockSi zeBlowfish ) * kCCBlockSi zeBlowfish ) 49 let v = NSMutableD ata( length: Int( wLength ) )! 50 let s: CCCryptorS tatus = CCCrypt( 51 operation 52 , CCAlgorith m( kCCAlgorit hmBlowfish ) 53 , options 54 , key.bytes 55 , UInt( key.length ) 56 , iv != nil ? iv!.bytes : nil 57 , p.bytes 58 , UInt( p.length ) 59 , v.mutableByt es 60 , wLength 61 , &wLength 62 ) 63 v.length = Int( wLength ) 64 return ( s, v ) 65 } 66 67 func 68 DataEncryp tedByBlowf ish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionP KCS7Paddin g ), _ iv: NSData? = nil ) -> ( CCCryptorS tatus, NSData ) { 69 return DataCrypte dByBlowfis h( CCOperatio n( kCCEncrypt ), p, key, options, iv ) 70 } 71 72 func 73 DataDecryp tedByBlowf ish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionP KCS7Paddin g ), _ iv: NSData? = nil ) -> ( CCCryptorS tatus, NSData ) { 74 return DataCrypte dByBlowfis h( CCOperatio n( kCCDecrypt ), p, key, options, iv ) 75 } 76 77 78 //USAGE 79 80 func 81 UTF8Data( p: String ) -> NSData? { 82 return p.dataUsingE ncoding( NSUTF8Stri ngEncoding ) 83 } 84 85 func 86 Usage() { 87 let wKey = UTF8Data( "teststring " )! 88 let wIV = UTF8Data( "12345678" )! 89 var ( s, data ) = DataEncryp tedByBlowf ish( UTF8Data( "testmessag e" )!, wKey, CCOptions( kCCOptionP KCS7Paddin g ), wIV ) 90 assert( Int( s ) == kCCSuccess ) 91 ( s, data ) = DataDecryp tedByBlowf ish( data, wKey, CCOptions( kCCOptionP KCS7Paddin g ), wIV ) 92 assert( Int( s ) == kCCSuccess ) 93 } 94
posted by
Saturn
on Thu 26 Mar 2015
at 08:45