Customize security storage
Protocol
The default implementation of my.setStorage() and my.getStorage() (as well as my.setStorageSync() and my.getStorageSync()) saves and loads data from NSUserDefaults, which is potentially unsafe. To securely store your sensitive data, implement a class that conforms to GRVSecureLocalStorageEncryptorProtocol. The definition of this protocol is as below.
@protocol GRVSecureLocalStorageEncryptorProtocol <NSObject>
@required
/// An identifier to distinguish different encrytor implementations.
/// While decryption, the identifier of the encrptor will be passed to [-decrypt:identifier:].
@property (nonatomic, copy, readonly) NSString *identifier;
/// To encrypt data
/// @param data data to be encrypted
- (NSString *)encrypt:(NSString *)data;
/// To decrypt data
/// @param data data to be decrypted
/// @param identifier the identifier of the encyptor used when the data was encrypted. If the identifier doesn't match the current implemention, nil should be returned.
- (NSString * _Nullable)decrypt:(NSString *)data identifier: (NSString *)identifier;
@endNote that the 'identifier' property is very important. It's used to decide which encryptor is used for your current encrypted data. Otherwise, the existed encrypted data can never be decrypted again.
Initialization
When initializing Griver, pass your implemented instance to IAPConnect, as shown below.
let extensionDelegate = GRVExtensionDelegate()
extensionDelegate.encryptorForLocalStorage = YOUR_INSTANCE
let config = IAPConnectInitConfig()
config.riverExtensionDelegate = extensionDelegate
.....The default AES implementation
With the protocol above, you can implement any security algorithm. But for your convenience, there is an AES version of such implementation out of the box. All you need to do is to set up the AES implementation with your own key, as shown below:
let keyProvider = GRVAESKeyProvider(key: YOUR_KEY)
let aesEncryptor = GRVAESEncryptor(keyProvider: keyProvider)
extensionDelegate.encryptorForLocalStorage = aesEncryptorThe default AES implementation is using AES (CBC mode) with a key size of 128-bit. So your key must be 16 bytes.