title: Paytm payment gateway checksum file crypto error
body: <p>Creating a custom payments page using paytm payments gateway.
  The initiate payments route requires to checksum hash and
  They have a checksum file which is giving a crypto error
  <strong>node:internal/crypto/cipher:116
  this[kHandle].initiv(cipher, credential, iv, authTagLength);
  ^
  RangeError: Invalid key length</strong></p>
  <p>and the code of this file is :-</p>
  <pre><code>&quot;use strict&quot;;
  
  var crypto = require('crypto');
  
  class PaytmChecksum {
  
      static encrypt(input, key) {
          var cipher = crypto.createCipheriv('AES-128-CBC', key, PaytmChecksum.iv);
          var encrypted = cipher.update(input, 'binary', 'base64');
          encrypted += cipher.final('base64');
          return encrypted;
      }
      static decrypt(encrypted, key) {
          var decipher = crypto.createDecipheriv('AES-128-CBC', key, PaytmChecksum.iv);
          var decrypted = decipher.update(encrypted, 'base64', 'binary');
          try {
              decrypted += decipher.final('binary');
          }
          catch (e) {
              console.log(e);
          }
          return decrypted;
      }
      static generateSignature(params, key) {
          if (typeof params !== &quot;object&quot; &amp;&amp; typeof params !== &quot;string&quot;) {
              var error = &quot;string or object expected, &quot; + (typeof params) + &quot; given.&quot;;
              return Promise.reject(error);
          }
          if (typeof params !== &quot;string&quot;){
              params = PaytmChecksum.getStringByParams(params);
          }
          return PaytmChecksum.generateSignatureByString(params, key);
      }
      
  
      static verifySignature(params, key, checksum) {
          if (typeof params !== &quot;object&quot; &amp;&amp; typeof params !== &quot;string&quot;) {
              var error = &quot;string or object expected, &quot; + (typeof params) + &quot; given.&quot;;
              return Promise.reject(error);
          }
          if(params.hasOwnProperty(&quot;CHECKSUMHASH&quot;)){
              delete params.CHECKSUMHASH
          }
          if (typeof params !== &quot;string&quot;){
              params = PaytmChecksum.getStringByParams(params);
          }
          return PaytmChecksum.verifySignatureByString(params, key, checksum);
      }
  
      static async generateSignatureByString(params, key) {
          var salt = await PaytmChecksum.generateRandomString(4);
          return PaytmChecksum.calculateChecksum(params, key, salt);
      }
  
      static verifySignatureByString(params, key, checksum) {     
          var paytm_hash = PaytmChecksum.decrypt(checksum, key);
          var salt = paytm_hash.substr(paytm_hash.length - 4);
          return (paytm_hash === PaytmChecksum.calculateHash(params, salt));
      }
  
      static generateRandomString(length) {
          return new Promise(function (resolve, reject) {
              crypto.randomBytes((length * 3.0) / 4.0, function (err, buf) {
                  if (!err) {
                      var salt = buf.toString(&quot;base64&quot;);
                      resolve(salt);                  
                  }
                  else {
                      console.log(&quot;error occurred in generateRandomString: &quot; + err);
                      reject(err);
                  }
              });
          });
      }
  
      static getStringByParams(params) {
          var data = {};
          Object.keys(params).sort().forEach(function(key,value) {
              data[key] = (params[key] !== null &amp;&amp; params[key].toLowerCase() !== &quot;null&quot;) ? params[key] : &quot;&quot;;
          });
          return Object.values(data).join('|');
      }
  
      static calculateHash(params, salt) {        
          var finalString = params + &quot;|&quot; + salt;
          return crypto.createHash('sha256').update(finalString).digest('hex') + salt;
      }
      static calculateChecksum(params, key, salt) {       
          var hashString = PaytmChecksum.calculateHash(params, salt);
          return PaytmChecksum.encrypt(hashString,key);
      }
  }
  PaytmChecksum.iv = '@@@@&amp;&amp;&amp;&amp;####$$$$';
  module.exports = PaytmChecksum;
  </code></pre>