Skip to content

Commit

Permalink
added conformances for equatable and hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
tgymnich committed Aug 23, 2020
1 parent 22634f6 commit e094f75
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/OTPKit/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import KeychainAccess

public struct Account<OTPType: OTP>: Codable, Equatable, Identifiable {
public struct Account<OTPType: OTP>: Codable, Hashable, Identifiable {
private let keychainKey: String
/// The label is used to identify which account a key is associated with
public let id = UUID()
Expand Down
14 changes: 14 additions & 0 deletions Sources/OTPKit/HOTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,19 @@ public final class HOTP: OTP {
defer { counter += 1 }
return code(for: counter)
}

// MARK: Equatable

public static func ==(lhs: HOTP, rhs: HOTP) -> Bool {
return lhs.secret == rhs.secret && lhs.algorithm == rhs.algorithm && lhs.digits == rhs.digits
}

// MARK: Hashable

public func hash(into hasher: inout Hasher) {
hasher.combine(secret)
hasher.combine(algorithm)
hasher.combine(digits)
}

}
2 changes: 1 addition & 1 deletion Sources/OTPKit/OTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import CommonCrypto

public protocol OTP: Codable {
public protocol OTP: Codable, Hashable {
/// Algorithm used to calculate the hash
var algorithm: Algorithm { get }
/// The secret is an arbitrary key value
Expand Down
17 changes: 16 additions & 1 deletion Sources/OTPKit/TOTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public final class TOTP: OTP {

self.init(algorithm: algorithm, secret: secret, digits: digits, period: period)
}

public func code() -> String {
return code(for: Date())
}
Expand All @@ -91,6 +91,21 @@ public final class TOTP: OTP {
timer.invalidate()
}
}

// MARK: Equatable

public static func ==(lhs: TOTP, rhs: TOTP) -> Bool {
return lhs.secret == rhs.secret && lhs.algorithm == rhs.algorithm && lhs.digits == rhs.digits && lhs.period == rhs.period
}

// MARK: Hashable

public func hash(into hasher: inout Hasher) {
hasher.combine(secret)
hasher.combine(algorithm)
hasher.combine(digits)
hasher.combine(period)
}

}

Expand Down

0 comments on commit e094f75

Please sign in to comment.