Creating custom codec in Grails
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!
Hey, do you know why services cannot be injected into a custom codec?