ํฐ์คํ ๋ฆฌ ๋ทฐ
์๋ ํ์ธ์?
๋๊ตฌ๋ฆฌ์ ๋๋ค.
์ ๋ฒ ํฌ์คํ ์ ์ด์ด
์ด๋ฒ์๋ keychain์ ๋ํด ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Keychain Service๋?
ํค์ฒด์ธ ์๋น์ค๋ ํค์ฒด์ธ์ด๋ผ๋ ์ํธํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ฒ ์ ์ฅํ ์ ์๋ ๋ฉ์ปค๋์ฆ์ ์ ๊ณตํฉ๋๋ค.
ํค์ฒด์ธ์ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ฑ์ ์ญ์ ํ๋๋ผ๋ ์๊ตฌ์ ์ผ๋ก ๋ฐ์ดํฐ๊ฐ ๋จ๊ฒ ๋ฉ๋๋ค.

๋น๋ฐ๋ฒํธ, cryptographic keys ๋ฑ ์ฌ์ฉ์๊ฐ ์ค์ํ๊ฒ ์๊ฐํ๋ ๋ฐ์ดํฐ๋ฅผ
์์ ํ๊ฒ ์ ์ฅํ ์ ์๋๋ก ๋์์ค๋๋ค.
Keychain Item
ํค์ฒด์ธ์ ์ ์ฅํ๋ ค๋ฉด ์ ์ฅํ ์ ์๋ ํํ๋ก ๋ฐ๊ฟ์ฃผ์ด์ผ ํ๋๋ฐ,
๊ทธ ํํ๋ฅผ Keychain Item์ด๋ผ๊ณ ํฉ๋๋ค.

์์ ๊ทธ๋ฆผ์ ๋ณด๋ฉด Data์ ํจ๊ป Attributes๋ ํจ๊ป ์ ์ฅ ๋๋๋ฐ
๋ฐ์ดํฐ์ ์ ๊ทผ์ฑ์ ๊ด๋ฆฌํ๊ณ ๊ฒ์ํ ์ ์๋๋ก ํ๋ ์ญํ ์ ํฉ๋๋ค.
ํค์ฒด์ธ ์๋น์ค๋ ๋ฐ์ดํฐ ์ํธํ์ ์ ์ฅ์ ํค์ฒด์ธ์ ์ฒ๋ฆฌํ๋ฉฐ, ์ด๋ ๋์คํฌ์ ์ ์ฅ๋ ์ํธํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋๋ค.
์ดํ ์น์ธ๋ ํ๋ก์ธ์ค๋ง์ด ํค์ฒด์ธ ์๋น์ค๋ฅผ ํตํด ํญ๋ชฉ์ ์ฐพ์ ๋ฐ์ดํฐ๋ฅผ ๋ณตํธํํ ์ ์์ต๋๋ค.
์ด๋ป๊ฒ ์ฌ์ฉํ๋๊ฐ?
๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ฌ ๋ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง ์์๋ณด๊ฒ ์ต๋๋ค.
๋ฐ๋ก ์ฌ์ฉ๋ฒ์ผ๋ก ๊ฐ๊ธฐ์ ์ ๋จผ์ ์์์ผ ํ ์ ๋ณด๊ฐ ์์ด ๋จผ์ ์์๋ณด๋๋ก ํ ๊ฒ์!
CFDictionary
keychain์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ์ ํด์ผํ๋๋ฐ
๋ฐ๋ก ์ด ํํ๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค.
kSecClass
๋ ์ ๋ฌํ ๋ฐ์ดํฐ์ ๋ํ ํ์ ์ kSecClass ํ์ ์ผ๋ก ์ ๋ฌํด ์ค ์ ์์ต๋๋ค.
- kSecClassGenericPassword: ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ์ฅํ ๋ ์ฌ์ฉํฉ๋๋ค. ์: ์ฑ ์ฌ์ฉ์ ๋น๋ฐ๋ฒํธ.
- kSecClassInternetPassword: ์ธํฐ๋ท ๋น๋ฐ๋ฒํธ, ์๋ฒ๋ ์น์ฌ์ดํธ ๋ก๊ทธ์ธ ์ ๋ณด ์ ์ฅ์ ์ฌ์ฉํฉ๋๋ค.
- kSecClassCertificate: ๋์งํธ ์ธ์ฆ์๋ฅผ ์ ์ฅํ ๋ ์ฌ์ฉํฉ๋๋ค.
- kSecClassKey: ์ํธํ ํค๋ฅผ ์ ์ฅํ ๋ ์ฌ์ฉํฉ๋๋ค.
- kSecClassIdentity: ์ธ์ฆ์์ ํค ์์ ์ ์ฅํ ๋ ์ฌ์ฉํฉ๋๋ค.
๊ทธ๋ผ ์ด์ ์ค์ ๋ก ์ฌ์ฉํ๋ ์ฝ๋์ ๋๋ค.
// ํค์ฒด์ธ์ ๋ฐ์ดํฐ ์ ์ฅ
func save(_ data: Data, forKey key: String) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecValueData as String: data
]
// ๊ธฐ์กด ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ์ญ์
SecItemDelete(query as CFDictionary)
// ์ ๋ฐ์ดํฐ ์ ์ฅ
let status = SecItemAdd(query as CFDictionary, nil)
if status != errSecSuccess {
print("Error saving data: \(status)")
}
}
// ํค์ฒด์ธ์์ ๋ฐ์ดํฐ ์ฝ๊ธฐ
func load(forKey key: String) -> Data? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]
var dataTypeRef: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
if status == errSecSuccess {
return dataTypeRef as? Data
} else {
print("Error loading data: \(status)")
return nil
}
}
// ํค์ฒด์ธ์์ ๋ฐ์ดํฐ ์
๋ฐ์ดํธ
func update(_ data: Data, forKey key: String) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key
]
let attributes: [String: Any] = [
kSecValueData as String: data
]
let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
if status == errSecItemNotFound {
// ํญ๋ชฉ์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์ ์ฅ
save(data, forKey: key)
print("Item not found, so saved new item.")
} else if status != errSecSuccess {
print("Error updating data: \(status)")
} else {
print("Data updated successfully.")
}
}
// ํค์ฒด์ธ์์ ๋ฐ์ดํฐ ์ญ์
func delete(forKey key: String) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key
]
let status = SecItemDelete(query as CFDictionary)
if status != errSecSuccess {
print("Error deleting data: \(status)")
}
}
์ฌ๊ธฐ์ ์ฃผ์ํ ์ ์ด ์๋๋ฐ
SecItemAdd ํจ์๋ ์ค๋ ๋๋ฅผ ๋ธ๋ฝ ์ํค๊ธฐ ๋๋ฌธ์
๋ฉ์ธ ์ค๋ ๋์์ ๋ค๋ฃฐ ๊ฒฝ์ฐ UI๊ฐ ๋ฉ์ถ๋ ๋ฌธ์ ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค.
๋ฐ๋ผ์ background ์ค๋ ๋๋ aync ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
๊ธ ์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
์ฐธ๊ณ : https://developer.apple.com/documentation/security/keychain-services
Keychain services | Apple Developer Documentation
Securely store small chunks of data on behalf of the user.
developer.apple.com
'iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Linker ์์๋ณด๊ธฐ (0) | 2025.01.12 |
|---|---|
| [Xcode] Xcode Build System ์ดํดํ๊ธฐ (0) | 2025.01.03 |
| [Xcode] build configuration file(xcconfig) ์์๋ณด๊ธฐ (0) | 2025.01.01 |
| [Xcode] Project, Target, Build Settings ๋ฑ ๊ฐ ๊ฐ๋ ์ ๋ฆฌ (0) | 2024.12.23 |
| [iOS] UserDefaults (0) | 2024.11.03 |
- Total
- Today
- Yesterday
- set
- func
- XCTest
- network
- RX
- ํ ์คํธ
- xcode
- ์ค์ํํธ ํ๋กํ ์ฝ
- swift ๊ณต๋ถ
- ios
- ํ๋กํผํฐ
- ์ค์ํํธ map
- RxSwift
- URLSession
- Property
- Unit Test
- Protocol
- swift urlsession
- Swift Network
- autolayout
- swift ํ๋กํ ์ฝ
- ์คํ ๋ ์ด์์
- ์ค์ํํธ ๊ณต๋ถ
- observable
- ํ๋กํ ์ฝ
- ์ค์ํํธ
- test
- swift
- Clean
- ๊ณต๋ถ
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |