【问题标题】:Verify md5 hash password with swift使用 swift 验证 md5 哈希密码
【发布时间】:2015-10-08 04:16:28
【问题描述】:

我试图从 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
    }
}

【问题讨论】:

  • 有什么问题?顺便说一句,我希望您了解 md5 是一个星期哈希函数,它适合密码哈希。
  • 我认为我犯了一个错误并编辑了一个错误的问题。最后我使用了 JKBCrypt 和 Bcript。

标签: ios swift sqlite swift2 xcode7


【解决方案1】:

我终于用 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)!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多