2015-10-08 110 views
0

我试图从Blob中获取NSData对象以将其加载到Web视图中。我已阅读关于SQLite.swift的文档,但我没有清楚。请帮帮我。我想我需要一个工作的例子。快速验证md5哈希密码

我的代码: 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 
    } 
} 
+0

问题是什么?顺便说一句,我希望你明白,MD5是一个星期的散列函数,这是不适合密码散列。 –

+0

我想我犯了一个错误,并编辑了一个错误的问题。最后我使用了JKBCrypt和Bcript。 – GoBo

回答

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)!