mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:45:20 +00:00
23 lines
704 B
Swift
23 lines
704 B
Swift
import Foundation
|
|
|
|
enum Base64URLCoding {
|
|
static func encode(_ data: Data) -> String {
|
|
data.base64EncodedString()
|
|
.replacingOccurrences(of: "+", with: "-")
|
|
.replacingOccurrences(of: "/", with: "_")
|
|
.replacingOccurrences(of: "=", with: "")
|
|
}
|
|
|
|
static func decode(_ string: String) -> Data? {
|
|
var base64 = string
|
|
let padding = (4 - (base64.count % 4)) % 4
|
|
if padding > 0 {
|
|
base64 += String(repeating: "=", count: padding)
|
|
}
|
|
base64 = base64
|
|
.replacingOccurrences(of: "-", with: "+")
|
|
.replacingOccurrences(of: "_", with: "/")
|
|
return Data(base64Encoded: base64)
|
|
}
|
|
}
|