75 lines
1.8 KiB
Swift
75 lines
1.8 KiB
Swift
//
|
|
// EditedKey.swift
|
|
// OTP
|
|
//
|
|
// Created by Shadowfacts on 8/22/21.
|
|
//
|
|
|
|
import Foundation
|
|
import OTPKit
|
|
|
|
struct EditedKey {
|
|
var secret: String
|
|
var period: Period
|
|
var digits: Digits
|
|
var issuer: String
|
|
var label: String
|
|
|
|
init() {
|
|
self.secret = ""
|
|
self.period = .thirty
|
|
self.digits = .six
|
|
self.issuer = ""
|
|
self.label = ""
|
|
}
|
|
|
|
init?(totpKey: TOTPKey) {
|
|
guard totpKey.period == 30 || totpKey.period == 60,
|
|
totpKey.digits == 6 || totpKey.digits == 8 else {
|
|
return nil
|
|
}
|
|
|
|
self.secret = totpKey.secret.base32EncodedString
|
|
self.period = totpKey.period == 30 ? .thirty : .sixty
|
|
self.digits = totpKey.digits == 6 ? .six : .eight
|
|
self.issuer = totpKey.issuer
|
|
self.label = totpKey.label ?? ""
|
|
}
|
|
|
|
func toTOTPKey() -> TOTPKey? {
|
|
let secretStr = self.secret.replacingOccurrences(of: " ", with: "")
|
|
guard secretStr.count > 0,
|
|
let secret = secretStr.base32DecodedData,
|
|
!issuer.isEmpty else {
|
|
return nil
|
|
}
|
|
return TOTPKey(secret: secret, period: period.value, digits: digits.value, label: label.trimmingCharacters(in: .whitespacesAndNewlines), issuer: issuer)
|
|
}
|
|
|
|
enum Period: Hashable {
|
|
case thirty, sixty
|
|
|
|
var value: Int {
|
|
switch self {
|
|
case .thirty:
|
|
return 30
|
|
case .sixty:
|
|
return 60
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Digits: Hashable {
|
|
case six, eight
|
|
|
|
var value: Int {
|
|
switch self {
|
|
case .six:
|
|
return 6
|
|
case .eight:
|
|
return 8
|
|
}
|
|
}
|
|
}
|
|
}
|