feat: add TOTPGenerator (RFC 6238)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import XCTest
|
||||
@testable import MyPassCore
|
||||
|
||||
final class TOTPGeneratorTests: XCTestCase {
|
||||
// RFC 6238 Section 8 test vectors for SHA-1
|
||||
// secret = "12345678901234567890" (ASCII), base32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
|
||||
let sha1Secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"
|
||||
|
||||
func test_sha1_at59s() {
|
||||
let config = TOTPConfig(secret: sha1Secret, period: 30, digits: 8, algorithm: .sha1)
|
||||
let date = Date(timeIntervalSince1970: 59)
|
||||
XCTAssertEqual(TOTPGenerator.generate(config: config, at: date), "94287082")
|
||||
}
|
||||
|
||||
func test_sha1_at1111111109s() {
|
||||
let config = TOTPConfig(secret: sha1Secret, period: 30, digits: 8, algorithm: .sha1)
|
||||
let date = Date(timeIntervalSince1970: 1111111109)
|
||||
XCTAssertEqual(TOTPGenerator.generate(config: config, at: date), "07081804")
|
||||
}
|
||||
|
||||
func test_secondsRemaining_isWithinPeriod() {
|
||||
let config = TOTPConfig(secret: sha1Secret, period: 30)
|
||||
let remaining = TOTPGenerator.secondsRemaining(config: config, at: Date())
|
||||
XCTAssertGreaterThan(remaining, 0)
|
||||
XCTAssertLessThanOrEqual(remaining, 30)
|
||||
}
|
||||
|
||||
func test_generate_defaultSixDigits() {
|
||||
let config = TOTPConfig(secret: sha1Secret)
|
||||
let code = TOTPGenerator.generate(config: config, at: Date())
|
||||
XCTAssertEqual(code.count, 6)
|
||||
XCTAssertNotNil(Int(code))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user