hendrakmg
hendrakmg

Reputation: 37

How do I encrypt a password in Griffon?

I tried to encrypt a password in Griffon, but I don't know how to do that. Usually I'd use md5 in another language, but what in griffon?

Here is a bit of my code:

if (sql.firstRow("SELECT userID FROM tbluser WHERE userID = ${model.userID}") != null) {
    // usually in SQL like this
    user.executeUpdate("UPDATE tbluser SET username = ${model.username}, password = md5(${model.password}), level = ${model.level} WHERE userID = ${model.userID}")

    edt {
        int index = model.listUser.findIndexOf{it['userID'] == model.userID}
        model.listUser[index] += [username: model.username, password: model.password, level: model.level] --> how to do md5 here?
    }
} else {
    user.add(userID: model.userID, username: model.username, password: model.password, level: model.level) --> and here?

    edt { model.listUser << [userID: model.userID, username: model.username, password: model.password, level: model.level] }
}

I just get a sample code about it and it worked.

Here the code:

import java.security.MessageDigest
String generateMD5(String s) {
        MessageDigest digest = MessageDigest.getInstance("MD5")
        digest.update(s.bytes);
        return new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
}

for my case: generateMD5(model.password)

Upvotes: 0

Views: 95

Answers (2)

Andres Almiray
Andres Almiray

Reputation: 3281

Per se encryption is not related to Griffon, as it's just a transformation of a character/byte array. However there's a bcrypt plugin available that applies the BCrypt algorithm http://artifacts.griffon-framework.org/plugin/bcrypt

More information on BCrypt can be found at http://codahale.com/how-to-safely-store-a-password/

Upvotes: 1

Galexrt
Galexrt

Reputation: 13

I'm very new to griffon but i think this can help you. I hope i could help you with this.

Codesearch Griffon MD5

Upvotes: 0

Related Questions