Creating custom codec in Grails

13 / Nov / 2010 by Ankur Tripathi 1 comments

Grails applications can define their own codecs and Grails will load them along with the standard codecs. A custom codec class must be defined in the grails-app/utils/ directory and the class name must end with ‘Codec’. For more information see Dynamic Encoding Methods.


import java.security.MessageDigest
import sun.misc.BASE64Encoder

class PasswordCodec {

static encode = { str ->
MessageDigest md = MessageDigest.getInstance('SHA')
md.update(str.getBytes('UTF-8'))
return (new BASE64Encoder()).encode(md.digest())
}

}

Using this custom codec


String pwd = "12345"
println pwd.encodeAsPassword()  // Output : jLIjfQZ5yojbZGTqxg2pY0VROWQ=

Hope this helps!

FOUND THIS USEFUL? SHARE IT

comments (1 “Creating custom codec in Grails”)

Leave a comment - Gregg Bolinger Cancel reply