-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from gaetanomatonti/release/0.2.0
Release 0.2.0
- Loading branch information
Showing
19 changed files
with
756 additions
and
224 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Uno | ||
// | ||
// Created by Gaetano Matonti on 06/09/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Array where Element == URLQueryItem { | ||
/// Accesses the value of the `URLQueryItem` for the specified `URIParser.ItemKey`. | ||
/// - Parameter key: The `URIParser.ItemKey` representing the name of the `URLQueryItem`. | ||
/// - Returns: An optional `String` representing the value of the query item. `nil` if a value couldn't be found for the specified key. | ||
subscript(_ key: URIParser.ItemKey) -> String? { | ||
value(for: key) | ||
} | ||
|
||
/// Gets the value of a `URLQueryItem` from its key. | ||
/// - Parameter key: The `ItemKey` representing the name of the query item. | ||
/// - Returns: An optional `String` representing the value of the query item. `nil` if a value couldn't be found for the specified key. | ||
func value(for key: URIParser.ItemKey) -> String? { | ||
first { | ||
$0.name == key.rawValue | ||
}?.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Uno | ||
// | ||
// Created by Gaetano Matonti on 28/08/21. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A protocol the represents the requirements for a one-time password generator. | ||
public protocol AuthenticationCodeGenerator { | ||
/// The secret to seed into the generator. | ||
var secret: OneTimePassword.Secret { get } | ||
|
||
/// The amount of digits composing the authentication code. | ||
var codeLength: OneTimePassword.Length { get } | ||
|
||
/// The hash function used to generate the authentication code's hash. | ||
var algorithm: OneTimePassword.Algorithm { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// Uno | ||
// | ||
// Created by Gaetano Matonti on 29/08/21. | ||
// | ||
|
||
#if canImport(CryptoKit) | ||
import CryptoKit | ||
#else | ||
import Crypto | ||
#endif | ||
|
||
import Foundation | ||
|
||
public extension OneTimePassword { | ||
/// The hash function used to generate the HMAC. | ||
enum Algorithm: String { | ||
/// The SHA1 hash function. This is the most frequently used albeit insecure. | ||
case sha1 = "SHA1" | ||
|
||
/// The SHA256 hash function. | ||
case sha256 = "SHA256" | ||
|
||
/// The SHA512 hash function. | ||
case sha512 = "SHA512" | ||
|
||
// MARK: - Computed Properties | ||
|
||
/// The minimum size of the symmetric key in bytes. | ||
var minimumKeySize: Int { | ||
switch self { | ||
case .sha1: | ||
return 20 | ||
|
||
case .sha256: | ||
return 32 | ||
|
||
case .sha512: | ||
return 64 | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
extension OneTimePassword.Algorithm { | ||
/// Gets the `Algorithm` from its name. | ||
/// | ||
/// - Note: If no algorithm could be found for the specified `String`, the default SHA1 algorithm will be used. | ||
/// - Parameter value: The `String` value of the algorithm's name. | ||
/// - Returns: A `Algorithm` used to generate the OTP. | ||
static func from(_ value: String) -> OneTimePassword.Algorithm { | ||
OneTimePassword.Algorithm(rawValue: value) ?? .sha1 | ||
} | ||
} | ||
|
||
// MARK: - Errors | ||
|
||
public extension OneTimePassword.Algorithm { | ||
/// The possible errors regarding the hash functions. | ||
enum Error: Swift.Error, LocalizedError { | ||
/// The minimum size of the symmetric key does not match the requirement. | ||
case invalidMinimumKeySize | ||
|
||
public var errorDescription: String? { | ||
switch self { | ||
case .invalidMinimumKeySize: | ||
return "The minimum size of the symmetric key does not match the requirement." | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.