string.unpack swift等价?

时间:2015-11-03 16:17:46

标签: ruby swift authentication encryption cryptography

我一直在寻找并且似乎找不到Swift的String.unpack(ruby)。例如,我试图在Swift中编写这两个语句:

offset = digest.unpack('@19C').first & 0x2f
value = digest.unpack("#{offset}@L").first

如果有人知道如何实现这一点,那将非常有帮助,谢谢!

编辑:

到目前为止

代码:

import Foundation

class PasswordViewController: UIViewController {

    @IBOutlet weak var simpleLabel: UILabel!

    var email:String!
    var LRA:String!
    var sysid:String = "SECRET"

    override func viewDidLoad() {
        super.viewDidLoad()
        var x:Int = 1446569017
        reset_code(x)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func randomInt(min: Int, max:Int) -> Int {
        return min + Int(arc4random_uniform(UInt32(max - min + 1)))
    }

    func reset_secret()->String {
        return sysid.hmac(.SHA512, key: "THISISAKEY")
    }

    func reset_input(t:Int)->String{
        var time:String = String(t)
        var input:[String] = [sysid, email, time]
        var stringrep:String = "|".join(input)
        return stringrep
    }

    func reset_hmac(t:Int)->String{
        var firstTime:String = reset_secret().hmac(.SHA256, key: reset_input(t))
        return reset_secret().hmac(.SHA256, key: firstTime)
    }

    func reset_code(t:Int){
        var time:Int = t / 43200
        var digest:String = reset_hmac(t)
        simpleLabel.text = digest
    }

}



enum CryptoAlgorithm {
    case SHA256, SHA512

    var HMACAlgorithm: CCHmacAlgorithm {
        var result: Int = 0
        switch self {
        case .SHA256:   result = kCCHmacAlgSHA256
        case .SHA512:   result = kCCHmacAlgSHA512
        }
        return CCHmacAlgorithm(result)
    }

    var digestLength: Int {
        var result: Int32 = 0
        switch self {
        case .SHA256:   result = CC_SHA256_DIGEST_LENGTH
        case .SHA512:   result = CC_SHA512_DIGEST_LENGTH
        }
        return Int(result)
    }
}

extension String {

    func hmac(algorithm: CryptoAlgorithm, key: String) -> String {
        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
        let strLen = Int(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
        let digestLen = algorithm.digestLength
        let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
        let keyStr = key.cStringUsingEncoding(NSUTF8StringEncoding)
        let keyLen = Int(key.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))

        CCHmac(algorithm.HMACAlgorithm, keyStr!, keyLen, str!, strLen, result)

        let digest = stringFromResult(result, length: digestLen)

        result.dealloc(digestLen)

        return digest
    }

    private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
        var hash = NSMutableString()
        for i in 0..<length {
            hash.appendFormat("%02x", result[i])
        }
        return String(hash)
    }

}

0 个答案:

没有答案