{"id":2054,"date":"2010-11-13T19:24:03","date_gmt":"2010-11-13T13:54:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2054"},"modified":"2016-12-16T11:51:57","modified_gmt":"2016-12-16T06:21:57","slug":"creating-custom-codec-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-custom-codec-in-grails\/","title":{"rendered":"Creating custom codec in Grails"},"content":{"rendered":"<p><a title=\"Grails Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails applications<\/a> 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 &#8216;Codec&#8217;. For more information see <a href=\"http:\/\/www.grails.org\/Dynamic+Encoding+Methods\">Dynamic Encoding Methods<\/a>.<\/p>\n<p>[java]<\/p>\n<p>import java.security.MessageDigest<br \/>\nimport sun.misc.BASE64Encoder<\/p>\n<p>class PasswordCodec {<\/p>\n<p>static encode = { str -&gt;<br \/>\nMessageDigest md = MessageDigest.getInstance(&#8216;SHA&#8217;)<br \/>\nmd.update(str.getBytes(&#8216;UTF-8&#8217;))<br \/>\nreturn (new BASE64Encoder()).encode(md.digest())<br \/>\n}<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>Using this custom codec<\/p>\n<p>[java]<\/p>\n<p>String pwd = &quot;12345&quot;<br \/>\nprintln pwd.encodeAsPassword()\u00a0 \/\/ Output : jLIjfQZ5yojbZGTqxg2pY0VROWQ=<\/p>\n<p>[\/java]<\/p>\n<p>Hope this helps!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8216;Codec&#8217;. For more information see Dynamic Encoding Methods. [java] import java.security.MessageDigest import sun.misc.BASE64Encoder class PasswordCodec { static encode = { [&hellip;]<\/p>\n","protected":false},"author":24,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[4301,4300,4840,4302],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2054"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2054"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2054\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}