writeToKeychain, readFromKeychain
This commit is contained in:
parent
7e5748b3a6
commit
e49b468fd5
4 changed files with 84 additions and 21 deletions
|
|
@ -38,6 +38,7 @@
|
|||
#include "QZXing.h"
|
||||
|
||||
#include "platforms/ios/QRCodeReaderBase.h"
|
||||
#include "platforms/ios/MobileUtils.h"
|
||||
|
||||
#include "debug.h"
|
||||
#include "defines.h"
|
||||
|
|
@ -143,22 +144,23 @@ int main(int argc, char *argv[])
|
|||
return 0;
|
||||
}
|
||||
|
||||
QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
|
||||
if (!oldSettings.allKeys().isEmpty()) {
|
||||
QSettings newSettingsForPath(QSettings::Format::CustomFormat1, QSettings::UserScope,
|
||||
ORGANIZATION_NAME, APPLICATION_NAME);
|
||||
QString oldSettingsFileName = oldSettings.fileName();
|
||||
QString newSettingsFileName = newSettingsForPath.fileName();
|
||||
|
||||
qDebug() << "New config removed:" << QFile::remove(newSettingsFileName);
|
||||
QSettings newSettings(newSettingsFileName, QSettings::Format::NativeFormat);
|
||||
SecureFormat::chiperSettings(oldSettings, newSettings);
|
||||
|
||||
// QSettings oldSettings(ORGANIZATION_NAME, APPLICATION_NAME);
|
||||
// if (!oldSettings.allKeys().isEmpty()) {
|
||||
// QSettings newSettings(QSettings::Format::CustomFormat1, QSettings::SystemScope,
|
||||
// ORGANIZATION_NAME, APPLICATION_NAME);
|
||||
// QString oldSettingsFileName = oldSettings.fileName();
|
||||
// QString newSettingsFileName = newSettings.fileName();
|
||||
// qDebug() << "oldSettingsFileName:" << oldSettingsFileName;
|
||||
// qDebug() << "newSettingsFileName:" << newSettingsFileName;
|
||||
// qDebug() << "Old config copied:" << QFile::copy(oldSettingsFileName, newSettingsFileName);
|
||||
//// qDebug() << "Old config removed:" << QFile::remove(oldSettingsFileName);
|
||||
}
|
||||
|
||||
// qDebug() << "New config removed:" << QFile::remove(newSettingsFileName);
|
||||
// SecureFormat::chiperSettings(oldSettings, newSettings);
|
||||
//// qDebug() << "Old config copied:" << QFile::copy(oldSettingsFileName, newSettingsFileName);
|
||||
////// qDebug() << "Old config removed:" << QFile::remove(oldSettingsFileName);
|
||||
// }
|
||||
|
||||
MobileUtils::writeToKeychain("testKey", "12345");
|
||||
qDebug() << "MobileUtils::readFromKeychain(\"testKey\"):" << MobileUtils::readFromKeychain("testKey");
|
||||
|
||||
Settings settings;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
#include "MobileUtils.h"
|
||||
|
||||
MobileUtils::MobileUtils()
|
||||
{
|
||||
void MobileUtils::shareText(const QStringList&) {}
|
||||
|
||||
}
|
||||
|
||||
void MobileUtils::shareText(const QStringList& filesToSend) {
|
||||
|
||||
}
|
||||
void MobileUtils::writeToKeychain(const QString&, const QString&) {}
|
||||
QString MobileUtils::readFromKeychain(const QString&) { return {}; }
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ public:
|
|||
|
||||
public slots:
|
||||
static void shareText(const QStringList& filesToSend);
|
||||
|
||||
static void writeToKeychain(const QString& tag, const QString& value);
|
||||
static QString readFromKeychain(const QString& tag);
|
||||
};
|
||||
|
||||
#endif // MOBILEUTILS_H
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
#include "MobileUtils.h"
|
||||
|
||||
#include <UIKit/UIKit.h>
|
||||
#include <Security/Security.h>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
static UIViewController* getViewController() {
|
||||
NSArray *windows = [[UIApplication sharedApplication]windows];
|
||||
|
|
@ -31,3 +34,62 @@ void MobileUtils::shareText(const QStringList& filesToSend) {
|
|||
popController.sourceView = qtController.view;
|
||||
}
|
||||
}
|
||||
|
||||
bool deleteFromKeychain(const QString& tag) {
|
||||
NSData* nsTag = [tag.toNSString() dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSDictionary *deleteQuery = @{ (id)kSecClass: (id)kSecClassKey,
|
||||
(id)kSecAttrApplicationTag: nsTag,
|
||||
};
|
||||
|
||||
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)deleteQuery);
|
||||
if (status != errSecSuccess) {
|
||||
qDebug() << "Error deleteFromKeychain" << status;
|
||||
return false;
|
||||
} else {
|
||||
qDebug() << "OK deleteFromKeychain";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void MobileUtils::writeToKeychain(const QString& tag, const QString& value) {
|
||||
deleteFromKeychain(tag);
|
||||
|
||||
NSData* nsValue = [value.toNSString() dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData* nsTag = [tag.toNSString() dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSDictionary* addQuery = @{ (id)kSecValueData: nsValue,
|
||||
(id)kSecClass: (id)kSecClassKey,
|
||||
(id)kSecAttrApplicationTag: nsTag,
|
||||
};
|
||||
|
||||
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)addQuery, NULL);
|
||||
if (status != errSecSuccess) {
|
||||
qDebug() << "Error writeToKeychain" << status;
|
||||
} else {
|
||||
qDebug() << "OK writeToKeychain";
|
||||
}
|
||||
}
|
||||
|
||||
QString MobileUtils::readFromKeychain(const QString& tag) {
|
||||
NSData* nsValue = NULL;
|
||||
NSData* nsTag = [tag.toNSString() dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSDictionary *getQuery = @{ (id)kSecReturnData: @YES,
|
||||
(id)kSecClass: (id)kSecClassKey,
|
||||
(id)kSecAttrApplicationTag: nsTag,
|
||||
};
|
||||
|
||||
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)getQuery,
|
||||
(CFTypeRef *)&nsValue);
|
||||
if (status != errSecSuccess) {
|
||||
qDebug() << "Error readFromKeychain" << status;
|
||||
} else {
|
||||
qDebug() << "OK readFromKeychain" << nsValue;
|
||||
}
|
||||
|
||||
QString result;
|
||||
if (nsValue) {
|
||||
result = QByteArray::fromNSData(nsValue);
|
||||
CFRelease(nsValue);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue