API Connect

 View Only
Expand all | Collapse all

CHECKSUM

  • 1.  CHECKSUM

    Posted Tue November 02, 2021 04:30 AM
    Hello Everyone,

    We want to implement Checksum &  GCM encryption using gateway script so can you guide us how we can implement this?

    ------------------------------
    Akshay Sawant
    ------------------------------


  • 2.  RE: CHECKSUM

    IBM Champion
    Posted Wed November 03, 2021 08:13 AM
    For checksum generation, you can make use of crypto module in GatewayScript. Here is an example - 

    var crypto = require('crypto')
    function checksumGenerate(str, algorithm, encoding) {
     return crypto
      .createHash(algorithm || 'md5')
      .update(str, 'utf8')
      .digest(encoding || 'hex')
    }

    checksumGenerate('Your Text')
    checksumGenerate('Your Text', 'sha1')

    You can find more details on the modules and supported algorithms here:
    https://www.ibm.com/docs/en/datapower-gateways/10.0.1?topic=apis-crypto-module


  • 3.  RE: CHECKSUM

    Posted Tue November 09, 2021 04:36 AM
    Hi Romil,

    We want to use key which is given by client & use HmacSHA256 Algorithm for calculate checksum so can you guide us how we can implement checksum code?

    ------------------------------
    Prashant Patel
    ------------------------------



  • 4.  RE: CHECKSUM

    IBM Champion
    Posted Tue November 09, 2021 07:56 AM
    Hi @prashant - You can create an HMAC using crypto.createHmac() module.

    You can find an example and additional details on the module here:
    https://www.ibm.com/docs/en/datapower-gateways/10.0.1?topic=apis-crypto-module#crypto.createHmac​​


  • 5.  RE: CHECKSUM

    Posted Tue November 09, 2021 08:46 AM
    Hi Romil,

    We have used below code for HMAC but getting Named shared secret key 'Alice' not found

    var crypto = require('crypto');
    var key = "Alice";
    var hmac = crypto.createHmac('hmac-sha256', key);
    var input = "This is plaintext to hash";
    var result = hmac.update(input).digest('base64');

    session.output.write(result);


    ------------------------------
    Prashant Patel
    ------------------------------



  • 6.  RE: CHECKSUM

    IBM Champion
    Posted Tue November 09, 2021 08:54 AM
    You will have to replace 'Alice' with your shared secret key object name


  • 7.  RE: CHECKSUM

    Posted Wed November 10, 2021 12:06 AM
    Hi Romil,

    We have "kpidfrtivrns5ZIvZdGOUirctckmjM"  key & calculate checksum for json request body using HmacSHA256 algorithm. So if you provide us  sample code so it's very helpful for us.


    ------------------------------
    Prashant Patel
    ------------------------------



  • 8.  RE: CHECKSUM

    Posted Wed November 10, 2021 05:19 AM
    Hi Romil,

    We have used below code, calculation of checksum for json request body using HmacSHA256 algorithm but getting "invalid type 'undefined (undefined)' detected on method invocation. Method name: createHmac; Parameter index: 0; Expected type: string". So can you guide us what we are doing wroung exactly in code?

    var crypto = require('crypto')
    function checksumGenerate(str, algorithm, encoding,key) {
    var key ="kpidfrtivrns5ZIvZdGOUirctckmjM";
    return crypto
    .createHmac(algorithm,key)
    .update(str, 'utf8')
    .digest(encoding || 'hex')
    }
    checksumGenerate('application/json')
    var store = checksumGenerate('application/json', 'sha1');
    //var str = {"CHECKSUM":store};
    session.output.write(store);
    apim.output('*/*');

    ------------------------------
    Prashant Patel
    ------------------------------



  • 9.  RE: CHECKSUM

    IBM Champion
    Posted Thu November 11, 2021 07:38 AM
    Hi Prashant, 
    Looks like you are not passing all the necessary parameters to your function. Here is an example based on your code. 

    const crypto = require('crypto');
    const yourKey = new Buffer('kpidfrtivrns5ZIvZdGOUirctckmjM');
    const yourAlg = 'sha1';
    const yourEnc = 'base64';
    const yourPayload = '1234565789';


    function checksumGenerate(str, algorithm, encoding,key) {
    let yourHash = crypto.createHmac(algorithm, key).update(str).digest(encoding);
    session.output.write({"hash": yourHash});
    }

    checksumGenerate(yourPayload, yourAlg, yourEnc, yourKey);



  • 10.  RE: CHECKSUM

    Posted Thu November 11, 2021 08:11 AM
    Hi Romil,

    Thanks for your support now it's working fine

    ------------------------------
    Prashant Patel
    ------------------------------



  • 11.  RE: CHECKSUM

    IBM Champion
    Posted Wed November 03, 2021 08:29 AM
    Forgot to add this:

    Similarly, you can use the crypto.createCipheriv() to perform encryption with below three supported GCM algorithm
    • A256GCM
    • A192GCM
    • A128GCM


    You can find more details on the module with examples here:
    https://www.ibm.com/docs/en/datapower-gateways/10.0.1?topic=apis-crypto-module#crypto.createCipherivcrypto module - IBM Documentation




    • 12.  RE: CHECKSUM

      Posted Fri November 19, 2021 05:46 AM
      Hi Romil,

      Now I want decode my calculated encoded checksum(HmacSHA256 algo.) value, Can you guide me How I can decode the checksum value using gateway script ?

      ------------------------------
      Akshay Sawant
      ------------------------------



    • 13.  RE: CHECKSUM

      IBM Champion
      Posted Fri November 19, 2021 11:08 AM
      Hi @Akshay Sawant - HMAC is a hash, not a cipher, so it cannot be decoded/decrypted. I am assuming you want to authenticate the Client's (incoming) message using HMAC.

      If so, on the server-side (assuming APIC in your case) you will need to re-generate the HMAC on the incoming message (or with the combination of incoming message + other pre-defined metadata) using the same hashing method. You will then compare the received HMAC with your server-side generated HMAC. If both the hashes are matched then the server will consider this request as a valid request.


    • 14.  RE: CHECKSUM

      Posted Fri November 19, 2021 12:10 PM
      Hi Romil,

      Thanks for replay We will try it.

      Now we validate checksum & basic authentication value which is pass in request header but when we used basic auth then we got 401 Unauthorize error which we mention in gateway script in else condition.

      When we removed validation gateway script then we got the output so can you guide us how we can access output without remove gatewayscript?

      ------------------------------
      Akshay Sawant
      ------------------------------



    • 15.  RE: CHECKSUM

      IBM Champion
      Posted Sat November 20, 2021 08:40 AM
      Please be more descriptive and start a new thread if this issue/ask is unrelated. If you feel there is a bug, please reach out to IBM Support.