<commit_before>//
//  OTPToken.swift
//  OneTimePassword
//
//  Copyright (c) 2013-2017 <NAME> and the OneTimePassword authors
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.
//

import Foundation
import OneTimePassword

/// `OTPToken` is a mutable, Objective-C-compatible wrapper around `OneTimePassword.Token`. For more
/// information about its properties and methods, consult the underlying `OneTimePassword`
/// documentation.
public final class OTPToken: NSObject {
    required public override init() {}

    @objc public var name: String = OTPToken.defaultName
    @objc public var issuer: String = OTPToken.defaultIssuer
    @objc public var type: OTPTokenType = .timer
    @objc public var secret: Data = Data()
    @objc public var algorithm: OTPAlgorithm = OTPToken.defaultAlgorithm
    @objc public var digits: UInt = OTPToken.defaultDigits
    @objc public var period: TimeInterval = OTPToken.defaultPeriod
    @objc public var counter: UInt64 = OTPToken.defaultInitialCounter

    private static let defaultName: String = ""
    private static let defaultIssuer: String = ""
    private static let defaultAlgorithm: OTPAlgorithm = .sha1
    private static var defaultDigits: UInt = 6
    private static var defaultInitialCounter: UInt64 = 0
    private static var defaultPeriod: TimeInterval = 30

    private func update(with token: Token) {
        self.name = token.name
        self.issuer = token.issuer

        self.secret = token.generator.secret
        self.algorithm = OTPAlgorithm(token.generator.algorithm)
        self.digits = UInt(token.generator.digits)

        switch token.generator.factor {
        case let .counter(counter):
            self.type = .counter
            self.counter = counter
        case let .timer(period):
            self.type = .timer
            self.period = period
        }
    }

    fileprivate convenience init(token: Token) {
        self.init()
        update(with: token)
    }

    @objc
    public func validate() -> Bool {
        return (tokenForOTPToken(self) != nil)
    }
}

public extension OTPToken {
    @objc(tokenWithURL:)
    static func token(from url: URL) -> Self? {
        return token(from: url, secret: nil)
    }

    @objc(tokenWithURL:secret:)
    static func token(from url: URL, secret: Data?) -> Self? {
        guard let token = Token(url: url, secret: secret) else {
            return nil
        }
        return self.init(token: token)
    }

    @objc
    func url() -> URL? {<commit_msg>Replace fileprivate with private where possible<commit_after>//
//  OTPToken.swift
//  OneTimePassword
//
//  Copyright (c) 2013-2017 <NAME> and the OneTimePassword authors
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.
//

import Foundation
import OneTimePassword

/// `OTPToken` is a mutable, Objective-C-compatible wrapper around `OneTimePassword.Token`. For more
/// information about its properties and methods, consult the underlying `OneTimePassword`
/// documentation.
public final class OTPToken: NSObject {
    required public override init() {}

    @objc public var name: String = OTPToken.defaultName
    @objc public var issuer: String = OTPToken.defaultIssuer
    @objc public var type: OTPTokenType = .timer
    @objc public var secret: Data = Data()
    @objc public var algorithm: OTPAlgorithm = OTPToken.defaultAlgorithm
    @objc public var digits: UInt = OTPToken.defaultDigits
    @objc public var period: TimeInterval = OTPToken.defaultPeriod
    @objc public var counter: UInt64 = OTPToken.defaultInitialCounter

    private static let defaultName: String = ""
    private static let defaultIssuer: String = ""
    private static let defaultAlgorithm: OTPAlgorithm = .sha1
    private static var defaultDigits: UInt = 6
    private static var defaultInitialCounter: UInt64 = 0
    private static var defaultPeriod: TimeInterval = 30

    private func update(with token: Token) {
        self.name = token.name
        self.issuer = token.issuer

        self.secret = token.generator.secret
        self.algorithm = OTPAlgorithm(token.generator.algorithm)
        self.digits = UInt(token.generator.digits)

        switch token.generator.factor {
        case let .counter(counter):
            self.type = .counter
            self.counter = counter
        case let .timer(period):
            self.type = .timer
            self.period = period
        }
    }

    private convenience init(token: Token) {
        self.init()
        update(with: token)
    }

    @objc
    public func validate() -> Bool {
        return (tokenForOTPToken(self) != nil)
    }
}

public extension OTPToken {
    @objc(tokenWithURL:)
    static func token(from url: URL) -> Self? {
        return token(from: url, secret: nil)
    }

    @objc(tokenWithURL:secret:)
    static func token(from url: URL, secret: Data?) -> Self? {
        guard let token = Token(url: url, secret: secret) else {
            return nil
        }
        return self.init(token: token)
    }

    @objc
    func url() -> URL? {
{'fraction_non_alphanumeric': 0.08316887510572314, 'fraction_numerical': 0.004792782633211165, 'mean_word_length': 2.9548494983277593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7545387', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1881, 'n_words': 757}