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
import CommonCrypto | |
// openssl enc -d -aes-128-cbc -pass pass:hey -base64 -in enc.txt -p | |
extension Data { | |
var hexString: String { | |
return map { String(format: "%02hhx", $0) }.joined() | |
} | |
var md5Data: Data { |
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
extension String { | |
func attributed(attributes: [NSAttributedString.Key: Any]? = nil, font: UIFont? = nil, color: UIColor? = nil, base: CGFloat? = nil, lineHeight: CGFloat? = nil) -> NSAttributedString { | |
var atb = attributes ?? [:] | |
if let v = base { atb[.baselineOffset] = v } | |
if let v = font { atb[.font] = v } | |
if let v = color { atb[.foregroundColor] = v } | |
if let v = lineHeight { | |
let ps = NSMutableParagraphStyle() | |
ps.lineBreakMode = .byTruncatingTail |
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
typealias ObjDecoderConverter = ((_ path: [CodingKey], _ container: [String: Any]) -> Any?) | |
class ObjDecoder: Decoder { | |
var converter: ObjDecoderConverter? | |
var useThrow = false | |
var codingPath: [CodingKey] = [] | |
var userInfo: [CodingUserInfoKey: Any] = [:] | |
var container: Any = NSNull() |
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
static let fixkey = Data(bytes: [171, 221, 144, 111, 79, 19, 231, 93, 101, 8, 84, 181, 85, 164, 105, 161]) // 16bytes | |
static func encrypt(_ value: String) -> Data? { | |
guard let src = value.data(using: .utf8) else { return nil } | |
var dst = Data(count: kCCBlockSizeAES128 + src.count) | |
let dstlen = dst.count | |
var numBytes: size_t = 0 | |
let cryptStatus = dst.withUnsafeMutableBytes { dstBytes in | |
src.withUnsafeBytes { srcBytes in |
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
extension Dictionary { | |
init(json: String) { | |
self = [:] | |
guard let a = (try? JSONSerialization.jsonObject(with: json.dataValue, options: [])) as? Dictionary else { return } | |
self = a | |
} | |
func json() -> String { | |
guard let d = try? JSONSerialization.data(withJSONObject: self, options: []) else { return "{}" } | |
return String(data: d) |
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
func refrectedString(_ iobj: Any?, indent: String = "") -> String { | |
guard let obj = iobj else { return "null" } | |
let m = Mirror(reflecting: obj) | |
if m.displayStyle == nil { | |
if let s = obj as? String { return "\"\(s)\"" } | |
return "\(obj)" | |
} |
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
extension NSDictionary { | |
func lastof(path: String) -> Any? { | |
var r: NSDictionary? = self | |
let ar = path.components(separatedBy: "/") | |
for a in ar.dropLast() { | |
if a.isEmpty { continue } | |
r = r?[a] as? NSDictionary | |
if r == nil { return nil } | |
} |
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
public extension Bundle { | |
func infoStringForKey(_ key: String) -> String { | |
return object(forInfoDictionaryKey: key) as? String ?? "" | |
} | |
var version: String { return infoStringForKey("CFBundleShortVersionString") } | |
var build: String { return infoStringForKey("CFBundleVersion") } | |
var displayName: String { return infoStringForKey("CFBundleDisplayName") } | |
var identifier: String { return infoStringForKey("CFBundleIdentifier") } | |
} |
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
public extension Date { | |
static func dataformatter() -> DateFormatter { | |
let f = DateFormatter() | |
f.locale = Locale(identifier: "en_US_POSIX") | |
f.timeZone = TimeZone.autoupdatingCurrent | |
f.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" // ISO8601 | |
return f | |
} |
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
public extension Data { | |
var JSONObject: NSObject { | |
return (try? JSONSerialization.jsonObject(with: self, options: .allowFragments)) as? NSObject ?? NSObject() | |
} | |
var XMLObject: Any { | |
let x = XMLParser() | |
return x.parse(self) | |
} |
NewerOlder