Path: blob/a-new-beginning/Folium-iOS/Extensions/String.swift
2 views
//
// String.swift
// Folium-iOS
//
// Created by Jarrod Norwell on 12/7/2025.
//
import CryptoKit
import Foundation
extension String {
static func localized(for key: LocalizedStringResource) -> String { NSLocalizedString(key.key, comment: "") }
static func nonce(with length: Int = 32) -> String {
precondition(length > 0)
let bytes = [UInt8](repeating: 0, count: length)
let charset: [Character] = .init("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
let nonce = bytes.map { charset[Int($0) % charset.count] }
return .init(nonce)
}
static func sha256(from input: String) -> String {
let data = SHA256.hash(data: Data(input.utf8))
let hash = data.compactMap { .init(format: "%02x", $0) }.joined()
return hash
}
}