Coder Social home page Coder Social logo

chenxing640 / dyfkeychain Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 133 KB

This library is used to store text and data in Keychain securely for iOS, OS X, tvOS and watchOS.

License: Other

Objective-C 93.27% Ruby 6.73%
ios objective-c objc security security-framework keychain keychain-wrapper keychain-access keychain-api keychain-services keychain-management keychainwrapper keychain-sharing keychain-synchronization icloud cocoapods

dyfkeychain's Introduction

中文版 | English Vision

DYFKeychain

在钥匙串中存储文本和数据(Swift Version)。你可能已经注意到苹果的 Keychain API 有点冗长。此库旨在提供较短的语法完成简单任务:读取/写入指定键的文本值:

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
[keychain add:@"User Account Passcode" forKey:@"kUserAccPasscode"];
NSString *p = [keychain get:@"kUserAccPasscode"];

此 Keychain 库包括以下功能:

  • 获取、设置和删除字符串、布尔值和数据的钥匙串项。
  • 指定项访问安全级别。
  • 通过 iCloud 同步项。
  • 与其他应用程序共享钥匙串项。

License MIT  CocoaPods Version  CocoaPods Platform 

QQ群 (ID:614799921)

安装

使用 CocoaPods:

pod 'DYFKeychain'

Or

pod 'DYFKeychain', '~> 1.2.0'

或者从 Keychain 目录手动添加文件。

什么是 Keychain?

Keychain 是一种安全的存储方式。你可以在其中存储所有类型的敏感数据:用户密码、信用卡号码、秘密令牌等。一旦存储在 Keychain 中,这些信息只对你的应用可用,其他应用看不到。除此之外,操作系统确保这些信息被安全地保存和处理。例如,存储在 Keychain 中的文本不能从 iPhone 备份或其文件系统中提取。苹果建议只在 Keychain 中存储少量数据。如果你需要保护一些大的东西,你可以手动加密它,保存到一个文件,并将密钥存储在 Keychain 中。

使用

#import "DYFKeychain.h" 添加到源代码中。

写入/读取字符串

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
[keychain add:@"User Account Passcode" forKey:@"kUserAccPasscode"];
NSString *s = [keychain get:@"kUserAccPasscode"];

写入/读取字节序列

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
[keychain addData:data forKey:@"kCommSecureCode"];
NSData *data = [keychain getData:@"kCommSecureCode"];

写入/读取布尔值

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
[keychain addBool:YES forKey:@"kFirstInstalledAndLaunched"];
BOOL ret = [keychain getBool:@"kFirstInstalledAndLaunched"];

从钥匙串移除数据

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
[keychain delete:@"kFirstInstalledAndLaunched"]; // Remove single key.
[keychain clear]; // Delete everything from app's Keychain. Does not work on macOS.

高级选项

钥匙串项访问选项

使用 options 参数指定 Keychain 存储的安全级别。默认情况下使用 DYFKeychainAccessOptionsAccessibleWhenUnlocked 选项。它是限制性最强的选项之一,可以提供良好的数据保护。

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
[keychain add:@"xxx" forKey:@"Key1" options:DYFKeychainAccessOptionsAccessibleWhenUnlocked];

如果需要应用程序在后台访问钥匙串项,则可以使用 DYFKeychainAccessOptionsAccessibleAfterFirstUnlock。请注意,它比 DYFKeychainAccessOptionsAccessibleAfterFirstUnlock 选项更不安全。

查看所有可用的 访问选项 列表。

将钥匙串项与其他设备同步

synchronizable 属性设置为 true 可在用户的多个设备之间启用钥匙串项同步。同步将适用于在其设备上的 iCloud 设置中启用了 Keychain 的用户。

synchronizable 属性设置为 true 将使用 add 方法将该项添加到其他设备,并使用 get 命令获取可同步的项。删除可同步项将从所有设备中删除。

请注意,您不需要在应用程序的目标中启用 iCloud 或 Keychain 共享功能,使此功能工作。

// The first device
DYFKeychain *keychain = [DYFKeychain defaultKeychain];
keychain.synchronizable = YES;
[keychain add:@"See you tomorrow!" forKey:@"key12"];

// The second device
DYFKeychain *keychain = [DYFKeychain defaultKeychain];
keychain.synchronizable = YES;
[keychain get:@"key12"];

我们无法在 macOS 上进行钥匙串同步工作。

与其他应用程序共享钥匙串项

为了在同一设备上的应用程序之间共享钥匙串项,它们需要在Capabilities > Keychain Sharing设置中注册通用Keychain Groups

使用 accessGroup 属性访问共享钥匙串项。在下面的示例中,我们指定一个访问组 9ZU3R2F3D4.com.omg.myapp.KeychainGroup,它将用于设置、获取和删除 key1 项。

DYFKeychain *keychain = [DYFKeychain defaultKeychain];
keychain.accessGroup = @"9ZU3R2F3D4.com.omg.myapp.KeychainGroup" // Use your own access group.

[keychain add:@"hello world!" forKey:@"key12"];
[keychain get:@"key12"];
[keychain delete:@"key12"];
[keychain clear];

:watchOS 2.0与其配对设备之间无法共享钥匙串项:https://forums.developer.apple.com/thread/5938

检查操作是否成功

通过检查 add, deleteclear 方法的返回值,可以验证它们是否成功完成。这些方法成功时返回 true,出错时返回 false

DYFKeychain *keychain = [DYFKeychain defaultKeychain];

if ([keychain add:@"xxx" forKey:@"key1"]) {
  // Keychain item is saved successfully
} else {
  // Report error
}

若要获取特定的失败原因,请使用包含上一个操作的结果代码的 osStatus 属性。请参见 Keychain Result Codes

DYFKeychain *keychain = [DYFKeychain defaultKeychain];

[keychain add:@"xxx" forKey:@"key1"];
if (keychain.osStatus != errSecSuccess) { /* Report error */ }

数据引用返回

使用 asReference: true 参数将数据作为引用返回,这是 NEVPNProtocol 所需的。

DYFKeychain *keychain = [DYFKeychain defaultKeychain];

[keychain addData:data forKey:@"key1"];
[keychain getData:@"key1" asReference:YES];

演示

DYFKeychain 在此 演示 下学习如何使用。

欢迎反馈

如果您发现任何问题,请创建问题。我很乐意帮助你。

dyfkeychain's People

Contributors

itenfay avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rbqren000

dyfkeychain's Issues

Please enable Stack Smashing protection to avoid component exposure to memory corruption attacks

We have identified a security issue in your package from our security scan.
Stack smashing protection has not been implemented in components included in the application. When an application is compiled with stack smashing protection, a known value or "canary" is placed on the stack directly before the local variables to protect the saved base pointer, saved instruction pointer, and function arguments. The value of the canary is verified upon the function return to see if it has been overwritten. The compiler uses a heuristic to intelligently apply stack protection to a function, typically functions using character arrays. This is a very simple best practice that hardens your app with little to no downside. Memory corruption vulnerabilities can be very hard to track down, but can be extremely severe.

iOS:
In XCode, under the Build Settings for the app, go to the "Other C Flags" section and add in -fstack-protector-all. More reading can be done on Apple's developer library

Android:
Make sure that the -fstack-protector-all, -fpic, and -fstack-protector-strong flags are all set in the build.gradle file (typically in the cmake/cppFlags).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.