使用swift验证md5哈希密码

时间:2015-10-08 04:16:28

标签: ios swift sqlite swift2 xcode7

我试图从Blob获取NSData对象以将其加载到Web视图中。我已经阅读了有关SQLite.swift的文档但我没有说清楚。请帮我。我想我需要一个有效的例子。

我的代码: https://gist.github.com/gobozgz/9d1b02364f878bc1a026

import UIKit
import SQLite
class AreaInfoViewController: UIViewController {
    @IBOutlet weak var webView: UIWebView!    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Here I Will load the webView PDFFile into the webView
        // webView.loadRequest(getAreaPdf())
        }

    func getAreaPdf () -> NSDATA {
        var myPdf:NSDATA // I think this is wrong

        let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        let docsDir = dirPaths[0]
        let databasePath = docsDir.stringByAppendingString("/db.sqlite")
        let db = try! Connection(databasePath, readonly: true)

        let areas = Table("areas")
        let id = Expression<Int64>("id")
        let name = Expression<String>("name")
        let pdffile = Expression<SQLite.Blob>("pdffile")
        let query = areas.select(id,name,pdffile)
            .filter(id == 1)
            .limit(1)
        let result = db.prepare(query)
        for row in result {
            myPdf = row[pdffile] // Obviusly this is wrong
        }
        return myPdf // Obviusly this is wrong too
    }
}

1 个答案:

答案 0 :(得分:0)

我最后用bcrypt而不是md5。我使用了JKBCrypt和JKBCrypt.verifyPassword

由于我使用的是从php生成的哈希中存储的密码,因此我需要一个小技巧:

 var hash = user[password]
 let regex = try! NSRegularExpression(pattern: "\\$2y\\$10\\$", options: .CaseInsensitive)
 hash = regex.stringByReplacingMatchesInString(hash, options: [], range: NSRange(0..<hash.utf16.count), withTemplate: "\\$2a\\$10\\$")
 auth = JKBCrypt.verifyPassword(userPassword, matchesHash: hash)!