@onekeyfe/react-native-aes-crypto 1.1.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AesCrypto.podspec +21 -0
- package/ios/AesCrypto.h +56 -0
- package/ios/AesCrypto.mm +419 -0
- package/lib/module/NativeAesCrypto.js +5 -0
- package/lib/module/NativeAesCrypto.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeAesCrypto.d.ts +16 -0
- package/lib/typescript/src/NativeAesCrypto.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +155 -0
- package/src/NativeAesCrypto.ts +33 -0
- package/src/index.tsx +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "AesCrypto"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/OneKeyHQ/app-modules/react-native-aes-crypto.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
|
17
|
+
|
|
18
|
+
s.frameworks = 'Security'
|
|
19
|
+
|
|
20
|
+
install_modules_dependencies(s)
|
|
21
|
+
end
|
package/ios/AesCrypto.h
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#import <AesCryptoSpec/AesCryptoSpec.h>
|
|
2
|
+
|
|
3
|
+
@interface AesCrypto : NativeAesCryptoSpecBase <NativeAesCryptoSpec>
|
|
4
|
+
|
|
5
|
+
- (void)encrypt:(NSString *)data
|
|
6
|
+
key:(NSString *)key
|
|
7
|
+
iv:(NSString *)iv
|
|
8
|
+
algorithm:(NSString *)algorithm
|
|
9
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
10
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
11
|
+
|
|
12
|
+
- (void)decrypt:(NSString *)base64
|
|
13
|
+
key:(NSString *)key
|
|
14
|
+
iv:(NSString *)iv
|
|
15
|
+
algorithm:(NSString *)algorithm
|
|
16
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
17
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
18
|
+
|
|
19
|
+
- (void)pbkdf2:(NSString *)password
|
|
20
|
+
salt:(NSString *)salt
|
|
21
|
+
cost:(double)cost
|
|
22
|
+
length:(double)length
|
|
23
|
+
algorithm:(NSString *)algorithm
|
|
24
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
25
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
26
|
+
|
|
27
|
+
- (void)hmac256:(NSString *)base64
|
|
28
|
+
key:(NSString *)key
|
|
29
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
30
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
31
|
+
|
|
32
|
+
- (void)hmac512:(NSString *)base64
|
|
33
|
+
key:(NSString *)key
|
|
34
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
35
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
36
|
+
|
|
37
|
+
- (void)sha1:(NSString *)text
|
|
38
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
39
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
40
|
+
|
|
41
|
+
- (void)sha256:(NSString *)text
|
|
42
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
43
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
44
|
+
|
|
45
|
+
- (void)sha512:(NSString *)text
|
|
46
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
47
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
48
|
+
|
|
49
|
+
- (void)randomUuid:(RCTPromiseResolveBlock)resolve
|
|
50
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
51
|
+
|
|
52
|
+
- (void)randomKey:(double)length
|
|
53
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
54
|
+
reject:(RCTPromiseRejectBlock)reject;
|
|
55
|
+
|
|
56
|
+
@end
|
package/ios/AesCrypto.mm
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
#import "AesCrypto.h"
|
|
2
|
+
#import <CommonCrypto/CommonCryptor.h>
|
|
3
|
+
#import <CommonCrypto/CommonDigest.h>
|
|
4
|
+
#import <CommonCrypto/CommonHMAC.h>
|
|
5
|
+
#import <CommonCrypto/CommonKeyDerivation.h>
|
|
6
|
+
#import <Security/Security.h>
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// MARK: - Internal helpers
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
static NSString *toHex(NSData *data) {
|
|
13
|
+
const unsigned char *bytes = (const unsigned char *)data.bytes;
|
|
14
|
+
NSMutableString *hex = [NSMutableString new];
|
|
15
|
+
for (NSUInteger i = 0; i < data.length; i++) {
|
|
16
|
+
[hex appendFormat:@"%02x", bytes[i]];
|
|
17
|
+
}
|
|
18
|
+
return [hex copy];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static NSData *fromHex(NSString *string) {
|
|
22
|
+
NSMutableData *data = [[NSMutableData alloc] init];
|
|
23
|
+
unsigned char whole_byte;
|
|
24
|
+
char byte_chars[3] = {'\0', '\0', '\0'};
|
|
25
|
+
NSUInteger len = string.length / 2;
|
|
26
|
+
for (NSUInteger i = 0; i < len; i++) {
|
|
27
|
+
byte_chars[0] = (char)[string characterAtIndex:i * 2];
|
|
28
|
+
byte_chars[1] = (char)[string characterAtIndex:i * 2 + 1];
|
|
29
|
+
whole_byte = (unsigned char)strtol(byte_chars, NULL, 16);
|
|
30
|
+
[data appendBytes:&whole_byte length:1];
|
|
31
|
+
}
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static NSData *aesCBC(NSString *operation, NSData *inputData, NSString *key, NSString *iv, NSString *algorithm) {
|
|
36
|
+
NSData *keyData = fromHex(key);
|
|
37
|
+
NSData *ivData = fromHex(iv);
|
|
38
|
+
|
|
39
|
+
NSArray *algorithms = @[@"aes-128-cbc", @"aes-192-cbc", @"aes-256-cbc"];
|
|
40
|
+
NSUInteger item = [algorithms indexOfObject:algorithm];
|
|
41
|
+
size_t keyLength;
|
|
42
|
+
switch (item) {
|
|
43
|
+
case 0: keyLength = kCCKeySizeAES128; break;
|
|
44
|
+
case 1: keyLength = kCCKeySizeAES192; break;
|
|
45
|
+
default: keyLength = kCCKeySizeAES256; break;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
NSMutableData *buffer = [[NSMutableData alloc] initWithLength:inputData.length + kCCBlockSizeAES128];
|
|
49
|
+
size_t numBytes = 0;
|
|
50
|
+
|
|
51
|
+
CCCryptorStatus status = CCCrypt(
|
|
52
|
+
[operation isEqualToString:@"encrypt"] ? kCCEncrypt : kCCDecrypt,
|
|
53
|
+
kCCAlgorithmAES,
|
|
54
|
+
kCCOptionPKCS7Padding,
|
|
55
|
+
keyData.bytes, keyLength,
|
|
56
|
+
ivData.length ? ivData.bytes : nil,
|
|
57
|
+
inputData.bytes, inputData.length,
|
|
58
|
+
buffer.mutableBytes, buffer.length,
|
|
59
|
+
&numBytes);
|
|
60
|
+
|
|
61
|
+
if (status == kCCSuccess) {
|
|
62
|
+
[buffer setLength:numBytes];
|
|
63
|
+
return buffer;
|
|
64
|
+
}
|
|
65
|
+
return nil;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static NSData *aesCTR(NSString *operation, NSData *inputData, NSString *key, NSString *iv, NSString *algorithm) {
|
|
69
|
+
NSData *keyData = fromHex(key);
|
|
70
|
+
NSData *ivData = fromHex(iv);
|
|
71
|
+
|
|
72
|
+
NSArray *algorithms = @[@"aes-128-ctr", @"aes-192-ctr", @"aes-256-ctr"];
|
|
73
|
+
NSUInteger item = [algorithms indexOfObject:algorithm];
|
|
74
|
+
size_t keyLength;
|
|
75
|
+
switch (item) {
|
|
76
|
+
case 0: keyLength = kCCKeySizeAES128; break;
|
|
77
|
+
case 1: keyLength = kCCKeySizeAES192; break;
|
|
78
|
+
default: keyLength = kCCKeySizeAES256; break;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
NSMutableData *buffer = [[NSMutableData alloc] initWithLength:inputData.length + kCCBlockSizeAES128];
|
|
82
|
+
size_t numBytes = 0;
|
|
83
|
+
CCCryptorRef cryptor = NULL;
|
|
84
|
+
|
|
85
|
+
CCCryptorStatus status = CCCryptorCreateWithMode(
|
|
86
|
+
[operation isEqualToString:@"encrypt"] ? kCCEncrypt : kCCDecrypt,
|
|
87
|
+
kCCModeCTR,
|
|
88
|
+
kCCAlgorithmAES,
|
|
89
|
+
ccPKCS7Padding,
|
|
90
|
+
ivData.length ? ivData.bytes : nil,
|
|
91
|
+
keyData.bytes,
|
|
92
|
+
keyLength,
|
|
93
|
+
NULL, 0, 0,
|
|
94
|
+
kCCModeOptionCTR_BE,
|
|
95
|
+
&cryptor);
|
|
96
|
+
|
|
97
|
+
if (status != kCCSuccess) {
|
|
98
|
+
return nil;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
CCCryptorStatus updateStatus = CCCryptorUpdate(
|
|
102
|
+
cryptor,
|
|
103
|
+
inputData.bytes, inputData.length,
|
|
104
|
+
buffer.mutableBytes, buffer.length,
|
|
105
|
+
&numBytes);
|
|
106
|
+
|
|
107
|
+
if (updateStatus != kCCSuccess) {
|
|
108
|
+
CCCryptorRelease(cryptor);
|
|
109
|
+
return nil;
|
|
110
|
+
}
|
|
111
|
+
buffer.length = numBytes;
|
|
112
|
+
|
|
113
|
+
size_t finalBytes = 0;
|
|
114
|
+
CCCryptorStatus finalStatus = CCCryptorFinal(
|
|
115
|
+
cryptor,
|
|
116
|
+
buffer.mutableBytes, buffer.length,
|
|
117
|
+
&finalBytes);
|
|
118
|
+
|
|
119
|
+
CCCryptorRelease(cryptor);
|
|
120
|
+
|
|
121
|
+
if (finalStatus != kCCSuccess) {
|
|
122
|
+
return nil;
|
|
123
|
+
}
|
|
124
|
+
return buffer;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
// MARK: - TurboModule implementation
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
@implementation AesCrypto
|
|
132
|
+
|
|
133
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
134
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
135
|
+
{
|
|
136
|
+
return std::make_shared<facebook::react::NativeAesCryptoSpecJSI>(params);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
+ (NSString *)moduleName
|
|
140
|
+
{
|
|
141
|
+
return @"AesCrypto";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
+ (BOOL)requiresMainQueueSetup
|
|
145
|
+
{
|
|
146
|
+
return NO;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// MARK: - encrypt
|
|
150
|
+
|
|
151
|
+
- (void)encrypt:(NSString *)data
|
|
152
|
+
key:(NSString *)key
|
|
153
|
+
iv:(NSString *)iv
|
|
154
|
+
algorithm:(NSString *)algorithm
|
|
155
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
156
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
157
|
+
{
|
|
158
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
159
|
+
@try {
|
|
160
|
+
NSData *inputData = fromHex(data);
|
|
161
|
+
NSData *result = nil;
|
|
162
|
+
if ([algorithm containsString:@"ctr"]) {
|
|
163
|
+
result = aesCTR(@"encrypt", inputData, key, iv, algorithm);
|
|
164
|
+
} else {
|
|
165
|
+
result = aesCBC(@"encrypt", inputData, key, iv, algorithm);
|
|
166
|
+
}
|
|
167
|
+
if (result == nil) {
|
|
168
|
+
reject(@"encrypt_fail", @"Encrypt error", nil);
|
|
169
|
+
} else {
|
|
170
|
+
resolve(toHex(result));
|
|
171
|
+
}
|
|
172
|
+
} @catch (NSException *exception) {
|
|
173
|
+
reject(@"encrypt_fail", exception.reason, nil);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// MARK: - decrypt
|
|
179
|
+
|
|
180
|
+
- (void)decrypt:(NSString *)base64
|
|
181
|
+
key:(NSString *)key
|
|
182
|
+
iv:(NSString *)iv
|
|
183
|
+
algorithm:(NSString *)algorithm
|
|
184
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
185
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
186
|
+
{
|
|
187
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
188
|
+
@try {
|
|
189
|
+
NSData *inputData = fromHex(base64);
|
|
190
|
+
NSData *result = nil;
|
|
191
|
+
if ([algorithm containsString:@"ctr"]) {
|
|
192
|
+
result = aesCTR(@"decrypt", inputData, key, iv, algorithm);
|
|
193
|
+
} else {
|
|
194
|
+
result = aesCBC(@"decrypt", inputData, key, iv, algorithm);
|
|
195
|
+
}
|
|
196
|
+
if (result == nil) {
|
|
197
|
+
reject(@"decrypt_fail", @"Decrypt failed", nil);
|
|
198
|
+
} else {
|
|
199
|
+
resolve(toHex(result));
|
|
200
|
+
}
|
|
201
|
+
} @catch (NSException *exception) {
|
|
202
|
+
reject(@"decrypt_fail", exception.reason, nil);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// MARK: - pbkdf2
|
|
208
|
+
|
|
209
|
+
- (void)pbkdf2:(NSString *)password
|
|
210
|
+
salt:(NSString *)salt
|
|
211
|
+
cost:(double)cost
|
|
212
|
+
length:(double)length
|
|
213
|
+
algorithm:(NSString *)algorithm
|
|
214
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
215
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
216
|
+
{
|
|
217
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
218
|
+
@try {
|
|
219
|
+
NSData *passwordData = fromHex(password);
|
|
220
|
+
NSData *saltData = fromHex(salt);
|
|
221
|
+
// length is in bits, convert to bytes
|
|
222
|
+
NSUInteger keyLengthBytes = (NSUInteger)(length / 8);
|
|
223
|
+
NSMutableData *hashKeyData = [NSMutableData dataWithLength:keyLengthBytes];
|
|
224
|
+
|
|
225
|
+
CCPseudoRandomAlgorithm prf = kCCPRFHmacAlgSHA512;
|
|
226
|
+
NSString *algoLower = [algorithm lowercaseString];
|
|
227
|
+
if ([algoLower isEqualToString:@"sha1"]) {
|
|
228
|
+
prf = kCCPRFHmacAlgSHA1;
|
|
229
|
+
} else if ([algoLower isEqualToString:@"sha256"]) {
|
|
230
|
+
prf = kCCPRFHmacAlgSHA256;
|
|
231
|
+
} else if ([algoLower isEqualToString:@"sha512"]) {
|
|
232
|
+
prf = kCCPRFHmacAlgSHA512;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
int status = CCKeyDerivationPBKDF(
|
|
236
|
+
kCCPBKDF2,
|
|
237
|
+
passwordData.bytes, passwordData.length,
|
|
238
|
+
saltData.bytes, saltData.length,
|
|
239
|
+
prf,
|
|
240
|
+
(unsigned int)cost,
|
|
241
|
+
hashKeyData.mutableBytes, hashKeyData.length);
|
|
242
|
+
|
|
243
|
+
if (status == kCCParamError) {
|
|
244
|
+
reject(@"keygen_fail", @"Key derivation parameter error", nil);
|
|
245
|
+
} else {
|
|
246
|
+
resolve(toHex(hashKeyData));
|
|
247
|
+
}
|
|
248
|
+
} @catch (NSException *exception) {
|
|
249
|
+
reject(@"keygen_fail", exception.reason, nil);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// MARK: - hmac256
|
|
255
|
+
|
|
256
|
+
- (void)hmac256:(NSString *)base64
|
|
257
|
+
key:(NSString *)key
|
|
258
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
259
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
260
|
+
{
|
|
261
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
262
|
+
@try {
|
|
263
|
+
NSData *keyData = fromHex(key);
|
|
264
|
+
NSData *inputData = fromHex(base64);
|
|
265
|
+
void *buffer = malloc(CC_SHA256_DIGEST_LENGTH);
|
|
266
|
+
if (!buffer) {
|
|
267
|
+
reject(@"hmac_fail", @"Memory allocation error", nil);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
CCHmac(kCCHmacAlgSHA256,
|
|
271
|
+
keyData.bytes, keyData.length,
|
|
272
|
+
inputData.bytes, inputData.length,
|
|
273
|
+
buffer);
|
|
274
|
+
NSData *result = [NSData dataWithBytesNoCopy:buffer
|
|
275
|
+
length:CC_SHA256_DIGEST_LENGTH
|
|
276
|
+
freeWhenDone:YES];
|
|
277
|
+
resolve(toHex(result));
|
|
278
|
+
} @catch (NSException *exception) {
|
|
279
|
+
reject(@"hmac_fail", exception.reason, nil);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// MARK: - hmac512
|
|
285
|
+
|
|
286
|
+
- (void)hmac512:(NSString *)base64
|
|
287
|
+
key:(NSString *)key
|
|
288
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
289
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
290
|
+
{
|
|
291
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
292
|
+
@try {
|
|
293
|
+
NSData *keyData = fromHex(key);
|
|
294
|
+
NSData *inputData = fromHex(base64);
|
|
295
|
+
void *buffer = malloc(CC_SHA512_DIGEST_LENGTH);
|
|
296
|
+
if (!buffer) {
|
|
297
|
+
reject(@"hmac_fail", @"Memory allocation error", nil);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
CCHmac(kCCHmacAlgSHA512,
|
|
301
|
+
keyData.bytes, keyData.length,
|
|
302
|
+
inputData.bytes, inputData.length,
|
|
303
|
+
buffer);
|
|
304
|
+
NSData *result = [NSData dataWithBytesNoCopy:buffer
|
|
305
|
+
length:CC_SHA512_DIGEST_LENGTH
|
|
306
|
+
freeWhenDone:YES];
|
|
307
|
+
resolve(toHex(result));
|
|
308
|
+
} @catch (NSException *exception) {
|
|
309
|
+
reject(@"hmac_fail", exception.reason, nil);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// MARK: - sha1
|
|
315
|
+
|
|
316
|
+
- (void)sha1:(NSString *)text
|
|
317
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
318
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
319
|
+
{
|
|
320
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
321
|
+
@try {
|
|
322
|
+
NSData *inputData = fromHex(text);
|
|
323
|
+
NSMutableData *result = [[NSMutableData alloc] initWithLength:CC_SHA1_DIGEST_LENGTH];
|
|
324
|
+
CC_SHA1(inputData.bytes, (CC_LONG)inputData.length, result.mutableBytes);
|
|
325
|
+
resolve(toHex(result));
|
|
326
|
+
} @catch (NSException *exception) {
|
|
327
|
+
reject(@"sha1_fail", exception.reason, nil);
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// MARK: - sha256
|
|
333
|
+
|
|
334
|
+
- (void)sha256:(NSString *)text
|
|
335
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
336
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
337
|
+
{
|
|
338
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
339
|
+
@try {
|
|
340
|
+
NSData *inputData = fromHex(text);
|
|
341
|
+
unsigned char *buffer = (unsigned char *)malloc(CC_SHA256_DIGEST_LENGTH);
|
|
342
|
+
if (!buffer) {
|
|
343
|
+
reject(@"sha256_fail", @"Memory allocation error", nil);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
CC_SHA256(inputData.bytes, (CC_LONG)inputData.length, buffer);
|
|
347
|
+
NSData *result = [NSData dataWithBytesNoCopy:buffer
|
|
348
|
+
length:CC_SHA256_DIGEST_LENGTH
|
|
349
|
+
freeWhenDone:YES];
|
|
350
|
+
resolve(toHex(result));
|
|
351
|
+
} @catch (NSException *exception) {
|
|
352
|
+
reject(@"sha256_fail", exception.reason, nil);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// MARK: - sha512
|
|
358
|
+
|
|
359
|
+
- (void)sha512:(NSString *)text
|
|
360
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
361
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
362
|
+
{
|
|
363
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
364
|
+
@try {
|
|
365
|
+
NSData *inputData = fromHex(text);
|
|
366
|
+
unsigned char *buffer = (unsigned char *)malloc(CC_SHA512_DIGEST_LENGTH);
|
|
367
|
+
if (!buffer) {
|
|
368
|
+
reject(@"sha512_fail", @"Memory allocation error", nil);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
CC_SHA512(inputData.bytes, (CC_LONG)inputData.length, buffer);
|
|
372
|
+
NSData *result = [NSData dataWithBytesNoCopy:buffer
|
|
373
|
+
length:CC_SHA512_DIGEST_LENGTH
|
|
374
|
+
freeWhenDone:YES];
|
|
375
|
+
resolve(toHex(result));
|
|
376
|
+
} @catch (NSException *exception) {
|
|
377
|
+
reject(@"sha512_fail", exception.reason, nil);
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// MARK: - randomUuid
|
|
383
|
+
|
|
384
|
+
- (void)randomUuid:(RCTPromiseResolveBlock)resolve
|
|
385
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
386
|
+
{
|
|
387
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
388
|
+
@try {
|
|
389
|
+
NSString *uuid = [[NSUUID UUID] UUIDString];
|
|
390
|
+
resolve(uuid);
|
|
391
|
+
} @catch (NSException *exception) {
|
|
392
|
+
reject(@"uuid_fail", exception.reason, nil);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// MARK: - randomKey
|
|
398
|
+
|
|
399
|
+
- (void)randomKey:(double)length
|
|
400
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
401
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
402
|
+
{
|
|
403
|
+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
404
|
+
@try {
|
|
405
|
+
NSUInteger len = (NSUInteger)length;
|
|
406
|
+
NSMutableData *data = [NSMutableData dataWithLength:len];
|
|
407
|
+
int result = SecRandomCopyBytes(kSecRandomDefault, len, data.mutableBytes);
|
|
408
|
+
if (result != errSecSuccess) {
|
|
409
|
+
reject(@"random_fail", @"Random key generation error", nil);
|
|
410
|
+
} else {
|
|
411
|
+
resolve(toHex(data));
|
|
412
|
+
}
|
|
413
|
+
} @catch (NSException *exception) {
|
|
414
|
+
reject(@"random_fail", exception.reason, nil);
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
@end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeAesCrypto.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAAQ,cAAc;AAgClD,eAAeA,mBAAmB,CAACC,YAAY,CAAO,WAAW,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["NativeAesCrypto"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,OAAOA,eAAe,MAAM,sBAAmB;AAE/C,eAAeA,eAAe","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { TurboModule } from 'react-native';
|
|
2
|
+
export interface Spec extends TurboModule {
|
|
3
|
+
encrypt(data: string, key: string, iv: string, algorithm: string): Promise<string>;
|
|
4
|
+
decrypt(base64: string, key: string, iv: string, algorithm: string): Promise<string>;
|
|
5
|
+
pbkdf2(password: string, salt: string, cost: number, length: number, algorithm: string): Promise<string>;
|
|
6
|
+
hmac256(base64: string, key: string): Promise<string>;
|
|
7
|
+
hmac512(base64: string, key: string): Promise<string>;
|
|
8
|
+
sha1(text: string): Promise<string>;
|
|
9
|
+
sha256(text: string): Promise<string>;
|
|
10
|
+
sha512(text: string): Promise<string>;
|
|
11
|
+
randomUuid(): Promise<string>;
|
|
12
|
+
randomKey(length: number): Promise<string>;
|
|
13
|
+
}
|
|
14
|
+
declare const _default: Spec;
|
|
15
|
+
export default _default;
|
|
16
|
+
//# sourceMappingURL=NativeAesCrypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeAesCrypto.d.ts","sourceRoot":"","sources":["../../../src/NativeAesCrypto.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,OAAO,CACL,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAAC;IACnB,OAAO,CACL,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAAC;IACnB,MAAM,CACJ,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAAC;IACnB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC9B,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC5C;;AAED,wBAAmE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAEhD,eAAe,eAAe,CAAC;AAC/B,YAAY,EAAE,IAAI,IAAI,aAAa,EAAE,MAAM,mBAAmB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onekeyfe/react-native-aes-crypto",
|
|
3
|
+
"version": "1.1.55",
|
|
4
|
+
"description": "react-native-aes-crypto",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"source": "./src/index.tsx",
|
|
10
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
11
|
+
"default": "./lib/module/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"lib",
|
|
18
|
+
"ios",
|
|
19
|
+
"*.podspec",
|
|
20
|
+
"!ios/build",
|
|
21
|
+
"!**/__tests__",
|
|
22
|
+
"!**/__fixtures__",
|
|
23
|
+
"!**/__mocks__",
|
|
24
|
+
"!**/.*"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean": "del-cli ios/build lib",
|
|
28
|
+
"prepare": "bob build",
|
|
29
|
+
"typecheck": "tsc",
|
|
30
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
31
|
+
"test": "jest",
|
|
32
|
+
"release": "yarn prepare && npm whoami && npm publish --access public"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react-native",
|
|
36
|
+
"ios",
|
|
37
|
+
"android"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/OneKeyHQ/app-modules/react-native-aes-crypto.git"
|
|
42
|
+
},
|
|
43
|
+
"author": "@onekeyhq <huanming@onekey.so> (https://github.com/OneKeyHQ/app-modules)",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/OneKeyHQ/app-modules/react-native-aes-crypto/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/OneKeyHQ/app-modules/react-native-aes-crypto#readme",
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"registry": "https://registry.npmjs.org/"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
54
|
+
"@eslint/compat": "^1.3.2",
|
|
55
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
56
|
+
"@eslint/js": "^9.35.0",
|
|
57
|
+
"@react-native/babel-preset": "0.83.0",
|
|
58
|
+
"@react-native/eslint-config": "0.83.0",
|
|
59
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
60
|
+
"@types/jest": "^29.5.14",
|
|
61
|
+
"@types/react": "^19.2.0",
|
|
62
|
+
"commitlint": "^19.8.1",
|
|
63
|
+
"del-cli": "^6.0.0",
|
|
64
|
+
"eslint": "^9.35.0",
|
|
65
|
+
"eslint-config-prettier": "^10.1.8",
|
|
66
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
67
|
+
"jest": "^29.7.0",
|
|
68
|
+
"lefthook": "^2.0.3",
|
|
69
|
+
"prettier": "^2.8.8",
|
|
70
|
+
"react": "19.2.0",
|
|
71
|
+
"react-native": "patch:react-native@npm%3A0.83.0#~/.yarn/patches/react-native-npm-0.83.0-577d0f2d83.patch",
|
|
72
|
+
"react-native-builder-bob": "^0.40.17",
|
|
73
|
+
"release-it": "^19.0.4",
|
|
74
|
+
"turbo": "^2.5.6",
|
|
75
|
+
"typescript": "^5.9.2"
|
|
76
|
+
},
|
|
77
|
+
"peerDependencies": {
|
|
78
|
+
"react": "*",
|
|
79
|
+
"react-native": "*"
|
|
80
|
+
},
|
|
81
|
+
"react-native-builder-bob": {
|
|
82
|
+
"source": "src",
|
|
83
|
+
"output": "lib",
|
|
84
|
+
"targets": [
|
|
85
|
+
[
|
|
86
|
+
"module",
|
|
87
|
+
{
|
|
88
|
+
"esm": true
|
|
89
|
+
}
|
|
90
|
+
],
|
|
91
|
+
[
|
|
92
|
+
"typescript",
|
|
93
|
+
{
|
|
94
|
+
"project": "tsconfig.build.json"
|
|
95
|
+
}
|
|
96
|
+
]
|
|
97
|
+
]
|
|
98
|
+
},
|
|
99
|
+
"codegenConfig": {
|
|
100
|
+
"name": "AesCryptoSpec",
|
|
101
|
+
"type": "modules",
|
|
102
|
+
"jsSrcsDir": "src",
|
|
103
|
+
"android": {
|
|
104
|
+
"javaPackageName": "com.aescrypto"
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
"prettier": {
|
|
108
|
+
"quoteProps": "consistent",
|
|
109
|
+
"singleQuote": true,
|
|
110
|
+
"tabWidth": 2,
|
|
111
|
+
"trailingComma": "es5",
|
|
112
|
+
"useTabs": false
|
|
113
|
+
},
|
|
114
|
+
"jest": {
|
|
115
|
+
"preset": "react-native",
|
|
116
|
+
"modulePathIgnorePatterns": [
|
|
117
|
+
"<rootDir>/lib/"
|
|
118
|
+
]
|
|
119
|
+
},
|
|
120
|
+
"commitlint": {
|
|
121
|
+
"extends": [
|
|
122
|
+
"@commitlint/config-conventional"
|
|
123
|
+
]
|
|
124
|
+
},
|
|
125
|
+
"release-it": {
|
|
126
|
+
"git": {
|
|
127
|
+
"commitMessage": "chore: release ${version}",
|
|
128
|
+
"tagName": "v${version}"
|
|
129
|
+
},
|
|
130
|
+
"npm": {
|
|
131
|
+
"publish": true
|
|
132
|
+
},
|
|
133
|
+
"github": {
|
|
134
|
+
"release": true
|
|
135
|
+
},
|
|
136
|
+
"plugins": {
|
|
137
|
+
"@release-it/conventional-changelog": {
|
|
138
|
+
"preset": {
|
|
139
|
+
"name": "angular"
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
"create-react-native-library": {
|
|
145
|
+
"type": "turbo-module",
|
|
146
|
+
"languages": "kotlin-objc",
|
|
147
|
+
"tools": [
|
|
148
|
+
"eslint",
|
|
149
|
+
"jest",
|
|
150
|
+
"lefthook",
|
|
151
|
+
"release-it"
|
|
152
|
+
],
|
|
153
|
+
"version": "0.56.0"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
2
|
+
import type { TurboModule } from 'react-native';
|
|
3
|
+
|
|
4
|
+
export interface Spec extends TurboModule {
|
|
5
|
+
encrypt(
|
|
6
|
+
data: string,
|
|
7
|
+
key: string,
|
|
8
|
+
iv: string,
|
|
9
|
+
algorithm: string,
|
|
10
|
+
): Promise<string>;
|
|
11
|
+
decrypt(
|
|
12
|
+
base64: string,
|
|
13
|
+
key: string,
|
|
14
|
+
iv: string,
|
|
15
|
+
algorithm: string,
|
|
16
|
+
): Promise<string>;
|
|
17
|
+
pbkdf2(
|
|
18
|
+
password: string,
|
|
19
|
+
salt: string,
|
|
20
|
+
cost: number,
|
|
21
|
+
length: number,
|
|
22
|
+
algorithm: string,
|
|
23
|
+
): Promise<string>;
|
|
24
|
+
hmac256(base64: string, key: string): Promise<string>;
|
|
25
|
+
hmac512(base64: string, key: string): Promise<string>;
|
|
26
|
+
sha1(text: string): Promise<string>;
|
|
27
|
+
sha256(text: string): Promise<string>;
|
|
28
|
+
sha512(text: string): Promise<string>;
|
|
29
|
+
randomUuid(): Promise<string>;
|
|
30
|
+
randomKey(length: number): Promise<string>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default TurboModuleRegistry.getEnforcing<Spec>('AesCrypto');
|
package/src/index.tsx
ADDED