We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40001
    • 15 Posts
    Hi there,

    I need a little bit of a pointer.
    I am using a php function named hash_hmac.

    The hash works perfectly when processed straight from a php file, even from the same server that modx is sat on.
    When however, this code is ran from a snippet I get a completely wrong hash.

    Does anyone know what could be going wrong?

    Many thanks
    • I put the PHP documentation's sample in a .php file, and the code into a snippet, and they both produced the same hash. This is on wildly different servers; the file on my local Mac and the snippet on a remote Linux hosted Revo 2.2.4

      The snippet:
      <?php
      $output = hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
      return $output;
      
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 40001
        • 15 Posts
        Please try this code:
        <?php
        
        $api_key = "1234AbCD";
        function get_verification($var_name, $var_val
        ue, $var_code) {
            global $api_key;
                $encodingval = htmlspecialchars($var_
        code) . htmlspecialchars($var_name) . htmlspe
        cialchars($var_value);
                return '||'.hash_hmac('sha256', $enco
        dingval, $api_key).($var_value == "--OPEN--" 
        ? "||open" : "");
        }
        
        $name = get_verification('name', 'My Widget',
         'mycode');
        $code = get_verification('code', 'mycode', 'm
        ycode');
        $price = get_verification('price', '1.99', 'm
        ycode');
        
        echo $name;
        echo "<br />";
        ?>
        • Ok, the api_key value is not getting loaded into the function. You can add it to the function argument list instead:

          $api_key = "1234AbCD";
          
          function get_verification($var_name, $var_value, $var_code, $api_key) {
              $encodingval = htmlspecialchars($var_code) . htmlspecialchars($var_name) . htmlspecialchars($var_value);
              return '||'.hash_hmac('sha256', $encodingval, $api_key).($var_value == "--OPEN--" ? "||open" : "");
          }
          
          $name = get_verification('name', 'My Widget', 'mycode', $api_key);
          $code = get_verification('code', 'mycode', 'mycode', $api_key);
          $price = get_verification('price', '1.99', 'mycode', $api_key);
           
          return $name;
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
          • This may have something to do with the globals issue...


            snippet code does not run in the global namespace in the first place.
            So before, you were addressing the globals in your function, then outside of the function, the variables were not actually globals as they may have seemed. To make predictable use of globals in a snippet, you really need to address them via the $GLOBAL array e.g. $GLOBAL['myPrice']
            http://forums.modx.com/thread/32924/functions-w-switch-case-and-global-variable-errors#dis-post-180705
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 40001
              • 15 Posts
              Thank you,

              Excuse my ignorance but I'm not sure what to do next.
              Should I move $api_key into the function?
                • 40001
                • 15 Posts
                Ah I think I get it. I will report back on if it works!
                  • 40001
                  • 15 Posts
                  Its Worked! Thank you, you're wonderful!