-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data+Extensions.swift
98 lines (84 loc) · 3 KB
/
Data+Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Data+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import Foundation
#if canImport(CryptoKit)
import CryptoKit
#endif
extension Data {
/// `data.oy_decode()`
public func oy_decode<T: Decodable>(decoder: JSONDecoder = JSONDecoder()) throws -> T {
try decoder.decode(T.self, from: self)
}
/// `data.oy_decode(with: nil)`
public func oy_decode<T: Decodable>(with decoder: JSONDecoder?) -> T? {
try? (decoder ?? JSONDecoder()).decode(T.self, from: self)
}
/// `data.oy_string()`
public func oy_string(encoding: String.Encoding = .utf8) -> String? {
String(data: self, encoding: encoding)
}
/// `data.oy_json()`
public func oy_json(options: JSONSerialization.ReadingOptions = []) throws -> Any {
try JSONSerialization.jsonObject(with: self, options: options)
}
/// `data.oy_dictionary()`
public func oy_dictionary(options: JSONSerialization.ReadingOptions = []) throws -> [String: Any]? {
let data = try JSONSerialization.jsonObject(with: self, options: options) as? [String: Any]
return data
}
/// `data.oy_bytes` → output → [100, 97, 116, 97]
public var oy_bytes: [UInt8] {
Array(self)
}
/// `data.oy_hexString` → output → "56616c7565"
public var oy_hexString: String {
return withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> String in
let buffer = bytes.bindMemory(to: UInt8.self)
return buffer.map { String(format: "%02hhx", $0) }.reduce("", { $0 + $1 })
}
}
/// `imageData.oy_mimeType` → output → "image/png"
public var oy_mimeType: String? {
var values = [UInt8](repeating: 0, count: 1)
copyBytes(to: &values, count: 1)
switch values[0] {
case 0xFF:
return "image/jpeg"
case 0x89:
return "image/png"
case 0x47:
return "image/gif"
case 0x49, 0x4D:
return "image/tiff"
default:
return nil
}
}
/// `imageData.oy_base64EncodedString`
public var oy_base64EncodedString: String {
base64EncodedString()
}
/// Encrypts Data with the given key using AES.GCM and returns the encrypted data
/// `let key = SymmetricKey(size: .bits256)`
/// `try? data?.oy_encrypt(key: key)`
@available(iOS 13.0, *) public func oy_encrypt(key: SymmetricKey) throws -> Data? {
try AES.GCM.seal(self, using: key).combined
}
/// Decrypts encrypted Data with the given key using AES.GCM and returns the decrypted Data
/// `let key = SymmetricKey(size: .bits256)`
/// `try? data?.oy_decrypt(key: key)`
@available(iOS 13.0, *) public func oy_decrypt(key: SymmetricKey) throws -> Data {
let sealedBox = try AES.GCM.SealedBox(combined: self)
return try AES.GCM.open(sealedBox, using: key)
}
}
extension String {
/// Convert base64 string to Data
public var oy_base64EncodedData: Data? {
Data(base64Encoded: self)
}
}