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.

[java]

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())
}

}

[/java]

Using this custom codec

[java]

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

[/java]

Hope this helps!

FOUND THIS USEFUL? SHARE IT

comments (1 “Creating custom codec in Grails”)

Leave a Reply to Gregg Bolinger Cancel reply

Your email address will not be published. Required fields are marked *