iPhone岩を採掘

岩をタッチすると、md5の頭000となるhashを掘ってみたりするiPhoneアプリのサンプルコードを描いてみます。

//

//  rockMining-Bridging-Header.h

//

#import <CommonCrypto/CommonCrypto.h>

//

//  ViewController.swift

//  

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    weak var terminal : UILabel?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createRock()

        createTerminal()

    }

    

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.backgroundColor = UIColor(hue: 0.5, saturation: 0.2, brightness: 1, alpha: 1)

        sv.scene = SCNScene()

        sv.allowsCameraControl = true

        view.addSubview(sv)

        

        sceneView = sv

    }

    func createRock() {

        let ground = SCNBox(width: 5, height: 1, length: 5, chamferRadius: 0)

        ground.firstMaterial?.diffuse.contents = UIColor.brownColor()

        let groundNode = SCNNode(geometry: ground)

        groundNode.position = SCNVector3(x: 0, y: –1, z: 0)

        sceneView?.scene?.rootNode.addChildNode(groundNode)

        

        let rock = SCNBox(width: 2, height: 2, length: 2, chamferRadius: 0.8)

        rock.firstMaterial?.diffuse.contents = UIColor.darkGrayColor()

        let rockNode = SCNNode(geometry: rock)

        rockNode.name = “rock”

        sceneView?.scene?.rootNode.addChildNode(rockNode)

    }

    

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        if let t = touches.first as? UITouch {

            let p = t.locationInView(self.sceneView)

            if let selected = self.sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey : true]) {

                if (selected.first?.node.name == “rock”) {

                    miningMD5Zero3()

                }

            }

        }

    }

    

    func miningMD5Zero3() {

        

        

        self.terminal?.font = UIFont.boldSystemFontOfSize(16)

        

        var queue = [String]()

        

        for i in 010000 {

            let hash = “asdfasdfa\(arc4random() % 1000000)”.md5()

            

            if (queue.count > 5) { queue.removeAtIndex(0) }

            queue.append(hash + “\n”)

            

            let str = queue.reduce(“”, combine: +)

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(i) * 0.001 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {

                self.terminal?.text = str

            })

            

            if hash.hasPrefix(“000”) {

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(Double(i) * 0.001 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {

                    self.terminal?.font = UIFont.boldSystemFontOfSize(30)

                    self.terminal?.text = hash

                })

                break

            }

        }

    }

    func createTerminal() {

        let title = UILabel()

        title.text = “mining 000”

        title.font = UIFont.systemFontOfSize(40)

        title.textColor = UIColor(hue: 0.48, saturation: 0.8, brightness: 0.6, alpha: 1)

        title.sizeToFit()

        title.center = CGPoint(x: CGRectGetMidX(view.bounds), y: 150)

        self.view.addSubview(title)

        

        let label = UILabel(frame: CGRect(x: 30, y: 300, width: CGRectGetMaxX(view.bounds) – 60, height: 200))

        label.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)

        label.textColor = UIColor(hue: 0.2, saturation: 0.1, brightness: 1, alpha: 1)

        label.lineBreakMode = .ByWordWrapping

        label.numberOfLines = 0

        self.view.addSubview(label)

        

        self.terminal = label

    }

}

extension String {

    func md5() -> String! {

        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)

        let strLen = CUnsignedInt(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))

        let digestLen = Int(CC_MD5_DIGEST_LENGTH)

        let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)

        

        CC_MD5(str!, strLen, result)

        

        var hash = NSMutableString()

        for i in 0..<digestLen {

            hash.appendFormat(“%02x”, result[i])

        }

        

        result.destroy()

        

        return String(format: hash as String)

    }

}