punchnet-macos/punchnet/Core/SDLUtil.swift
2026-03-19 20:13:50 +08:00

65 lines
1.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// SDLUtil.swift
// punchnet
//
// Created by on 2026/3/9.
//
import Foundation
import CommonCrypto
struct SDLUtil {
enum ContactType {
case phone
case email
case invalid
}
static func hmacMD5(key: String, data: String) -> String {
let keyData = key.data(using: .utf8)!
let dataData = data.data(using: .utf8)!
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
keyData.withUnsafeBytes { keyBytes in
dataData.withUnsafeBytes { dataBytes in
CCHmac(
CCHmacAlgorithm(kCCHmacAlgMD5),
keyBytes.baseAddress!,
keyBytes.count,
dataBytes.baseAddress!,
dataBytes.count,
&digest
)
}
}
return digest.map { String(format: "%02x", $0) }.joined()
}
static func isValidIdentifyContact(_ input: String) -> Bool {
switch identifyContact(input) {
case .email, .phone:
true
default:
false
}
}
static func identifyContact(_ input: String) -> ContactType {
let trimmed = input.trimmingCharacters(in: .whitespacesAndNewlines)
// 1 11
let phoneRegex = /^1[3-9][0-9]{9}$/
//
let emailRegex = /^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}$/
if trimmed.wholeMatch(of: phoneRegex) != nil {
return .phone
} else if trimmed.wholeMatch(of: emailRegex) != nil {
return .email
} else {
return .invalid
}
}
}