Sesame-iOS/Sesame/SymmetricKey+Extensions.swift

49 lines
1.0 KiB
Swift
Raw Permalink Normal View History

2022-01-29 18:59:42 +01:00
import Foundation
import CryptoKit
extension SymmetricKey {
var data: Data {
withUnsafeBytes { Data(Array($0)) }
}
var base64: String {
data.base64EncodedString()
}
var displayString: String {
data.hexEncoded.uppercased().split(by: 4).joined(separator: " ")
}
2022-01-29 18:59:42 +01:00
var codeString: String {
" {" +
withUnsafeBytes {
return Data(Array($0))
}.map(String.init).joined(separator: ", ") +
"},"
}
}
extension SHA256.Digest {
var hexEncoded: String {
Data(map { $0 }).hexEncoded
}
}
extension String {
func split(by length: Int) -> [String] {
var startIndex = self.startIndex
var results = [Substring]()
while startIndex < self.endIndex {
let endIndex = self.index(startIndex, offsetBy: length, limitedBy: self.endIndex) ?? self.endIndex
results.append(self[startIndex..<endIndex])
startIndex = endIndex
}
return results.map { String($0) }
}
}