Explorar o código

(add) iOS implementation

Daniel Torres %!s(int64=6) %!d(string=hai) anos
pai
achega
c6d2182563

+ 5 - 0
ios/RCTPBKDF2/RCTPBKDF2.h

@@ -0,0 +1,5 @@
+#import <React/RCTBridgeModule.h>
+
+@interface RCTPBKDF2 : NSObject <RCTBridgeModule>
+
+@end

+ 20 - 0
ios/RCTPBKDF2/RCTPBKDF2.m

@@ -0,0 +1,20 @@
+#import "RCTPBKDF2.h"
+#import "PBKDF2.h"
+
+@implementation RCTPBKDF2
+
+RCT_EXPORT_MODULE()
+
+RCT_EXPORT_METHOD(derivationKey:(NSString *)password salt:(NSString *)salt iterations:(nonnull NSNumber *)iterations
+                  resolver:(RCTPromiseResolveBlock)resolve
+                  rejecter:(RCTPromiseRejectBlock)reject) {
+    NSError *error = nil;
+    NSString *data = [PBKDF2 derivationKey:password salt:salt iterations: iterations];
+    if (data == nil) {
+        reject(@"keygen_fail", @"Key generation failed", error);
+    } else {
+        resolve(data);
+    }
+}
+
+@end

+ 6 - 0
ios/RCTPBKDF2/lib/PBKDF2.h

@@ -0,0 +1,6 @@
+#import <Foundation/Foundation.h>
+
+@interface PBKDF2 : NSObject
++ (NSString *) derivationKey:(NSString *)password salt: (NSString *)salt iterations: (nonnull NSNumber *)iterations;
++ (NSString *) toHex: (NSData *)nsdata;
+@end

+ 44 - 0
ios/RCTPBKDF2/lib/PBKDF2.m

@@ -0,0 +1,44 @@
+#import <CommonCrypto/CommonCryptor.h>
+#import <CommonCrypto/CommonDigest.h>
+#import <CommonCrypto/CommonKeyDerivation.h>
+
+#import "PBKDF2.h"
+
+@implementation PBKDF2
+
++ (NSString *) toHex:(NSData *)nsdata {
+    NSString * hexStr = [NSString stringWithFormat:@"%@", nsdata];
+    for(NSString * toRemove in [NSArray arrayWithObjects:@"<", @">", @" ", nil])
+        hexStr = [hexStr stringByReplacingOccurrencesOfString:toRemove withString:@""];
+    return hexStr;
+}
+
++ (NSString *) derivationKey:(NSString *)password salt: (NSString *)salt iterations: (nonnull NSNumber *)iterations {
+    // Data of String to generate Hash key(hexa decimal string).
+    NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
+    NSData *saltData = [salt dataUsingEncoding:NSUTF8StringEncoding];
+
+    // Hash key (hexa decimal) string data length.
+    NSMutableData *hashKeyData = [NSMutableData dataWithLength:CC_SHA512_DIGEST_LENGTH];
+
+    // Key Derivation using PBKDF2 algorithm.
+    int status = CCKeyDerivationPBKDF(
+                    kCCPBKDF2,
+                    passwordData.bytes,
+                    passwordData.length,
+                    saltData.bytes,
+                    saltData.length,
+                    kCCPRFHmacAlgSHA512,
+                    iterations,
+                    hashKeyData.mutableBytes,
+                    hashKeyData.length);
+
+    if (status == kCCParamError) {
+        NSLog(@"Key derivation error");
+        return @"";
+    }
+
+    return [self toHex:hashKeyData];
+}
+
+@end

+ 282 - 0
ios/RNPBKDF2.xcodeproj/project.pbxproj

@@ -0,0 +1,282 @@
+// !$*UTF8*$!
+{
+	archiveVersion = 1;
+	classes = {
+	};
+	objectVersion = 48;
+	objects = {
+
+/* Begin PBXBuildFile section */
+		32D980E11BE9F11C00FA27E5 /* RCTPBKDF2.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 32D980E01BE9F11C00FA27E5 /* RCTPBKDF2.h */; };
+		32D980E31BE9F11C00FA27E5 /* RCTPBKDF2.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D980E21BE9F11C00FA27E5 /* RCTPBKDF2.m */; };
+		9BE20B5B1E4E1A3700696172 /* PBKDF2.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BE20B5A1E4E1A3700696172 /* PBKDF2.m */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXCopyFilesBuildPhase section */
+		32D980DB1BE9F11C00FA27E5 /* CopyFiles */ = {
+			isa = PBXCopyFilesBuildPhase;
+			buildActionMask = 2147483647;
+			dstPath = "include/$(PRODUCT_NAME)";
+			dstSubfolderSpec = 16;
+			files = (
+				32D980E11BE9F11C00FA27E5 /* RCTPBKDF2.h in CopyFiles */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXCopyFilesBuildPhase section */
+
+/* Begin PBXFileReference section */
+		32D980DD1BE9F11C00FA27E5 /* libRCTPBKDF2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTPBKDF2.a; sourceTree = BUILT_PRODUCTS_DIR; };
+		32D980E01BE9F11C00FA27E5 /* RCTPBKDF2.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTPBKDF2.h; sourceTree = "<group>"; };
+		32D980E21BE9F11C00FA27E5 /* RCTPBKDF2.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTPBKDF2.m; sourceTree = "<group>"; };
+		9BE20B591E4E1A3700696172 /* PBKDF2.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBKDF2.h; sourceTree = "<group>"; };
+		9BE20B5A1E4E1A3700696172 /* PBKDF2.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBKDF2.m; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+		32D980DA1BE9F11C00FA27E5 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+		32D980D41BE9F11C00FA27E5 = {
+			isa = PBXGroup;
+			children = (
+				32D980DF1BE9F11C00FA27E5 /* RCTPBKDF2 */,
+				32D980DE1BE9F11C00FA27E5 /* Products */,
+			);
+			sourceTree = "<group>";
+		};
+		32D980DE1BE9F11C00FA27E5 /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				32D980DD1BE9F11C00FA27E5 /* libRCTPBKDF2.a */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
+		32D980DF1BE9F11C00FA27E5 /* RCTPBKDF2 */ = {
+			isa = PBXGroup;
+			children = (
+				32D980E01BE9F11C00FA27E5 /* RCTPBKDF2.h */,
+				32D980E21BE9F11C00FA27E5 /* RCTPBKDF2.m */,
+				32D981161BE9F1B600FA27E5 /* lib */,
+			);
+			path = RCTPBKDF2;
+			sourceTree = "<group>";
+		};
+		32D981161BE9F1B600FA27E5 /* lib */ = {
+			isa = PBXGroup;
+			children = (
+				9BE20B591E4E1A3700696172 /* PBKDF2.h */,
+				9BE20B5A1E4E1A3700696172 /* PBKDF2.m */,
+			);
+			path = lib;
+			sourceTree = "<group>";
+		};
+/* End PBXGroup section */
+
+/* Begin PBXNativeTarget section */
+		32D980DC1BE9F11C00FA27E5 /* RCTPBKDF2 */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = 32D980F11BE9F11C00FA27E5 /* Build configuration list for PBXNativeTarget "RCTPBKDF2" */;
+			buildPhases = (
+				32D980D91BE9F11C00FA27E5 /* Sources */,
+				32D980DA1BE9F11C00FA27E5 /* Frameworks */,
+				32D980DB1BE9F11C00FA27E5 /* CopyFiles */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = RCTPBKDF2;
+			productName = RCTPBKDF2;
+			productReference = 32D980DD1BE9F11C00FA27E5 /* libRCTPBKDF2.a */;
+			productType = "com.apple.product-type.library.static";
+		};
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+		32D980D51BE9F11C00FA27E5 /* Project object */ = {
+			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 0820;
+				ORGANIZATIONNAME = tectiv3;
+				TargetAttributes = {
+					32D980DC1BE9F11C00FA27E5 = {
+						CreatedOnToolsVersion = 6.4;
+					};
+				};
+			};
+			buildConfigurationList = 32D980D81BE9F11C00FA27E5 /* Build configuration list for PBXProject "RCTPBKDF2" */;
+			compatibilityVersion = "Xcode 8.0";
+			developmentRegion = English;
+			hasScannedForEncodings = 0;
+			knownRegions = (
+				en,
+			);
+			mainGroup = 32D980D41BE9F11C00FA27E5;
+			productRefGroup = 32D980DE1BE9F11C00FA27E5 /* Products */;
+			projectDirPath = "";
+			projectRoot = "";
+			targets = (
+				32D980DC1BE9F11C00FA27E5 /* RCTPBKDF2 */,
+			);
+		};
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+		32D980D91BE9F11C00FA27E5 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				9BE20B5B1E4E1A3700696172 /* PBKDF2.m in Sources */,
+				32D980E31BE9F11C00FA27E5 /* RCTPBKDF2.m in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+		32D980EF1BE9F11C00FA27E5 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INFINITE_RECURSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_SUSPICIOUS_MOVE = YES;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_DYNAMIC_NO_PIC = NO;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_OPTIMIZATION_LEVEL = 0;
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"DEBUG=1",
+					"$(inherited)",
+				);
+				GCC_SYMBOLS_PRIVATE_EXTERN = NO;
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
+				MTL_ENABLE_DEBUG_INFO = YES;
+				ONLY_ACTIVE_ARCH = YES;
+				SDKROOT = iphoneos;
+			};
+			name = Debug;
+		};
+		32D980F01BE9F11C00FA27E5 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INFINITE_RECURSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_SUSPICIOUS_MOVE = YES;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				ENABLE_NS_ASSERTIONS = NO;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu99;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				IPHONEOS_DEPLOYMENT_TARGET = 9.0;
+				MTL_ENABLE_DEBUG_INFO = NO;
+				SDKROOT = iphoneos;
+				VALIDATE_PRODUCT = YES;
+			};
+			name = Release;
+		};
+		32D980F21BE9F11C00FA27E5 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				HEADER_SEARCH_PATHS = (
+					"$(inherited)",
+					/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
+					"$(SRCROOT)/../../../react-native/React/**",
+					"$(SRCROOT)/../../../../../node_modules/react-native/React/**",
+				);
+				OTHER_LDFLAGS = "-ObjC";
+				PRODUCT_NAME = "$(TARGET_NAME)";
+				SKIP_INSTALL = YES;
+			};
+			name = Debug;
+		};
+		32D980F31BE9F11C00FA27E5 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				HEADER_SEARCH_PATHS = (
+					"$(inherited)",
+					/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
+					"$(SRCROOT)/../../../react-native/React/**",
+					"$(SRCROOT)/../../../../../node_modules/react-native/React/**",
+				);
+				OTHER_LDFLAGS = "-ObjC";
+				PRODUCT_NAME = "$(TARGET_NAME)";
+				SKIP_INSTALL = YES;
+			};
+			name = Release;
+		};
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+		32D980D81BE9F11C00FA27E5 /* Build configuration list for PBXProject "RCTPBKDF2" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				32D980EF1BE9F11C00FA27E5 /* Debug */,
+				32D980F01BE9F11C00FA27E5 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		32D980F11BE9F11C00FA27E5 /* Build configuration list for PBXNativeTarget "RCTPBKDF2" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				32D980F21BE9F11C00FA27E5 /* Debug */,
+				32D980F31BE9F11C00FA27E5 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+/* End XCConfigurationList section */
+	};
+	rootObject = 32D980D51BE9F11C00FA27E5 /* Project object */;
+}