@capgo/capacitor-updater 8.0.0 → 8.0.1
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/CapgoCapacitorUpdater.podspec +2 -2
- package/Package.swift +35 -0
- package/README.md +667 -206
- package/android/build.gradle +16 -11
- package/android/proguard-rules.pro +28 -0
- package/android/src/main/AndroidManifest.xml +0 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +134 -194
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +23 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +967 -1027
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +1283 -1180
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipherV2.java +276 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +28 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +45 -48
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +440 -113
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +101 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +32 -0
- package/dist/docs.json +1316 -473
- package/dist/esm/definitions.d.ts +518 -248
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +25 -41
- package/dist/esm/web.js +67 -35
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +67 -35
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +67 -35
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/CapacitorUpdater.swift +736 -361
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +436 -136
- package/ios/Plugin/CryptoCipherV2.swift +310 -0
- package/ios/Plugin/InternalUtils.swift +258 -0
- package/package.json +33 -29
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +0 -153
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CryptoCipher.swift +0 -240
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
package ee.forgr.capacitor_updater;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Created by Awesometic
|
|
11
|
-
* It's encrypt returns Base64 encoded, and also decrypt for Base64 encoded cipher
|
|
12
|
-
* references: http://stackoverflow.com/questions/12471999/rsa-encryption-decryption-in-android
|
|
13
|
-
*/
|
|
14
|
-
import android.util.Base64;
|
|
15
|
-
import java.security.GeneralSecurityException;
|
|
16
|
-
import java.security.InvalidAlgorithmParameterException;
|
|
17
|
-
import java.security.InvalidKeyException;
|
|
18
|
-
import java.security.KeyFactory;
|
|
19
|
-
import java.security.NoSuchAlgorithmException;
|
|
20
|
-
import java.security.PrivateKey;
|
|
21
|
-
import java.security.spec.InvalidKeySpecException;
|
|
22
|
-
import java.security.spec.MGF1ParameterSpec;
|
|
23
|
-
import java.security.spec.PKCS8EncodedKeySpec;
|
|
24
|
-
import javax.crypto.BadPaddingException;
|
|
25
|
-
import javax.crypto.Cipher;
|
|
26
|
-
import javax.crypto.IllegalBlockSizeException;
|
|
27
|
-
import javax.crypto.NoSuchPaddingException;
|
|
28
|
-
import javax.crypto.SecretKey;
|
|
29
|
-
import javax.crypto.spec.IvParameterSpec;
|
|
30
|
-
import javax.crypto.spec.OAEPParameterSpec;
|
|
31
|
-
import javax.crypto.spec.PSource;
|
|
32
|
-
import javax.crypto.spec.SecretKeySpec;
|
|
33
|
-
|
|
34
|
-
public class CryptoCipher {
|
|
35
|
-
|
|
36
|
-
public static byte[] decryptRSA(byte[] source, PrivateKey privateKey)
|
|
37
|
-
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
|
|
38
|
-
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
|
|
39
|
-
OAEPParameterSpec oaepParams = new OAEPParameterSpec(
|
|
40
|
-
"SHA-256",
|
|
41
|
-
"MGF1",
|
|
42
|
-
new MGF1ParameterSpec("SHA-256"),
|
|
43
|
-
PSource.PSpecified.DEFAULT
|
|
44
|
-
);
|
|
45
|
-
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
|
|
46
|
-
byte[] decryptedBytes = cipher.doFinal(source);
|
|
47
|
-
return decryptedBytes;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public static byte[] decryptAES(byte[] cipherText, SecretKey key, byte[] iv) {
|
|
51
|
-
try {
|
|
52
|
-
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
|
|
53
|
-
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
54
|
-
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");
|
|
55
|
-
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
|
|
56
|
-
byte[] decryptedText = cipher.doFinal(cipherText);
|
|
57
|
-
return decryptedText;
|
|
58
|
-
} catch (Exception e) {
|
|
59
|
-
e.printStackTrace();
|
|
60
|
-
}
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public static SecretKey byteToSessionKey(byte[] sessionKey) {
|
|
65
|
-
// rebuild key using SecretKeySpec
|
|
66
|
-
SecretKey originalKey = new SecretKeySpec(
|
|
67
|
-
sessionKey,
|
|
68
|
-
0,
|
|
69
|
-
sessionKey.length,
|
|
70
|
-
"AES"
|
|
71
|
-
);
|
|
72
|
-
return originalKey;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private static PrivateKey readPkcs8PrivateKey(byte[] pkcs8Bytes)
|
|
76
|
-
throws GeneralSecurityException {
|
|
77
|
-
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
78
|
-
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8Bytes);
|
|
79
|
-
try {
|
|
80
|
-
return keyFactory.generatePrivate(keySpec);
|
|
81
|
-
} catch (InvalidKeySpecException e) {
|
|
82
|
-
throw new IllegalArgumentException("Unexpected key format!", e);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private static byte[] join(byte[] byteArray1, byte[] byteArray2) {
|
|
87
|
-
byte[] bytes = new byte[byteArray1.length + byteArray2.length];
|
|
88
|
-
System.arraycopy(byteArray1, 0, bytes, 0, byteArray1.length);
|
|
89
|
-
System.arraycopy(
|
|
90
|
-
byteArray2,
|
|
91
|
-
0,
|
|
92
|
-
bytes,
|
|
93
|
-
byteArray1.length,
|
|
94
|
-
byteArray2.length
|
|
95
|
-
);
|
|
96
|
-
return bytes;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private static PrivateKey readPkcs1PrivateKey(byte[] pkcs1Bytes)
|
|
100
|
-
throws GeneralSecurityException {
|
|
101
|
-
// We can't use Java internal APIs to parse ASN.1 structures, so we build a PKCS#8 key Java can understand
|
|
102
|
-
int pkcs1Length = pkcs1Bytes.length;
|
|
103
|
-
int totalLength = pkcs1Length + 22;
|
|
104
|
-
byte[] pkcs8Header = new byte[] {
|
|
105
|
-
0x30,
|
|
106
|
-
(byte) 0x82,
|
|
107
|
-
(byte) ((totalLength >> 8) & 0xff),
|
|
108
|
-
(byte) (totalLength & 0xff), // Sequence + total length
|
|
109
|
-
0x2,
|
|
110
|
-
0x1,
|
|
111
|
-
0x0, // Integer (0)
|
|
112
|
-
0x30,
|
|
113
|
-
0xD,
|
|
114
|
-
0x6,
|
|
115
|
-
0x9,
|
|
116
|
-
0x2A,
|
|
117
|
-
(byte) 0x86,
|
|
118
|
-
0x48,
|
|
119
|
-
(byte) 0x86,
|
|
120
|
-
(byte) 0xF7,
|
|
121
|
-
0xD,
|
|
122
|
-
0x1,
|
|
123
|
-
0x1,
|
|
124
|
-
0x1,
|
|
125
|
-
0x5,
|
|
126
|
-
0x0, // Sequence: 1.2.840.113549.1.1.1, NULL
|
|
127
|
-
0x4,
|
|
128
|
-
(byte) 0x82,
|
|
129
|
-
(byte) ((pkcs1Length >> 8) & 0xff),
|
|
130
|
-
(byte) (pkcs1Length & 0xff), // Octet string + length
|
|
131
|
-
};
|
|
132
|
-
byte[] pkcs8bytes = join(pkcs8Header, pkcs1Bytes);
|
|
133
|
-
return readPkcs8PrivateKey(pkcs8bytes);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
public static PrivateKey stringToPrivateKey(String private_key)
|
|
137
|
-
throws GeneralSecurityException {
|
|
138
|
-
// Base64 decode the result
|
|
139
|
-
|
|
140
|
-
String pkcs1Pem = private_key.toString();
|
|
141
|
-
pkcs1Pem = pkcs1Pem.replace("-----BEGIN RSA PRIVATE KEY-----", "");
|
|
142
|
-
pkcs1Pem = pkcs1Pem.replace("-----END RSA PRIVATE KEY-----", "");
|
|
143
|
-
pkcs1Pem = pkcs1Pem.replace("\\n", "");
|
|
144
|
-
pkcs1Pem = pkcs1Pem.replace(" ", "");
|
|
145
|
-
|
|
146
|
-
byte[] pkcs1EncodedBytes = Base64.decode(
|
|
147
|
-
pkcs1Pem.getBytes(),
|
|
148
|
-
Base64.DEFAULT
|
|
149
|
-
);
|
|
150
|
-
// extract the private key
|
|
151
|
-
return readPkcs1PrivateKey(pkcs1EncodedBytes);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
#import <UIKit/UIKit.h>
|
|
2
|
-
|
|
3
|
-
//! Project version number for Plugin.
|
|
4
|
-
FOUNDATION_EXPORT double PluginVersionNumber;
|
|
5
|
-
|
|
6
|
-
//! Project version string for Plugin.
|
|
7
|
-
FOUNDATION_EXPORT const unsigned char PluginVersionString[];
|
|
8
|
-
|
|
9
|
-
// In this header, you should import all the public headers of your framework using statements like #import <Plugin/PublicHeader.h>
|
|
10
|
-
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
#import <Foundation/Foundation.h>
|
|
2
|
-
#import <Capacitor/Capacitor.h>
|
|
3
|
-
|
|
4
|
-
// Define the plugin using the CAP_PLUGIN Macro, and
|
|
5
|
-
// each method the plugin supports using the CAP_PLUGIN_METHOD macro.
|
|
6
|
-
CAP_PLUGIN(CapacitorUpdaterPlugin, "CapacitorUpdater",
|
|
7
|
-
CAP_PLUGIN_METHOD(download, CAPPluginReturnPromise);
|
|
8
|
-
CAP_PLUGIN_METHOD(set, CAPPluginReturnPromise);
|
|
9
|
-
CAP_PLUGIN_METHOD(list, CAPPluginReturnPromise);
|
|
10
|
-
CAP_PLUGIN_METHOD(delete, CAPPluginReturnPromise);
|
|
11
|
-
CAP_PLUGIN_METHOD(reset, CAPPluginReturnPromise);
|
|
12
|
-
CAP_PLUGIN_METHOD(current, CAPPluginReturnPromise);
|
|
13
|
-
CAP_PLUGIN_METHOD(reload, CAPPluginReturnPromise);
|
|
14
|
-
CAP_PLUGIN_METHOD(notifyAppReady, CAPPluginReturnPromise);
|
|
15
|
-
CAP_PLUGIN_METHOD(setDelay, CAPPluginReturnPromise);
|
|
16
|
-
CAP_PLUGIN_METHOD(setMultiDelay, CAPPluginReturnPromise);
|
|
17
|
-
CAP_PLUGIN_METHOD(cancelDelay, CAPPluginReturnPromise);
|
|
18
|
-
CAP_PLUGIN_METHOD(getLatest, CAPPluginReturnPromise);
|
|
19
|
-
CAP_PLUGIN_METHOD(setChannel, CAPPluginReturnPromise);
|
|
20
|
-
CAP_PLUGIN_METHOD(getChannel, CAPPluginReturnPromise);
|
|
21
|
-
CAP_PLUGIN_METHOD(setCustomId, CAPPluginReturnPromise);
|
|
22
|
-
CAP_PLUGIN_METHOD(getDeviceId, CAPPluginReturnPromise);
|
|
23
|
-
CAP_PLUGIN_METHOD(getPluginVersion, CAPPluginReturnPromise);
|
|
24
|
-
CAP_PLUGIN_METHOD(next, CAPPluginReturnPromise);
|
|
25
|
-
CAP_PLUGIN_METHOD(isAutoUpdateEnabled, CAPPluginReturnPromise);
|
|
26
|
-
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnPromise);
|
|
27
|
-
)
|
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import Foundation
|
|
8
|
-
import CommonCrypto
|
|
9
|
-
|
|
10
|
-
///
|
|
11
|
-
/// Constants
|
|
12
|
-
///
|
|
13
|
-
private enum CryptoCipherConstants {
|
|
14
|
-
static let rsaKeySizeInBits: NSNumber = 2048
|
|
15
|
-
static let aesAlgorithm: CCAlgorithm = CCAlgorithm(kCCAlgorithmAES)
|
|
16
|
-
static let aesOptions: CCOptions = CCOptions(kCCOptionPKCS7Padding)
|
|
17
|
-
static let rsaAlgorithm: SecKeyAlgorithm = .rsaEncryptionOAEPSHA256
|
|
18
|
-
}
|
|
19
|
-
///
|
|
20
|
-
/// The AES key. Contains both the initialization vector and secret key.
|
|
21
|
-
///
|
|
22
|
-
public struct AES128Key {
|
|
23
|
-
/// Initialization vector
|
|
24
|
-
private let iv: Data
|
|
25
|
-
private let aes128Key: Data
|
|
26
|
-
#if DEBUG
|
|
27
|
-
public var __debug_iv: Data { iv }
|
|
28
|
-
public var __debug_aes128Key: Data { aes128Key }
|
|
29
|
-
#endif
|
|
30
|
-
init(iv: Data, aes128Key: Data) {
|
|
31
|
-
self.iv = iv
|
|
32
|
-
self.aes128Key = aes128Key
|
|
33
|
-
}
|
|
34
|
-
///
|
|
35
|
-
/// Takes the data and uses the private key to decrypt it. Will call `CCCrypt` in CommonCrypto
|
|
36
|
-
/// and provide it `ivData` for the initialization vector. Will use cipher block chaining (CBC) as
|
|
37
|
-
/// the mode of operation.
|
|
38
|
-
///
|
|
39
|
-
/// Returns the decrypted data.
|
|
40
|
-
///
|
|
41
|
-
public func decrypt(data: Data) -> Data? {
|
|
42
|
-
let encryptedData: UnsafePointer<UInt8> = (data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count)
|
|
43
|
-
let encryptedDataLength: Int = data.count
|
|
44
|
-
|
|
45
|
-
if let result: NSMutableData = NSMutableData(length: encryptedDataLength) {
|
|
46
|
-
let keyData: UnsafePointer<UInt8> = (self.aes128Key as NSData).bytes.bindMemory(to: UInt8.self, capacity: self.aes128Key.count)
|
|
47
|
-
let keyLength: size_t = size_t(self.aes128Key.count)
|
|
48
|
-
let ivData: UnsafePointer<UInt8> = (iv as NSData).bytes.bindMemory(to: UInt8.self, capacity: self.iv.count)
|
|
49
|
-
|
|
50
|
-
let decryptedData: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(result.mutableBytes.assumingMemoryBound(to: UInt8.self))
|
|
51
|
-
let decryptedDataLength: size_t = size_t(result.length)
|
|
52
|
-
|
|
53
|
-
var decryptedLength: size_t = 0
|
|
54
|
-
|
|
55
|
-
let status: CCCryptorStatus = CCCrypt(CCOperation(kCCDecrypt), CryptoCipherConstants.aesAlgorithm, CryptoCipherConstants.aesOptions, keyData, keyLength, ivData, encryptedData, encryptedDataLength, decryptedData, decryptedDataLength, &decryptedLength)
|
|
56
|
-
|
|
57
|
-
if UInt32(status) == UInt32(kCCSuccess) {
|
|
58
|
-
result.length = Int(decryptedLength)
|
|
59
|
-
return result as Data
|
|
60
|
-
} else {
|
|
61
|
-
return nil
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
return nil
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
///
|
|
70
|
-
/// The RSA keypair. Includes both private and public key.
|
|
71
|
-
///
|
|
72
|
-
public struct RSAKeyPair {
|
|
73
|
-
private let privateKey: SecKey
|
|
74
|
-
private let publicKey: SecKey
|
|
75
|
-
|
|
76
|
-
#if DEBUG
|
|
77
|
-
public var __debug_privateKey: SecKey { self.privateKey }
|
|
78
|
-
public var __debug_publicKey: SecKey { self.publicKey }
|
|
79
|
-
#endif
|
|
80
|
-
|
|
81
|
-
fileprivate init(privateKey: SecKey, publicKey: SecKey) {
|
|
82
|
-
self.privateKey = privateKey
|
|
83
|
-
self.publicKey = publicKey
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public func extractPublicKey() -> RSAPublicKey {
|
|
87
|
-
RSAPublicKey(publicKey: publicKey)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
///
|
|
91
|
-
/// Takes the data and uses the private key to decrypt it.
|
|
92
|
-
/// Returns the decrypted data.
|
|
93
|
-
///
|
|
94
|
-
public func decrypt(data: Data) -> Data? {
|
|
95
|
-
var error: Unmanaged<CFError>?
|
|
96
|
-
if let decryptedData: CFData = SecKeyCreateDecryptedData(self.privateKey, CryptoCipherConstants.rsaAlgorithm, data as CFData, &error) {
|
|
97
|
-
if error != nil {
|
|
98
|
-
return nil
|
|
99
|
-
} else {
|
|
100
|
-
return decryptedData as Data
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
return nil
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
///
|
|
109
|
-
/// The RSA public key.
|
|
110
|
-
///
|
|
111
|
-
public struct RSAPublicKey {
|
|
112
|
-
private let publicKey: SecKey
|
|
113
|
-
|
|
114
|
-
#if DEBUG
|
|
115
|
-
public var __debug_publicKey: SecKey { self.publicKey }
|
|
116
|
-
#endif
|
|
117
|
-
|
|
118
|
-
fileprivate init(publicKey: SecKey) {
|
|
119
|
-
self.publicKey = publicKey
|
|
120
|
-
}
|
|
121
|
-
///
|
|
122
|
-
/// Takes the data and uses the public key to encrypt it.
|
|
123
|
-
/// Returns the encrypted data.
|
|
124
|
-
///
|
|
125
|
-
public func encrypt(data: Data) -> Data? {
|
|
126
|
-
var error: Unmanaged<CFError>?
|
|
127
|
-
if let encryptedData: CFData = SecKeyCreateEncryptedData(self.publicKey, CryptoCipherConstants.rsaAlgorithm, data as CFData, &error) {
|
|
128
|
-
if error != nil {
|
|
129
|
-
return nil
|
|
130
|
-
} else {
|
|
131
|
-
return encryptedData as Data
|
|
132
|
-
}
|
|
133
|
-
} else {
|
|
134
|
-
return nil
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
///
|
|
138
|
-
/// Allows you to export the RSA public key to a format (so you can send over the net).
|
|
139
|
-
///
|
|
140
|
-
public func export() -> Data? {
|
|
141
|
-
return publicKey.exportToData()
|
|
142
|
-
}
|
|
143
|
-
//
|
|
144
|
-
|
|
145
|
-
///
|
|
146
|
-
/// Allows you to load an RSA public key (i.e. one downloaded from the net).
|
|
147
|
-
///
|
|
148
|
-
public static func load(rsaPublicKeyData: Data) -> RSAPublicKey? {
|
|
149
|
-
if let publicKey: SecKey = .loadPublicFromData(rsaPublicKeyData) {
|
|
150
|
-
return RSAPublicKey(publicKey: publicKey)
|
|
151
|
-
} else {
|
|
152
|
-
return nil
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
///
|
|
157
|
-
/// The RSA public key.
|
|
158
|
-
///
|
|
159
|
-
public struct RSAPrivateKey {
|
|
160
|
-
private let privateKey: SecKey
|
|
161
|
-
|
|
162
|
-
#if DEBUG
|
|
163
|
-
public var __debug_privateKey: SecKey { self.privateKey }
|
|
164
|
-
#endif
|
|
165
|
-
|
|
166
|
-
fileprivate init(privateKey: SecKey) {
|
|
167
|
-
self.privateKey = privateKey
|
|
168
|
-
}
|
|
169
|
-
///
|
|
170
|
-
/// Takes the data and uses the private key to decrypt it.
|
|
171
|
-
/// Returns the decrypted data.
|
|
172
|
-
///
|
|
173
|
-
public func decrypt(data: Data) -> Data? {
|
|
174
|
-
var error: Unmanaged<CFError>?
|
|
175
|
-
if let decryptedData: CFData = SecKeyCreateDecryptedData(self.privateKey, CryptoCipherConstants.rsaAlgorithm, data as CFData, &error) {
|
|
176
|
-
if error != nil {
|
|
177
|
-
return nil
|
|
178
|
-
} else {
|
|
179
|
-
return decryptedData as Data
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
return nil
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
///
|
|
187
|
-
/// Allows you to export the RSA public key to a format (so you can send over the net).
|
|
188
|
-
///
|
|
189
|
-
public func export() -> Data? {
|
|
190
|
-
return privateKey.exportToData()
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
///
|
|
194
|
-
/// Allows you to load an RSA public key (i.e. one downloaded from the net).
|
|
195
|
-
///
|
|
196
|
-
public static func load(rsaPrivateKey: String) -> RSAPrivateKey? {
|
|
197
|
-
var privKey: String = rsaPrivateKey
|
|
198
|
-
privKey = privKey.replacingOccurrences(of: "-----BEGIN RSA PRIVATE KEY-----", with: "")
|
|
199
|
-
privKey = privKey.replacingOccurrences(of: "-----END RSA PRIVATE KEY-----", with: "")
|
|
200
|
-
privKey = privKey.replacingOccurrences(of: "\\n+", with: "", options: .regularExpression)
|
|
201
|
-
privKey = privKey.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
202
|
-
let rsaPrivateKeyData: Data = Data(base64Encoded: privKey)!
|
|
203
|
-
if let privateKey: SecKey = .loadPrivateFromData(rsaPrivateKeyData) {
|
|
204
|
-
return RSAPrivateKey(privateKey: privateKey)
|
|
205
|
-
} else {
|
|
206
|
-
return nil
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
fileprivate extension SecKey {
|
|
212
|
-
func exportToData() -> Data? {
|
|
213
|
-
var error: Unmanaged<CFError>?
|
|
214
|
-
if let cfData: CFData = SecKeyCopyExternalRepresentation(self, &error) {
|
|
215
|
-
if error != nil {
|
|
216
|
-
return nil
|
|
217
|
-
} else {
|
|
218
|
-
return cfData as Data
|
|
219
|
-
}
|
|
220
|
-
} else {
|
|
221
|
-
return nil
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
static func loadPublicFromData(_ data: Data) -> SecKey? {
|
|
225
|
-
let keyDict: [NSObject: NSObject] = [
|
|
226
|
-
kSecAttrKeyType: kSecAttrKeyTypeRSA,
|
|
227
|
-
kSecAttrKeyClass: kSecAttrKeyClassPublic,
|
|
228
|
-
kSecAttrKeySizeInBits: CryptoCipherConstants.rsaKeySizeInBits
|
|
229
|
-
]
|
|
230
|
-
return SecKeyCreateWithData(data as CFData, keyDict as CFDictionary, nil)
|
|
231
|
-
}
|
|
232
|
-
static func loadPrivateFromData(_ data: Data) -> SecKey? {
|
|
233
|
-
let keyDict: [NSObject: NSObject] = [
|
|
234
|
-
kSecAttrKeyType: kSecAttrKeyTypeRSA,
|
|
235
|
-
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
|
|
236
|
-
kSecAttrKeySizeInBits: CryptoCipherConstants.rsaKeySizeInBits
|
|
237
|
-
]
|
|
238
|
-
return SecKeyCreateWithData(data as CFData, keyDict as CFDictionary, nil)
|
|
239
|
-
}
|
|
240
|
-
}
|