Skip to content

Instantly share code, notes, and snippets.

@tsif
tsif / swift.4.0.strings.swift
Last active June 21, 2017 11:11
Swift 4 & String
// collections
let filtered = string.filter { $0 != "🙀" }
// substrings
let substring = string[index...]
// multi line literals
@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
@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 =
import Foundation
import UIKit
@objc class CollectionViewLayout: UICollectionViewLayout {
var contentSize: CGSize = .zero
var cachedAttributes = [IndexPath: UICollectionViewLayoutAttributes]()
override func prepare() {
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)
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)
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)
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 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 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,