I'm still really struggling with this. I've tried to back the calculation up a few steps in the JS code, but that's leading to a ton of other problems for me, so I'm not sure that's going to work too well. I keep coming back to attempting to do this in Perl instead. I've boiled this down to a simple example:
#!/usr/bin/perl -w use strict; use CGI; use URI::Escape; use JSON; use Digest::SHA qw(hmac_sha512_hex); my $query = CGI->new; my $raw_data = $query->param('data'); my $data = decode_json($raw_data); my $actual_data = '{"request":{"service":"test"},"data":{"test_input": +"%2B2"}}'; print "raw_data: ".$raw_data."\n"; print "no escaping: ".$data->{data}->{test_input}." vs escaping: "; print uri_escape($data->{data}->{test_input})."\n"; print hmac_sha512_hex($raw_data,"ABCD1234")."\n"; print hmac_sha512_hex($actual_data,"ABCD1234")."\n";
If you run that on the command line like this: ./script.pl 'data={"request":{"service":"test"},"data":{"test_input":"%2B2"}}' You'll get this:
raw_data: {"request":{"service":"test"},"data":{"test_input":"+2"}} no escaping: +2 vs escaping: %2B2 3c6de296682e7f3896073fe41af9732a294ef723bb1e5c75aa1eba1af981f04f0a0963 +d03604119ea92b719a2912ef0c957c03a7268b51e2170f8fed7c875465 32595bf215b309a73c8dd4d09600430378f455c7cb44d31573b08566ddff0a7bd3c536 +8d70696b57a2c1c95e862ed7b062501e39820bf973c9309812250df460
I need the 32595... calculation to compare to the input (which I removed from the Perl example to make it shorter). I can't just unescape the whole string, because then it'll attempt to escape the part that makes it a JSON input ({, :, etc). It's as if the CGI input unescapes automatically and I can't figure out how to make it not unescape (or "re-escape"). Any ideas on how to do that before I write something myself to deal with escaped characters in a hmac sha? Thanks!

In reply to Re^2: CGI Input with Escaped Characters by hoyt
in thread CGI Input with Escaped Characters by hoyt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.