123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import Foundation
- import UIKit
- import CommonCrypto
- extension String {
- var alphanumeric: String {
- return self.components(separatedBy: CharacterSet.alphanumerics.inverted).joined().lowercased()
- }
- public var uppercaseInitials: String? {
- let initials = self.components(separatedBy: .whitespaces)
- .reduce("", {
- guard $0.count < 2, let nextLetter = $1.first else { return $0 }
- return $0 + nextLetter.uppercased()
- })
- return initials.isEmpty ? nil : initials
- }
- func formatSecondsToString(_ seconds: TimeInterval) -> String {
- if seconds.isNaN {
- return "00:00:00"
- }
- let sec = Int(seconds.truncatingRemainder(dividingBy: 60))
- let min = Int(seconds.truncatingRemainder(dividingBy: 3600) / 60)
- let hour = Int(seconds / 3600)
- return String(format: "%02d:%02d:%02d", hour, min, sec)
- }
- func md5() -> String {
-
- let length = Int(CC_MD5_DIGEST_LENGTH)
- let messageData = self.data(using: .utf8) ?? Data()
- var digestData = Data(count: length)
- _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
- messageData.withUnsafeBytes { messageBytes -> UInt8 in
- if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
- let messageLength = CC_LONG(messageData.count)
- CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
- }
- return 0
- }
- }
- return digestData.map { String(format: "%02hhx", $0) }.joined()
- }
- }
|