// // 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 } } }