Skip to content

Instantly share code, notes, and snippets.

@iosdevzone
Created December 10, 2014 08:59
Show Gist options
  • Save iosdevzone/1c667baedec5c1e6c156 to your computer and use it in GitHub Desktop.
Save iosdevzone/1c667baedec5c1e6c156 to your computer and use it in GitHub Desktop.
/* ==== KeychainItem.swift === */
public class KeychainItem {
public var dictionary : KeychainDictionary
public init() {
self.dictionary = KeychainDictionary()
}
public init(dictionary: KeychainDictionary) {
self.dictionary = dictionary
}
public var accessGroup : String? {
get {
return dictionary.getAttribute(.AccessGroup) as? String
}
set(accessGroup) {
dictionary.setAttribute(.AccessGroup, value: accessGroup)
}
}
public var accessible : Accessible? {
get {
return Accessible(rawValue: dictionary.getAttribute(.Accessible) as String)
}
set(accessible) {
dictionary.setAttribute(.Accessible, value: accessible?.rawValue)
}
}
public var label : String? {
get {
return dictionary.getAttribute(.Label) as? String
}
set(label) {
dictionary.setAttribute(.Label, value: label)
}
}
public var itemClass : ItemClass? {
get {
return ItemClass(rawValue:dictionary.getAttribute(.ItemClass) as String)
}
set(itemClass) {
dictionary.setAttribute(.ItemClass, value: itemClass?.rawValue)
}
}
func toNativeDictionary() -> CFDictionaryRef {
return dictionary.dictionary as CFDictionaryRef
}
}
/* === KeychainDictionary.swift === */
import Foundation
public struct KeychainDictionary : Printable {
var dictionary = Dictionary<String, AnyObject>()
init() {
dictionary = Dictionary<String, AnyObject>()
}
init(dictionary: [String: AnyObject]) {
self.dictionary = dictionary
}
mutating func setKey(key: String, value: AnyObject?) {
if let v : AnyObject = value {
dictionary[key] = v
}
else {
dictionary.removeValueForKey(key)
}
}
func getValue(key:String) -> AnyObject? {
return dictionary[key]
}
mutating func setAttribute(a : Attribute, value : AnyObject?) {
setKey(a.rawValue, value:value)
}
func getAttribute(a : Attribute) -> AnyObject? {
return dictionary[a.rawValue]
}
public var description : String {
var s = "{\n"
for (k,v) in self.dictionary {
if let a = Attribute(rawValue:k) {
var r = reflect(v)
switch a {
case .Creator:
var ui : UInt32 = UInt32((v as NSNumber).unsignedIntegerValue)
var fcc = FourCharacterCodeToString(ui)
s = "\(s)\t\(a):\(v) [\(fcc)]\n"
default:
s = "\(s)\t\(a):\(v) [\(r)]\n"
}
}
else {
s = "\(s)\t\(k):\(v)\n"
}
}
s = "\(s)}"
return s
}
}
/* === Attribute.swift === */
import Foundation
import Security
// MARK: - Attribute
public enum Attribute : RawRepresentable, Printable {
case Accessible, AccessControl, AccessGroup, Synchronizable, SynchronizableAny, CreationDate, ModificationDate, Description, Comment, Creator, Type, Label, IsInvisible, IsNegative, Account, Service, Generic, SecurityDomain, Server, Protocol, AuthenticationType, Port, Path, Subject, Issuer, SerialNumber, SubjectKeyID, PublicKeyHash, CertificateType, CertificateEncoding, KeyClass, ApplicationLabel, IsPermanent, ApplicationTag, KeyType, KeySizeInBits, EffectiveKeySize, CanEncrypt, CanDecrypt, CanDerive, CanSign, CanVerify, CanWrap, CanUnwrap, ItemClass
public static let allValues: [Attribute] = [Accessible, AccessControl, AccessGroup, Synchronizable, SynchronizableAny, CreationDate, ModificationDate, Description, Comment, Creator, .Type, Label, IsInvisible, IsNegative, Account, Service, Generic, SecurityDomain, Server, .Protocol, AuthenticationType, Port, Path, Subject, Issuer, SerialNumber, SubjectKeyID, PublicKeyHash, CertificateType, CertificateEncoding, KeyClass, ApplicationLabel, IsPermanent, ApplicationTag, KeyType, KeySizeInBits, EffectiveKeySize, CanEncrypt, CanDecrypt, CanDerive, CanSign, CanVerify, CanWrap, CanUnwrap, ItemClass]
public init?(rawValue: String) {
if rawValue == String(kSecAttrAccessible) {
self = Accessible
}
else if rawValue == String(kSecAttrAccessControl) {
self = AccessControl
}
else if rawValue == String(kSecAttrAccessGroup) {
self = AccessGroup
}
else if rawValue == String(kSecAttrSynchronizable) {
self = Synchronizable
}
else if rawValue == String(kSecAttrSynchronizableAny) {
self = SynchronizableAny
}
else if rawValue == String(kSecAttrCreationDate) {
self = CreationDate
}
else if rawValue == String(kSecAttrModificationDate) {
self = ModificationDate
}
else if rawValue == String(kSecAttrDescription) {
self = Description
}
else if rawValue == String(kSecAttrComment) {
self = Comment
}
else if rawValue == String(kSecAttrCreator) {
self = Creator
}
else if rawValue == String(kSecAttrType) {
self = .Type
}
else if rawValue == String(kSecAttrLabel) {
self = Label
}
else if rawValue == String(kSecAttrIsInvisible) {
self = IsInvisible
}
else if rawValue == String(kSecAttrIsNegative) {
self = IsNegative
}
else if rawValue == String(kSecAttrAccount) {
self = Account
}
else if rawValue == String(kSecAttrService) {
self = Service
}
else if rawValue == String(kSecAttrGeneric) {
self = Generic
}
else if rawValue == String(kSecAttrSecurityDomain) {
self = SecurityDomain
}
else if rawValue == String(kSecAttrServer) {
self = Server
}
else if rawValue == String(kSecAttrProtocol) {
self = .Protocol
}
else if rawValue == String(kSecAttrAuthenticationType) {
self = AuthenticationType
}
else if rawValue == String(kSecAttrPort) {
self = Port
}
else if rawValue == String(kSecAttrPath) {
self = Path
}
else if rawValue == String(kSecAttrSubject) {
self = Subject
}
else if rawValue == String(kSecAttrIssuer) {
self = Issuer
}
else if rawValue == String(kSecAttrSerialNumber) {
self = SerialNumber
}
else if rawValue == String(kSecAttrSubjectKeyID) {
self = SubjectKeyID
}
else if rawValue == String(kSecAttrPublicKeyHash) {
self = PublicKeyHash
}
else if rawValue == String(kSecAttrCertificateType) {
self = CertificateType
}
else if rawValue == String(kSecAttrCertificateEncoding) {
self = CertificateEncoding
}
else if rawValue == String(kSecAttrKeyClass) {
self = KeyClass
}
else if rawValue == String(kSecAttrApplicationLabel) {
self = ApplicationLabel
}
else if rawValue == String(kSecAttrIsPermanent) {
self = IsPermanent
}
else if rawValue == String(kSecAttrApplicationTag) {
self = ApplicationTag
}
else if rawValue == String(kSecAttrKeyType) {
self = KeyType
}
else if rawValue == String(kSecAttrKeySizeInBits) {
self = KeySizeInBits
}
else if rawValue == String(kSecAttrEffectiveKeySize) {
self = EffectiveKeySize
}
else if rawValue == String(kSecAttrCanEncrypt) {
self = CanEncrypt
}
else if rawValue == String(kSecAttrCanDecrypt) {
self = CanDecrypt
}
else if rawValue == String(kSecAttrCanDerive) {
self = CanDerive
}
else if rawValue == String(kSecAttrCanSign) {
self = CanSign
}
else if rawValue == String(kSecAttrCanVerify) {
self = CanVerify
}
else if rawValue == String(kSecAttrCanWrap) {
self = CanWrap
}
else if rawValue == String(kSecAttrCanUnwrap) {
self = CanUnwrap
}
else if rawValue == String(kSecClass) {
self = ItemClass
}
else {
return nil
}
}
public var rawValue: String {
switch self {
case Accessible: return String(kSecAttrAccessible)
case AccessControl: return String(kSecAttrAccessControl)
case AccessGroup: return String(kSecAttrAccessGroup)
case Synchronizable: return String(kSecAttrSynchronizable)
case SynchronizableAny: return String(kSecAttrSynchronizableAny)
case CreationDate: return String(kSecAttrCreationDate)
case ModificationDate: return String(kSecAttrModificationDate)
case Description: return String(kSecAttrDescription)
case Comment: return String(kSecAttrComment)
case Creator: return String(kSecAttrCreator)
case Type: return String(kSecAttrType)
case Label: return String(kSecAttrLabel)
case IsInvisible: return String(kSecAttrIsInvisible)
case IsNegative: return String(kSecAttrIsNegative)
case Account: return String(kSecAttrAccount)
case Service: return String(kSecAttrService)
case Generic: return String(kSecAttrGeneric)
case SecurityDomain: return String(kSecAttrSecurityDomain)
case Server: return String(kSecAttrServer)
case Protocol: return String(kSecAttrProtocol)
case AuthenticationType: return String(kSecAttrAuthenticationType)
case Port: return String(kSecAttrPort)
case Path: return String(kSecAttrPath)
case Subject: return String(kSecAttrSubject)
case Issuer: return String(kSecAttrIssuer)
case SerialNumber: return String(kSecAttrSerialNumber)
case SubjectKeyID: return String(kSecAttrSubjectKeyID)
case PublicKeyHash: return String(kSecAttrPublicKeyHash)
case CertificateType: return String(kSecAttrCertificateType)
case CertificateEncoding: return String(kSecAttrCertificateEncoding)
case KeyClass: return String(kSecAttrKeyClass)
case ApplicationLabel: return String(kSecAttrApplicationLabel)
case IsPermanent: return String(kSecAttrIsPermanent)
case ApplicationTag: return String(kSecAttrApplicationTag)
case KeyType: return String(kSecAttrKeyType)
case KeySizeInBits: return String(kSecAttrKeySizeInBits)
case EffectiveKeySize: return String(kSecAttrEffectiveKeySize)
case CanEncrypt: return String(kSecAttrCanEncrypt)
case CanDecrypt: return String(kSecAttrCanDecrypt)
case CanDerive: return String(kSecAttrCanDerive)
case CanSign: return String(kSecAttrCanSign)
case CanVerify: return String(kSecAttrCanVerify)
case CanWrap: return String(kSecAttrCanWrap)
case CanUnwrap: return String(kSecAttrCanUnwrap)
case ItemClass: return String(kSecClass)
}
}
public var description : String {
switch self {
case Accessible: return "Accessible"
case AccessControl: return "AccessControl"
case AccessGroup: return "AccessGroup"
case Synchronizable: return "Synchronizable"
case SynchronizableAny: return "SynchronizableAny"
case CreationDate: return "CreationDate"
case ModificationDate: return "ModificationDate"
case Description: return "Description"
case Comment: return "Comment"
case Creator: return "Creator"
case Type: return "Type"
case Label: return "Label"
case IsInvisible: return "IsInvisible"
case IsNegative: return "IsNegative"
case Account: return "Account"
case Service: return "Service"
case Generic: return "Generic"
case SecurityDomain: return "SecurityDomain"
case Server: return "Server"
case Protocol: return "Protocol"
case AuthenticationType: return "AuthenticationType"
case Port: return "Port"
case Path: return "Path"
case Subject: return "Subject"
case Issuer: return "Issuer"
case SerialNumber: return "SerialNumber"
case SubjectKeyID: return "SubjectKeyID"
case PublicKeyHash: return "PublicKeyHash"
case CertificateType: return "CertificateType"
case CertificateEncoding: return "CertificateEncoding"
case KeyClass: return "KeyClass"
case ApplicationLabel: return "ApplicationLabel"
case IsPermanent: return "IsPermanent"
case ApplicationTag: return "ApplicationTag"
case KeyType: return "KeyType"
case KeySizeInBits: return "KeySizeInBits"
case EffectiveKeySize: return "EffectiveKeySize"
case CanEncrypt: return "CanEncrypt"
case CanDecrypt: return "CanDecrypt"
case CanDerive: return "CanDerive"
case CanSign: return "CanSign"
case CanVerify: return "CanVerify"
case CanWrap: return "CanWrap"
case CanUnwrap: return "CanUnwrap"
case ItemClass: return "ItemClass"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment