IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
Topic
  • 6 replies
  • Latest Post - ‏2015-01-08T06:53:29Z by Offline
srini1973
srini1973
19 Posts

Pinned topic Base64 Encoding using gateway Script

‏2014-11-20T03:35:33Z |

Hello,

 

How can we do Base64 Encoding using gateway Script?

 

Thanks.

  • HermannSW
    HermannSW
    8729 Posts

    Re: Base64 Encoding using gateway Script

    ‏2014-11-20T11:11:20Z  

    Hi,

    there is no general base64 encode/decode solution available in JavaScript.
    Mozilla provides  atob()  and  btoa()  methods  for that purpose:
    https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

     

    In GatewayScript base64 encoding/decoding is done via the Buffer object and "base64" encoding.

    Here is an encode example (attached as well):

    $ cat base64.encode.js 
    var buffer = new Buffer("täst12345");
    session.output.write(buffer.toString('base64',1,5));
    $ 
    $ echo foobar | coproc2 base64.encode.js - http://dp2-l3:2227 ; echo
    w6RzdA==
    $ 
    $ echo -n "w6RzdA==" | base64 -d  | od -tcx1
    0000000 303 244   s   t
             c3  a4  73  74
    0000004
    $
    

     

    And here is decode example (attached as well):

    $ cat base64.decode.js 
    var buffer = new Buffer("test12345");
    buffer.write("YmxhaA==",0,8,"base64");
    session.output.write(buffer.toString());
    $ 
    $ echo foobar | coproc2 base64.decode.js - http://dp2-l3:2227 ; echo
    blah12345
    $ 
    $ echo -n "YmxhaA==" | base64 -d | od -tcx1
    0000000   b   l   a   h
             62  6c  61  68
    0000004
    $
    

     


    Hermann <myBlog/> <myTweets/> | <GraphvizFiddle/> | <xqib/> | <myCE/> <myFrameless/> | 

  • srini1973
    srini1973
    19 Posts

    Re: Base64 Encoding using gateway Script

    ‏2014-11-21T23:15:16Z  
    • HermannSW
    • ‏2014-11-20T11:11:20Z

    Hi,

    there is no general base64 encode/decode solution available in JavaScript.
    Mozilla provides  atob()  and  btoa()  methods  for that purpose:
    https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

     

    In GatewayScript base64 encoding/decoding is done via the Buffer object and "base64" encoding.

    Here is an encode example (attached as well):

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">$ cat base64.encode.js var buffer = new Buffer("täst12345"); session.output.write(buffer.toString('base64',1,5)); $ $ echo foobar | coproc2 base64.encode.js - http://dp2-l3:2227 ; echo w6RzdA== $ $ echo -n "w6RzdA==" | base64 -d | od -tcx1 0000000 303 244 s t c3 a4 73 74 0000004 $ </pre>

     

    And here is decode example (attached as well):

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">$ cat base64.decode.js var buffer = new Buffer("test12345"); buffer.write("YmxhaA==",0,8,"base64"); session.output.write(buffer.toString()); $ $ echo foobar | coproc2 base64.decode.js - http://dp2-l3:2227 ; echo blah12345 $ $ echo -n "YmxhaA==" | base64 -d | od -tcx1 0000000 b l a h 62 6c 61 68 0000004 $ </pre>

     


    Hermann <myBlog/> <myTweets/> | <GraphvizFiddle/> | <xqib/> | <myCE/> <myFrameless/> | 

    Thank you.

  • Offline
    Offline
    497 Posts

    Re: Base64 Encoding using gateway Script

    ‏2014-11-22T20:42:02Z  
    • srini1973
    • ‏2014-11-21T23:15:16Z

    Thank you.

    Since DP runs Node.js the following might work:

    console.log(new Buffer("Hello World").toString('base64'));
    //SGVsbG8gV29ybGQ=
    console.log(new Buffer("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
    //Hello World
    

    Regards,

    Anders

    Updated on 2014-11-22T20:42:38Z at 2014-11-22T20:42:38Z by Offline
  • onderozcan
    onderozcan
    13 Posts

    Re: Base64 Encoding using gateway Script

    ‏2015-01-07T17:06:56Z  
    • Offline
    • ‏2014-11-22T20:42:02Z

    Since DP runs Node.js the following might work:

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">console.log(new Buffer("Hello World").toString('base64')); //SGVsbG8gV29ybGQ= console.log(new Buffer("SGVsbG8gV29ybGQ=", 'base64').toString('ascii')) //Hello World </pre>

    Regards,

    Anders

    Hi Anders,

    I am not sure about this is the right thread but I have a problem regarding reading and writing text file that contains Turkish characters. I used toString('ascii') method but did not work out. By the way, running Node.js on DataPower is an exciting solution for me. Could you please explain howto install Node.js on datapower ? Thank you so much.

     

    Best regards,

    Önder

  • HermannSW
    HermannSW
    8729 Posts

    Re: Base64 Encoding using gateway Script

    ‏2015-01-07T17:59:30Z  

    Hi Anders,

    I am not sure about this is the right thread but I have a problem regarding reading and writing text file that contains Turkish characters. I used toString('ascii') method but did not work out. By the way, running Node.js on DataPower is an exciting solution for me. Could you please explain howto install Node.js on datapower ? Thank you so much.

     

    Best regards,

    Önder

    Hi Önder,

    please use  toString('utf8')  in case your Turkish characters are UTF-8 encoded, find all toString() options here:
    http://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.0.0/com.ibm.dp.xb.doc/buffer_js.html

    You do not need to (and cannot) install any new software package on DataPower, but you can upgrede DataPower firmware. Any firmware ≥7.0.0.0 comes with GatewayScript support, see:
    http://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.0.0/com.ibm.dp.xg.doc/gatewayscript_development_reference.html
    http://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.0.0/com.ibm.dp.xg.doc/gs_introduction.html


    Hermann.

     

    Updated on 2015-01-07T18:00:17Z at 2015-01-07T18:00:17Z by HermannSW
  • Offline
    Offline
    497 Posts

    Re: Base64 Encoding using gateway Script

    ‏2015-01-08T06:53:29Z  

    Hi Anders,

    I am not sure about this is the right thread but I have a problem regarding reading and writing text file that contains Turkish characters. I used toString('ascii') method but did not work out. By the way, running Node.js on DataPower is an exciting solution for me. Could you please explain howto install Node.js on datapower ? Thank you so much.

     

    Best regards,

    Önder

    First of all make sure that the data is "intact" when it arrives. Use File capture or simply output the message to a MQ queue or something so you can see that the Turkish characters are ok.

    I often have this problem with Swedish double-byte characters, e.g. é, Å, Ä, Ö, where the sending party has encoded them as ANSI or something and thus "breaking" any double-byte character. If the double-byte character is already "broken" when it hits DataPower there is nothing you can do to salvage it...