Skip to content

Instantly share code, notes, and snippets.

@tsif
tsif / gist:00905439048e01b67625778fb0abd144
Last active June 23, 2021 13:12
SwiftUI preview landscape view modifier
struct LandscapeModifier: ViewModifier {
func body(content: Content) -> some View {
content
.previewLayout(.fixed(width: 812, height: 375))
.environment(\.horizontalSizeClass, .compact)
.environment(\.verticalSizeClass, .compact)
}
}
extension View {
let salt = "Salt".data(using: .utf8)!
let firstPrivateKey = P256.KeyAgreement.PrivateKey()
let firstPublicKey = firstPrivateKey.publicKey
let secondPrivateKey = P256.KeyAgreement.PrivateKey()
let secondPublicKey = secondPrivateKey.publicKey
let firstSharedSecret = try! firstPrivateKey.sharedSecretFromKeyAgreement(with: secondPublicKey)
let firstSymmetricKey = firstSharedSecret.hkdfDerivedSymmetricKey(using: SHA256.self,
let path = Bundle.main.path(forResource: "video", ofType: "mp4")!
let data = FileManager.default.contents(atPath: path)!
let key = SymmetricKey(size: .bits256) // key shared by both parties
let authentication = HMAC<SHA256>.authenticationCode(for: data,
using: key)
let authenticationData = Data(authentication)
// ~~~~~~ travel accross the network ~~~~~~~
if (HMAC<SHA256>.isValidAuthenticationCode(authenticationData,
let privateKey = Curve25519.Signing.PrivateKey()
let publicKey = privateKey.publicKey // publicize freely
let publicKeyData = publicKey.rawRepresentation
let signingPublicKey = try! Curve25519.Signing.PublicKey(rawRepresentation: publicKeyData)
// sign some data with your private key
let data = "All your base are belong to us".data(using: .utf8)!
let signature = try! privateKey.signature(for: data)
if signingPublicKey.isValidSignature(signature, for: data) {
let key = SymmetricKey(size: .bits256)
let path = Bundle.main.path(forResource: "video", ofType: "mp4")!
let data = FileManager.default.contents(atPath: path)!
// To encrypt
let encryptedData = try! ChaChaPoly.seal(data, using: key)
// To decrypt
let sealedBox = try! ChaChaPoly.SealedBox(combined: encryptedData)
var hasher = SHA256()
let path = Bundle.main.path(forResource: "video", ofType: "mp4")!
let stream = InputStream(fileAtPath: path)!
stream.open()
let bufferSize = 512
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
while stream.hasBytesAvailable {
let read = stream.read(buffer, maxLength: bufferSize)
import CryptoKit
import Foundation
let path = Bundle.main.path(forResource: "video", ofType: "mp4")!
let data = FileManager.default.contents(atPath: path)!
let digest = SHA256.hash(data: data)
print(digest)
import Foundation
import UIKit
@objc class CollectionViewLayout: UICollectionViewLayout {
var contentSize: CGSize = .zero
var cachedAttributes = [IndexPath: UICollectionViewLayoutAttributes]()
override func prepare() {
@tsif
tsif / iTunesTrackList.swift
Last active April 4, 2018 12:01
Search for a list of itunes tracks and put the results into a collection of Codables
//: Playground - noun: a place where people can play
import UIKit
import Foundation
import PlaygroundSupport
// so we can run asynchronous code in a playground
PlaygroundPage.current.needsIndefiniteExecution = true
let iso8601Full: DateFormatter =
@tsif
tsif / TableViewcontroller.swift
Last active July 13, 2017 07:39
Boilerplate for Swift UITableView
/*
* TableViewcontroller.swift
*/
import UIKit
class TableViewcontroller: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK: - PROPERTIES