1 // To use those functions below
2 // You need to import CommonCrypto.h in your bridging header.
3 //
4
5 func
6 DataCryptedByAES( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
7 var wKeyLength = 0
8 if key.length < kCCKeySizeAES128 { assert( false ) }
9 else if key.length < kCCKeySizeAES192 { wKeyLength = kCCKeySizeAES128 }
10 else if key.length < kCCKeySizeAES256 { wKeyLength = kCCKeySizeAES192 }
11 else { wKeyLength = kCCKeySizeAES256 }
12
13 if iv != nil { assert( iv!.length == kCCBlockSizeAES128 ) }
14
15 var wLength = UInt( ( ( p.length + kCCBlockSizeAES128 - 1 ) / kCCBlockSizeAES128 ) * kCCBlockSizeAES128 )
16 let v = NSMutableData( length: Int( wLength ) )!
17 let s: CCCryptorStatus = CCCrypt(
18 operation
19 , CCAlgorithm( kCCAlgorithmAES )
20 , options
21 , key.bytes
22 , UInt( wKeyLength )
23 , iv != nil ? iv!.bytes : nil
24 , p.bytes
25 , UInt( p.length )
26 , v.mutableBytes
27 , wLength
28 , &wLength
29 )
30 v.length = Int( wLength )
31 return ( s, v )
32 }
33
34 func
35 DataEncryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
36 return DataCryptedByAES( CCOperation( kCCEncrypt ), p, key, options, iv )
37 }
38
39 func
40 DataDecryptedByAES( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
41 return DataCryptedByAES( CCOperation( kCCDecrypt ), p, key, options, iv )
42 }
43
44 func
45 DataCryptedByBlowfish( operation: CCOperation, p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
46 if iv != nil { assert( iv!.length == kCCBlockSizeBlowfish ) }
47
48 var wLength = UInt( ( ( p.length + kCCBlockSizeBlowfish - 1 ) / kCCBlockSizeBlowfish ) * kCCBlockSizeBlowfish )
49 let v = NSMutableData( length: Int( wLength ) )!
50 let s: CCCryptorStatus = CCCrypt(
51 operation
52 , CCAlgorithm( kCCAlgorithmBlowfish )
53 , options
54 , key.bytes
55 , UInt( key.length )
56 , iv != nil ? iv!.bytes : nil
57 , p.bytes
58 , UInt( p.length )
59 , v.mutableBytes
60 , wLength
61 , &wLength
62 )
63 v.length = Int( wLength )
64 return ( s, v )
65 }
66
67 func
68 DataEncryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
69 return DataCryptedByBlowfish( CCOperation( kCCEncrypt ), p, key, options, iv )
70 }
71
72 func
73 DataDecryptedByBlowfish( p: NSData, key: NSData, _ options: CCOptions = CCOptions( kCCOptionPKCS7Padding ), _ iv: NSData? = nil ) -> ( CCCryptorStatus, NSData ) {
74 return DataCryptedByBlowfish( CCOperation( kCCDecrypt ), p, key, options, iv )
75 }
76
77
78 //USAGE
79
80 func
81 UTF8Data( p: String ) -> NSData? {
82 return p.dataUsingEncoding( NSUTF8StringEncoding )
83 }
84
85 func
86 Usage() {
87 let wKey = UTF8Data( "teststring" )!
88 let wIV = UTF8Data( "12345678" )!
89 var ( s, data ) = DataEncryptedByBlowfish( UTF8Data( "testmessage" )!, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
90 assert( Int( s ) == kCCSuccess )
91 ( s, data ) = DataDecryptedByBlowfish( data, wKey, CCOptions( kCCOptionPKCS7Padding ), wIV )
92 assert( Int( s ) == kCCSuccess )
93 }
94