GoBo GoBo - 6 months ago 77
iOS Question

Verify md5 hash password with swift

Im trying to get a

NSData
object from a Blob to load it into a Web View. I've read the documentation about SQLite.swift but I've nothing clear. Please, help me. I think I need a working example.

my code:
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
}
}

Answer

I finally used bcrypt instead md5. I used JKBCrypt and the JKBCrypt.verifyPassword

Since I was using password stored from a php generated hash I had to a little trick:

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