Skip to content

Instantly share code, notes, and snippets.

@alskipp
Last active November 1, 2018 11:10
Show Gist options
  • Save alskipp/016c8ba96352e5c74bf2 to your computer and use it in GitHub Desktop.
Save alskipp/016c8ba96352e5c74bf2 to your computer and use it in GitHub Desktop.

Revisions

  1. alskipp revised this gist Feb 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion encrypt_xor1.swift
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ extension Character {

    func encrypt(c:Character, key:Character) -> String {
    let byte = [c.utf8() ^ key.utf8()]
    return String(bytes: byte, encoding: NSUTF8StringEncoding)!
    return String(bytes: byte, encoding: NSUTF8StringEncoding)! // forced unwrapping alert!
    }

    func encrypt(message:String, #key:String) -> String {
  2. alskipp revised this gist Feb 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion encrypt_xor2.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /*
    /* (Requires Swift 1.2)
    In the version above you'll notice that there's use of forced Optional unwrapping.
    This is potentially hazardous. Here's a version that deals with Optional values safely.
    Having to 'reduce' Optional Strings adds significant complexity.
  3. alskipp revised this gist Feb 27, 2015. 2 changed files with 52 additions and 0 deletions.
    File renamed without changes.
    52 changes: 52 additions & 0 deletions encrypt_xor2.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    /*
    In the version above you'll notice that there's use of forced Optional unwrapping.
    This is potentially hazardous. Here's a version that deals with Optional values safely.
    Having to 'reduce' Optional Strings adds significant complexity.
    Below, I've shown 2 ways of dealing with this (Monadic bind & 'if let' syntax).
    */

    import Foundation

    // Monadic bind for Optionals
    infix operator >>= {associativity left}
    func >>= <A,B> (m: A?, f: A -> B?) -> B? {
    if let x = m {return f(x)}
    return .None
    }

    extension Character {
    func utf8() -> UInt8 {
    let utf8 = String(self).utf8
    return utf8[utf8.startIndex]
    }
    }

    func encrypt(key:Character, c:Character) -> String? {
    let byte = [key.utf8() ^ c.utf8()]
    return String(bytes: byte, encoding: NSUTF8StringEncoding)
    }

    // Curried func for convenient use with map
    func encryptKey(key:String)(message:String) -> String? {
    return reduce(zip(key, message), Optional("")) { str, c in str >>= { s in encrypt(c).map {s + $0} }}
    }


    let message = "Hello world!"
    let secretKey = "(:.,?P!9@PAz" // really should be randomly generated!
    let encryptedMessage = encryptKey(secretKey)(message: message) // > .Some("`_B@PpVV2<%[")

    // As the encryptKey func is curried it can be passed directly to map
    let decryptedMessage = encryptedMessage.map(encryptKey(secretKey)) // > .Some("Hello world!")


    /* here's how the encrypt function would look like using 'if let' syntax

    func encryptKey_if_let(key:String)(message:String) -> String? {
    return reduce(zip(key, message), Optional("")) { if let str = $0, c = encrypt($1) {
    return str + c
    } else {
    return .None}
    }
    }
    */
  4. alskipp revised this gist Oct 28, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import Foundation

    extension Character {
    func utf8() -> UInt8 {
    let utf8 = String(self).utf8
  5. alskipp revised this gist Oct 28, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.swift
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@ extension Character {
    }
    }

    func encrypt(s:Character, key:Character) -> String {
    let byte = [s.utf8() ^ key.utf8()]
    func encrypt(c:Character, key:Character) -> String {
    let byte = [c.utf8() ^ key.utf8()]
    return String(bytes: byte, encoding: NSUTF8StringEncoding)!
    }

  6. alskipp revised this gist Oct 28, 2014. No changes.
  7. alskipp created this gist Oct 28, 2014.
    20 changes: 20 additions & 0 deletions gistfile1.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    extension Character {
    func utf8() -> UInt8 {
    let utf8 = String(self).utf8
    return utf8[utf8.startIndex]
    }
    }

    func encrypt(s:Character, key:Character) -> String {
    let byte = [s.utf8() ^ key.utf8()]
    return String(bytes: byte, encoding: NSUTF8StringEncoding)!
    }

    func encrypt(message:String, #key:String) -> String {
    return reduce(Zip2(message, key), "") { $0 + encrypt($1) }
    }

    let message = "Hello world!"
    let secretKey = "(:.,?P!9@PAz" // really should be randomly generated!
    let encryptedMessage = encrypt(message, key: secretKey) // > "`_B@PpVV2<%["
    let decryptedMessage = encrypt(encryptedMessage, key: secretKey) // > "Hello world!"