mirror of
https://github.com/grishka/NearDrop.git
synced 2026-04-03 09:46:19 +02:00
32 lines
618 B
Swift
32 lines
618 B
Swift
//
|
|
// Data+URLSafeBase64.swift
|
|
// NearDrop
|
|
//
|
|
// Created by Grishka on 08.04.2023.
|
|
//
|
|
|
|
import Foundation
|
|
import CoreFoundation
|
|
|
|
extension Data{
|
|
func urlSafeBase64EncodedString() -> String {
|
|
return String(base64EncodedString().replacingOccurrences(of: "=", with: "").map {
|
|
if $0=="/"{
|
|
return "_"
|
|
} else if $0=="+" {
|
|
return "-"
|
|
} else {
|
|
return $0
|
|
}
|
|
})
|
|
}
|
|
|
|
static func randomData(length: Int) -> Data{
|
|
var data=Data(count: length)
|
|
data.withUnsafeMutableBytes {
|
|
guard 0 == SecRandomCopyBytes(kSecRandomDefault, length, $0.baseAddress!) else { fatalError() }
|
|
}
|
|
return data
|
|
}
|
|
}
|