Skip to content

Instantly share code, notes, and snippets.

View thomsmed's full-sized avatar
:octocat:
✌️😎👌

Thomas Asheim Smedmann thomsmed

:octocat:
✌️😎👌
View GitHub Profile
@thomsmed
thomsmed / GenerateExportImportEC256.swift
Created January 19, 2024 22:10
Quick and dirty generate, export and import EC256 Key Pair (SecKey -> base64 -> SecKey).
//
// Quickly and dirty generatte EC256 Key Pair, export as base64 Data/String, and then import from base64 Data/String.
// Perfect when you need an in-memory key pair for mocks/unit testing.
//
import CryptoKit
// Generate EC256 Key Pair and export as base64 String/Data
let attributes = [
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
@thomsmed
thomsmed / URLRequest+curlRepresentation.swift
Created September 5, 2023 18:28
A simple URLRequest extension that map the request to curl representation.
import Foundation
public extension URLRequest {
var curlRepresentation: String {
guard let url = url?.absoluteString else {
return "could not create curl command"
}
var components = ["curl \(url)"]
@thomsmed
thomsmed / SecureDataStorage.swift
Last active September 4, 2023 18:49
A general storage for data required to be backed by a secure storage (e.g the device's keychain).
import Foundation
/// A general storage for data required to be backed by a secure storage.
public protocol SecureDataStorage {
func string(for key: String) throws -> String?
func set(_ string: String, for key: String) throws
func value<Value: Decodable>(for key: String) throws -> Value?
func set<Value: Encodable>(_ value: Value, for key: String) throws
func data(for key: String) throws -> Data?
func set(_ data: Data, for key: String) throws
@thomsmed
thomsmed / CryptographicKeyStorage.swift
Last active February 10, 2024 10:45
A general storage for Cryptographic Key Pairs generated on device.
import Foundation
/// A general storage for Cryptographic Key Pairs generated on device.
///
/// The generated and stored keys are protected by Secure Enclave,
/// hence only NIST P-256 elliptic curve key pairs are supported.
///
/// These keys can only be used for creating and verifying cryptographic signatures, or for elliptic curve Diffie-Hellman key exchange (and by extension, symmetric encryption).
public protocol CryptographicKeyStorageProtocol {
/// Generate a NIST P-256 elliptic curve key pair.
@thomsmed
thomsmed / DefaultHTTPClient+HTTPClient.swift
Last active September 2, 2023 19:26
A general purpose HTTPClient with support for interceptors.
//
// DefaultHTTPClient+HTTPClient.swift
//
import Foundation
/// A context for interceptors to get a hold on the ``URLSession``, ``JSONEncoder`` and ``JSONDecoder`` the associated ``HTTPClient`` uses.
///
/// These properties can be used to decode/encode or do intermediate network request before/after outgoing network request.
public struct HTTPClientContext {
@thomsmed
thomsmed / AppInfoProvider.swift
Created July 9, 2023 13:02
AppInfoProvider.swift - A basic wrapper around your app's Info.plist and more
//
// AppInfoProvider.swift
//
import Foundation
protocol AppInfoProvider: AnyObject {
var bundleDisplayName: String { get } // Your app's name.
var bundleVersion: String { get } // The current build number, e.g 137
var bundleShortVersionString: String { get } // The current app version, e.g 1.2.0.
@thomsmed
thomsmed / avoid-developer-team-pre-commit.sh
Last active June 25, 2023 10:43
avoid-developer-team-pre-commit.sh - A simple shell script that makes sure no DEVELOPMENT_TEAM is set in Xcode project files.
#!/bin/sh
#
# Find all project.pbxproj files with staged changes and make sure they do not specify any DEVELOPMENT_TEAM.
matches=$(git grep --cached -E -o 'DEVELOPMENT_TEAM = .{3,};$')
if [ ! -z "${matches}" ]; then
echo "One or more project files specify DEVELOPMENT_TEAM:"
echo "$matches"
@thomsmed
thomsmed / ScrollableContentViewController.swift
Created April 22, 2023 21:22
ScrollableContentViewController - A simple but powerful base UIViewController to automatically handle scrollable content.
//
// ScrollableContentViewController.swift
//
import UIKit
class ScrollableContentViewController: UIViewController {
private lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
@thomsmed
thomsmed / KeyboardAvoidanceViewController.swift
Last active April 22, 2023 21:18
KeyboardAvoidanceViewController - A simple but powerful base UIViewController to handle keyboard appearance/disappearance.
//
// KeyboardAvoidanceViewController.swift
//
import UIKit
class KeyboardAvoidanceViewController: UIViewController {
private lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
@thomsmed
thomsmed / ValueOrUnknown.swift
Created December 8, 2022 12:45
ValueOrUnknown.swift - A simple wrapper enum for decoding JSON with optional or unknown/shifting values.
import Foundation
/// Simple wrapper enum for decoding JSON with optional or unknown/shifting values.
///
/// If decoding of the Value type fails, the value is decoded as a string.
///
/// ```swift
/// enum State { case idle; case running; case stopped }
/// try JSONDecoder().decode(ValueOrUnknown<State>.self, from: "waiting") // == .unknown("waiting")
/// ```