├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── ios ├── RNAES.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ └── tenaciousmv.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── tenaciousmv.xcuserdatad │ │ └── xcschemes │ │ ├── RNAES.xcscheme │ │ └── xcschememanagement.plist └── RNAES │ ├── RNAES.h │ └── RNAES.m └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mark Vayngrib 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-aes 2 | 3 | AES encryption/decryption in react native 4 | 5 | ## Supported Ciphers 6 | 7 | * AES-256-CBC 8 | 9 | ## Usage 10 | 11 | ```js 12 | var AES = require('react-native-aes') 13 | var Buffer = require('buffer').Buffer 14 | 15 | var stringInput = 'hey ho' 16 | var bufferInput = new Buffer(stringInput) 17 | // sample key 18 | var key = new Buffer('f0ki13SQeRpLQrqk73UxhBAI7vd35FgYrNkVybgBIxc=', 'base64') 19 | var cipherName = 'AES-256-CBC' 20 | AES.encryptWithCipher( 21 | cipherName, // String 22 | bufferInput, // Buffer (input data) 23 | key, // AES key, e.g. 32 bytes of random data 24 | function (err, encrypted) { 25 | // "encrypted" is of the form 26 | // { 27 | // ciphertext: Buffer, 28 | // iv: Buffer 29 | // } 30 | // 31 | // you'll need both parts to decrypt 32 | 33 | AES.decryptWithCipher( 34 | cipherName, // String 35 | encrypted.ciphertext, // Buffer (input data) 36 | key, 37 | encrypted.iv, // Buffer 38 | function (err, plaintext) { 39 | // plaintext is a Buffer 40 | if (plaintext.toString() !== stringInput) { 41 | throw new Error('time to report an issue!') 42 | } 43 | } 44 | ) 45 | } 46 | ) 47 | ``` 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | var { NativeModules } = require('react-native') 3 | var RNAES = NativeModules.RNAES 4 | var Buffer = require('buffer').Buffer 5 | var assert = function (statement, err) { 6 | if (!statement) throw normalizeError(err || 'assert failed') 7 | } 8 | 9 | export function encryptWithCipher (cipherName, data, key, cb) { 10 | assert(typeof cipherName === 'string', 'expected "String" cipherName') 11 | assert(Buffer.isBuffer(data), 'expected Buffer "data"') 12 | assert(Buffer.isBuffer(key), 'expected Buffer "key"') 13 | assert(typeof cb === 'function', 'expected Function "cb"') 14 | return RNAES.encryptWithCipher( 15 | cipherName, 16 | toBase64String(data), 17 | toBase64String(key), 18 | function (err, result) { 19 | if (err) return cb(normalizeError(err)) 20 | 21 | cb(null, { 22 | ciphertext: toBuffer(result.ciphertext), 23 | iv: toBuffer(result.iv) 24 | }) 25 | } 26 | ) 27 | } 28 | 29 | export function decryptWithCipher (cipherName, data, key, iv, cb) { 30 | assert(typeof cipherName === 'string', 'expected "String" cipherName') 31 | assert(Buffer.isBuffer(data), 'expected Buffer "data"') 32 | assert(Buffer.isBuffer(key), 'expected Buffer "key"') 33 | assert(Buffer.isBuffer(iv), 'expected Buffer "iv"') 34 | assert(typeof cb === 'function', 'expected Function "cb"') 35 | return RNAES.decryptWithCipher( 36 | cipherName, 37 | toBase64String(data), 38 | toBase64String(key), 39 | toBase64String(iv), 40 | function (err, plaintext) { 41 | if (err) return cb(normalizeError(err)) 42 | 43 | cb(null, toBuffer(plaintext)) 44 | } 45 | ) 46 | } 47 | 48 | function normalizeError (msg) { 49 | return msg instanceof Error ? msg : new Error(msg) 50 | } 51 | 52 | function toBase64String (buf) { 53 | return typeof buf === 'string' ? buf : buf.toString('base64') 54 | } 55 | 56 | function toBuffer (buf) { 57 | return Buffer.isBuffer(buf) ? buf : new Buffer(buf, 'base64') 58 | } 59 | -------------------------------------------------------------------------------- /ios/RNAES.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 739B70B31C22823E00BCD8BA /* RNAES.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 739B70B21C22823E00BCD8BA /* RNAES.h */; }; 11 | 739B70B51C22823E00BCD8BA /* RNAES.m in Sources */ = {isa = PBXBuildFile; fileRef = 739B70B41C22823E00BCD8BA /* RNAES.m */; }; 12 | /* End PBXBuildFile section */ 13 | 14 | /* Begin PBXCopyFilesBuildPhase section */ 15 | 739B70AD1C22823E00BCD8BA /* CopyFiles */ = { 16 | isa = PBXCopyFilesBuildPhase; 17 | buildActionMask = 2147483647; 18 | dstPath = "include/$(PRODUCT_NAME)"; 19 | dstSubfolderSpec = 16; 20 | files = ( 21 | 739B70B31C22823E00BCD8BA /* RNAES.h in CopyFiles */, 22 | ); 23 | runOnlyForDeploymentPostprocessing = 0; 24 | }; 25 | /* End PBXCopyFilesBuildPhase section */ 26 | 27 | /* Begin PBXFileReference section */ 28 | 739B70AF1C22823E00BCD8BA /* libRNAES.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNAES.a; sourceTree = BUILT_PRODUCTS_DIR; }; 29 | 739B70B21C22823E00BCD8BA /* RNAES.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNAES.h; sourceTree = ""; }; 30 | 739B70B41C22823E00BCD8BA /* RNAES.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNAES.m; sourceTree = ""; }; 31 | /* End PBXFileReference section */ 32 | 33 | /* Begin PBXFrameworksBuildPhase section */ 34 | 739B70AC1C22823E00BCD8BA /* Frameworks */ = { 35 | isa = PBXFrameworksBuildPhase; 36 | buildActionMask = 2147483647; 37 | files = ( 38 | ); 39 | runOnlyForDeploymentPostprocessing = 0; 40 | }; 41 | /* End PBXFrameworksBuildPhase section */ 42 | 43 | /* Begin PBXGroup section */ 44 | 739B70A61C22823E00BCD8BA = { 45 | isa = PBXGroup; 46 | children = ( 47 | 739B70B11C22823E00BCD8BA /* RNAES */, 48 | 739B70B01C22823E00BCD8BA /* Products */, 49 | ); 50 | sourceTree = ""; 51 | }; 52 | 739B70B01C22823E00BCD8BA /* Products */ = { 53 | isa = PBXGroup; 54 | children = ( 55 | 739B70AF1C22823E00BCD8BA /* libRNAES.a */, 56 | ); 57 | name = Products; 58 | sourceTree = ""; 59 | }; 60 | 739B70B11C22823E00BCD8BA /* RNAES */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 739B70B21C22823E00BCD8BA /* RNAES.h */, 64 | 739B70B41C22823E00BCD8BA /* RNAES.m */, 65 | ); 66 | path = RNAES; 67 | sourceTree = ""; 68 | }; 69 | /* End PBXGroup section */ 70 | 71 | /* Begin PBXNativeTarget section */ 72 | 739B70AE1C22823E00BCD8BA /* RNAES */ = { 73 | isa = PBXNativeTarget; 74 | buildConfigurationList = 739B70B81C22823E00BCD8BA /* Build configuration list for PBXNativeTarget "RNAES" */; 75 | buildPhases = ( 76 | 739B70AB1C22823E00BCD8BA /* Sources */, 77 | 739B70AC1C22823E00BCD8BA /* Frameworks */, 78 | 739B70AD1C22823E00BCD8BA /* CopyFiles */, 79 | ); 80 | buildRules = ( 81 | ); 82 | dependencies = ( 83 | ); 84 | name = RNAES; 85 | productName = RNAES; 86 | productReference = 739B70AF1C22823E00BCD8BA /* libRNAES.a */; 87 | productType = "com.apple.product-type.library.static"; 88 | }; 89 | /* End PBXNativeTarget section */ 90 | 91 | /* Begin PBXProject section */ 92 | 739B70A71C22823E00BCD8BA /* Project object */ = { 93 | isa = PBXProject; 94 | attributes = { 95 | LastUpgradeCheck = 0710; 96 | ORGANIZATIONNAME = "Tradle, Inc."; 97 | TargetAttributes = { 98 | 739B70AE1C22823E00BCD8BA = { 99 | CreatedOnToolsVersion = 7.1.1; 100 | }; 101 | }; 102 | }; 103 | buildConfigurationList = 739B70AA1C22823E00BCD8BA /* Build configuration list for PBXProject "RNAES" */; 104 | compatibilityVersion = "Xcode 3.2"; 105 | developmentRegion = English; 106 | hasScannedForEncodings = 0; 107 | knownRegions = ( 108 | en, 109 | ); 110 | mainGroup = 739B70A61C22823E00BCD8BA; 111 | productRefGroup = 739B70B01C22823E00BCD8BA /* Products */; 112 | projectDirPath = ""; 113 | projectRoot = ""; 114 | targets = ( 115 | 739B70AE1C22823E00BCD8BA /* RNAES */, 116 | ); 117 | }; 118 | /* End PBXProject section */ 119 | 120 | /* Begin PBXSourcesBuildPhase section */ 121 | 739B70AB1C22823E00BCD8BA /* Sources */ = { 122 | isa = PBXSourcesBuildPhase; 123 | buildActionMask = 2147483647; 124 | files = ( 125 | 739B70B51C22823E00BCD8BA /* RNAES.m in Sources */, 126 | ); 127 | runOnlyForDeploymentPostprocessing = 0; 128 | }; 129 | /* End PBXSourcesBuildPhase section */ 130 | 131 | /* Begin XCBuildConfiguration section */ 132 | 739B70B61C22823E00BCD8BA /* Debug */ = { 133 | isa = XCBuildConfiguration; 134 | buildSettings = { 135 | ALWAYS_SEARCH_USER_PATHS = NO; 136 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 137 | CLANG_CXX_LIBRARY = "libc++"; 138 | CLANG_ENABLE_MODULES = YES; 139 | CLANG_ENABLE_OBJC_ARC = YES; 140 | CLANG_WARN_BOOL_CONVERSION = YES; 141 | CLANG_WARN_CONSTANT_CONVERSION = YES; 142 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 143 | CLANG_WARN_EMPTY_BODY = YES; 144 | CLANG_WARN_ENUM_CONVERSION = YES; 145 | CLANG_WARN_INT_CONVERSION = YES; 146 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 147 | CLANG_WARN_UNREACHABLE_CODE = YES; 148 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 149 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 150 | COPY_PHASE_STRIP = NO; 151 | DEBUG_INFORMATION_FORMAT = dwarf; 152 | ENABLE_STRICT_OBJC_MSGSEND = YES; 153 | ENABLE_TESTABILITY = YES; 154 | GCC_C_LANGUAGE_STANDARD = gnu99; 155 | GCC_DYNAMIC_NO_PIC = NO; 156 | GCC_NO_COMMON_BLOCKS = YES; 157 | GCC_OPTIMIZATION_LEVEL = 0; 158 | GCC_PREPROCESSOR_DEFINITIONS = ( 159 | "DEBUG=1", 160 | "$(inherited)", 161 | ); 162 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 163 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 164 | GCC_WARN_UNDECLARED_SELECTOR = YES; 165 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 166 | GCC_WARN_UNUSED_FUNCTION = YES; 167 | GCC_WARN_UNUSED_VARIABLE = YES; 168 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 169 | MTL_ENABLE_DEBUG_INFO = YES; 170 | ONLY_ACTIVE_ARCH = YES; 171 | SDKROOT = iphoneos; 172 | }; 173 | name = Debug; 174 | }; 175 | 739B70B71C22823E00BCD8BA /* Release */ = { 176 | isa = XCBuildConfiguration; 177 | buildSettings = { 178 | ALWAYS_SEARCH_USER_PATHS = NO; 179 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 180 | CLANG_CXX_LIBRARY = "libc++"; 181 | CLANG_ENABLE_MODULES = YES; 182 | CLANG_ENABLE_OBJC_ARC = YES; 183 | CLANG_WARN_BOOL_CONVERSION = YES; 184 | CLANG_WARN_CONSTANT_CONVERSION = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_EMPTY_BODY = YES; 187 | CLANG_WARN_ENUM_CONVERSION = YES; 188 | CLANG_WARN_INT_CONVERSION = YES; 189 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 193 | COPY_PHASE_STRIP = NO; 194 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 195 | ENABLE_NS_ASSERTIONS = NO; 196 | ENABLE_STRICT_OBJC_MSGSEND = YES; 197 | GCC_C_LANGUAGE_STANDARD = gnu99; 198 | GCC_NO_COMMON_BLOCKS = YES; 199 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 200 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 201 | GCC_WARN_UNDECLARED_SELECTOR = YES; 202 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 203 | GCC_WARN_UNUSED_FUNCTION = YES; 204 | GCC_WARN_UNUSED_VARIABLE = YES; 205 | IPHONEOS_DEPLOYMENT_TARGET = 9.1; 206 | MTL_ENABLE_DEBUG_INFO = NO; 207 | SDKROOT = iphoneos; 208 | VALIDATE_PRODUCT = YES; 209 | }; 210 | name = Release; 211 | }; 212 | 739B70B91C22823E00BCD8BA /* Debug */ = { 213 | isa = XCBuildConfiguration; 214 | buildSettings = { 215 | HEADER_SEARCH_PATHS = "$(SRCROOT)/../../react-native/React/**"; 216 | OTHER_LDFLAGS = "-ObjC"; 217 | PRODUCT_NAME = "$(TARGET_NAME)"; 218 | SKIP_INSTALL = YES; 219 | }; 220 | name = Debug; 221 | }; 222 | 739B70BA1C22823E00BCD8BA /* Release */ = { 223 | isa = XCBuildConfiguration; 224 | buildSettings = { 225 | HEADER_SEARCH_PATHS = "$(SRCROOT)/../../react-native/React/**"; 226 | OTHER_LDFLAGS = "-ObjC"; 227 | PRODUCT_NAME = "$(TARGET_NAME)"; 228 | SKIP_INSTALL = YES; 229 | }; 230 | name = Release; 231 | }; 232 | /* End XCBuildConfiguration section */ 233 | 234 | /* Begin XCConfigurationList section */ 235 | 739B70AA1C22823E00BCD8BA /* Build configuration list for PBXProject "RNAES" */ = { 236 | isa = XCConfigurationList; 237 | buildConfigurations = ( 238 | 739B70B61C22823E00BCD8BA /* Debug */, 239 | 739B70B71C22823E00BCD8BA /* Release */, 240 | ); 241 | defaultConfigurationIsVisible = 0; 242 | defaultConfigurationName = Release; 243 | }; 244 | 739B70B81C22823E00BCD8BA /* Build configuration list for PBXNativeTarget "RNAES" */ = { 245 | isa = XCConfigurationList; 246 | buildConfigurations = ( 247 | 739B70B91C22823E00BCD8BA /* Debug */, 248 | 739B70BA1C22823E00BCD8BA /* Release */, 249 | ); 250 | defaultConfigurationIsVisible = 0; 251 | defaultConfigurationName = Release; 252 | }; 253 | /* End XCConfigurationList section */ 254 | }; 255 | rootObject = 739B70A71C22823E00BCD8BA /* Project object */; 256 | } 257 | -------------------------------------------------------------------------------- /ios/RNAES.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ios/RNAES.xcodeproj/project.xcworkspace/xcuserdata/tenaciousmv.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvayngrib/react-native-aes/55828eca6d4e2984e7b090252aebaa668644d443/ios/RNAES.xcodeproj/project.xcworkspace/xcuserdata/tenaciousmv.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /ios/RNAES.xcodeproj/xcuserdata/tenaciousmv.xcuserdatad/xcschemes/RNAES.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 34 | 35 | 45 | 46 | 52 | 53 | 54 | 55 | 56 | 57 | 63 | 64 | 70 | 71 | 72 | 73 | 75 | 76 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /ios/RNAES.xcodeproj/xcuserdata/tenaciousmv.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | RNAES.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 739B70AE1C22823E00BCD8BA 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /ios/RNAES/RNAES.h: -------------------------------------------------------------------------------- 1 | // 2 | // RNAESManager.h 3 | // ReactNativeAES 4 | // 5 | // Created by Mark Vayngrib on 12/16/15. 6 | // Copyright © 2015 Facebook. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | #import "RCTBridgeModule.h" 13 | 14 | @interface RNAES : NSObject 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /ios/RNAES/RNAES.m: -------------------------------------------------------------------------------- 1 | // 2 | // RNAESManager.m 3 | // ReactNativeAES 4 | // 5 | // Created by Mark Vayngrib on 12/16/15. 6 | // Copyright © 2015 Tradle. All rights reserved. 7 | // 8 | 9 | #import "RNAES.h" 10 | #import 11 | #import 12 | #import "RCTBridgeModule.h" 13 | #import "RCTLog.h" 14 | 15 | @implementation RNAES 16 | 17 | RCT_EXPORT_MODULE(); 18 | 19 | @synthesize bridge = _bridge; 20 | 21 | NSString * const kRNAESErrorDomain = @"io.tradle.RNAES"; 22 | 23 | //const NSDictionary *CBC = @{ 24 | // @"alogrithm": [NSNumber kCCAlgorithmAES128, 25 | // @"keySize": kCCKeySizeAES128, 26 | // @"blockSize": kCCBlockSizeAES128, 27 | // @"ivSize": kCCBlockSizeAES128 28 | //} 29 | 30 | const CCAlgorithm kAlgorithm = kCCAlgorithmAES128; 31 | const NSUInteger kAlgorithmKeySize = kCCKeySizeAES128; 32 | const NSUInteger kAlgorithmBlockSize = kCCBlockSizeAES128; 33 | const NSUInteger kAlgorithmIVSize = kCCBlockSizeAES128; 34 | 35 | // =================== 36 | 37 | RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location) 38 | { 39 | RCTLogInfo(@"Pretending to create an event %@ at %@", name, location); 40 | } 41 | 42 | RCT_EXPORT_METHOD(encryptWithCipher:(NSString *)cipherName 43 | data:(NSString *) base64Plaintext 44 | key:(NSString *)base64Key 45 | callback:(RCTResponseSenderBlock)callback) 46 | { 47 | if ([cipherName caseInsensitiveCompare:@"aes-256-cbc"] != NSOrderedSame) { 48 | NSString* errMsg = [NSString stringWithFormat:@"cipher %@ not supported", cipherName]; 49 | callback(@[errMsg]); 50 | return; 51 | } 52 | 53 | NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Plaintext options:0]; 54 | NSData *key = [[NSData alloc] initWithBase64EncodedString:base64Key options:0]; 55 | NSData *iv = nil; 56 | NSError *error = nil; 57 | NSData* cipherData = [RNAES encryptData:data key:key iv:&iv error:&error]; 58 | if (error) { 59 | NSString* msg = [[error userInfo] valueForKey:@"NSLocalizedFailureReason"]; 60 | callback(@[msg]); 61 | } else { 62 | NSString *base64Ciphertext = [cipherData base64EncodedStringWithOptions:0]; 63 | NSString *base64IV = [iv base64EncodedStringWithOptions:0]; 64 | callback(@[[NSNull null], @{ 65 | @"iv": base64IV, 66 | @"ciphertext": base64Ciphertext 67 | }]); 68 | } 69 | } 70 | 71 | RCT_EXPORT_METHOD(decryptWithCipher:(NSString *)cipherName 72 | data: base64Str 73 | key:(NSString *)base64Key 74 | iv:(NSString *)base64IV 75 | callback:(RCTResponseSenderBlock)callback) 76 | { 77 | if ([cipherName caseInsensitiveCompare:@"aes-256-cbc"] != NSOrderedSame) { 78 | NSString* errMsg = [NSString stringWithFormat:@"cipher %@ not supported", cipherName]; 79 | callback(@[errMsg]); 80 | return; 81 | } 82 | 83 | NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Str options:0]; 84 | NSData *iv = [[NSData alloc] initWithBase64EncodedString:base64IV options:0]; 85 | NSData *key = [[NSData alloc] initWithBase64EncodedString:base64Key options:0]; 86 | NSError *error = nil; 87 | NSData* plaintext = [RNAES decryptData:data key:key iv:iv error:&error]; 88 | if (error) { 89 | NSString* msg = [[error userInfo] valueForKey:@"NSLocalizedFailureReason"]; 90 | callback(@[msg]); 91 | } else { 92 | NSString * base64Plaintext = [plaintext base64EncodedStringWithOptions:0]; 93 | callback(@[[NSNull null], base64Plaintext]); 94 | } 95 | } 96 | 97 | + (NSData *)encryptData:(NSData *)data 98 | key:(NSData *)key 99 | iv:(NSData **)iv 100 | error:(NSError **)error { 101 | NSAssert(iv, @"IV must not be NULL"); 102 | 103 | *iv = [self randomDataOfLength:kAlgorithmIVSize]; 104 | 105 | size_t outLength; 106 | NSMutableData * 107 | cipherData = [NSMutableData dataWithLength:data.length + 108 | kAlgorithmBlockSize]; 109 | 110 | CCCryptorStatus 111 | result = CCCrypt(kCCEncrypt, // operation 112 | kAlgorithm, // Algorithm 113 | kCCOptionPKCS7Padding, // options 114 | key.bytes, // key 115 | key.length, // keylength 116 | (*iv).bytes,// iv 117 | data.bytes, // dataIn 118 | data.length, // dataInLength, 119 | cipherData.mutableBytes, // dataOut 120 | cipherData.length, // dataOutAvailable 121 | &outLength); // dataOutMoved 122 | 123 | if (result == kCCSuccess) { 124 | cipherData.length = outLength; 125 | } 126 | else { 127 | if (error) { 128 | *error = [NSError errorWithDomain:kRNAESErrorDomain 129 | code:result 130 | userInfo:nil]; 131 | } 132 | return nil; 133 | } 134 | 135 | return cipherData; 136 | } 137 | 138 | + (NSData *)decryptData:(NSData *)data 139 | key:(NSData *)key 140 | iv:(NSData *)iv 141 | error:(NSError **)error { 142 | NSAssert(iv, @"IV must not be NULL"); 143 | 144 | size_t outLength; 145 | NSMutableData *plaintext = [NSMutableData dataWithLength:data.length + kAlgorithmBlockSize]; 146 | 147 | CCCryptorStatus 148 | result = CCCrypt(kCCDecrypt, // operation 149 | kAlgorithm, // Algorithm 150 | kCCOptionPKCS7Padding, // options 151 | key.bytes, // key 152 | key.length, // keylength 153 | iv.bytes,// iv 154 | data.bytes, // dataIn 155 | data.length, // dataInLength, 156 | plaintext.mutableBytes, // dataOut 157 | plaintext.length, // dataOutAvailable 158 | &outLength); // dataOutMoved 159 | 160 | if (result == kCCSuccess) { 161 | plaintext.length = outLength; 162 | } 163 | else { 164 | if (error) { 165 | *error = [NSError errorWithDomain:kRNAESErrorDomain 166 | code:result 167 | userInfo:nil]; 168 | } 169 | return nil; 170 | } 171 | 172 | return plaintext; 173 | } 174 | 175 | // =================== 176 | 177 | + (NSData *)randomDataOfLength:(size_t)length { 178 | NSMutableData *data = [NSMutableData dataWithLength:length]; 179 | 180 | int result = SecRandomCopyBytes(kSecRandomDefault, 181 | length, 182 | data.mutableBytes); 183 | NSAssert(result == 0, @"Unable to generate random bytes: %d", 184 | errno); 185 | 186 | return data; 187 | } 188 | 189 | //- (dispatch_queue_t)methodQueue 190 | //{ 191 | // return dispatch_queue_create("com.tradle.io.React.AESQueue", DISPATCH_QUEUE_SERIAL); 192 | //} 193 | 194 | @end 195 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-aes", 3 | "version": "1.0.0", 4 | "description": "AES in react-native", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/mvayngrib/react-native-aes" 12 | }, 13 | "keywords": [ 14 | "aes", 15 | "encrypt", 16 | "decrypt", 17 | "react-component", 18 | "react-native", 19 | "ios" 20 | ], 21 | "author": "Mark Vayngrib (http://github.com/mvayngrib)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/mvayngrib/react-native-aes/issues" 25 | }, 26 | "homepage": "https://github.com/mvayngrib/react-native-aes", 27 | "dependencies": { 28 | "buffer": "^3.5.5" 29 | } 30 | } 31 | --------------------------------------------------------------------------------