<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I generate HMAC-SHA256 hashes in JSL in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456641#M70194</link>
    <description>&lt;P&gt;Thanks for confirming that&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jan 2022 08:46:26 GMT</pubDate>
    <dc:creator>pauldeen</dc:creator>
    <dc:date>2022-01-28T08:46:26Z</dc:date>
    <item>
      <title>How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/454725#M70105</link>
      <description>&lt;P&gt;For querrying a REST API I need to authenticate with HMAC-SHA256. Does anybody know of a way to do this in JSL? We might even use another REST API or webscraper to do it. For example this website:&amp;nbsp;&lt;A href="https://www.devglan.com/online-tools/hmac-sha256-online" target="_blank"&gt;https://www.devglan.com/online-tools/hmac-sha256-online&lt;/A&gt;&amp;nbsp;can calculate the hash but how do I make it work?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:09:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/454725#M70105</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2023-06-09T18:09:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/454984#M70148</link>
      <description>&lt;P&gt;certutil on windows. openssl on mac. (But I think you might not need to, see final sentence below.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A long time ago in &lt;LI-MESSAGE title="7 things to know about Twitter" uid="21000" url="https://community.jmp.com/t5/Uncharted/7-things-to-know-about-Twitter/m-p/21000#U21000" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-blog-thread lia-fa-icon lia-fa-blog lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp; I needed a SHA1. "certutil" is already installed on windows and will apparently do SHA256 as well. You might start with the code in the .zip AUTH.jsl file which uses runprogram and parses the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll get a bunch of choices of varying quality with this search &lt;A href="https://www.google.com/search?q=windows+hmac+sha256+certutil" target="_blank" rel="noopener"&gt;https://www.google.com/search?q=windows+hmac+sha256+certutil&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The HMAC part is described &lt;A href="https://en.wikipedia.org/wiki/HMAC" target="_blank" rel="noopener"&gt;https://en.wikipedia.org/wiki/HMAC&lt;/A&gt;&amp;nbsp;and &lt;STRONG&gt;there appears to be a related function&lt;/STRONG&gt; in the AUTH.jsl file. It's been a long time since I thought about it. edit: I probably used the Wikipedia example when the JSL was written. Wikipedia also provides some test cases if you write your own.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this may be built in to JMP's httpRequest/oauth2 functions as well &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/7172"&gt;@Paul_Nelson&lt;/a&gt;&amp;nbsp; &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/5036"&gt;@bryan_boone&lt;/a&gt;&amp;nbsp; but I've not studied them yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 01:24:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/454984#M70148</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2022-01-27T01:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/455410#M70161</link>
      <description>&lt;P&gt;Definetly interested in the built in solution but in the meantime your solution also works! I replaced SHA1 with SHA256 in line 9 of your auth.jsl file, thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 14:55:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/455410#M70161</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2022-01-27T14:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456310#M70173</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;for some reason I got it to work earlier today, then saved it and now it won't work anymore. Can you see my mistake? BITS.jsl and AUTH.jsl from your twitterpackage are also attached for reference.&lt;/P&gt;&lt;P&gt;Update, it was the blocksize, it is 64 for SHA265 I had assumed that is should be 265&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;include("BITS.jsl");
SHA256 = Function( {txt},
	{hash, rc, hexdata,temppath},
	try(deleteFile("$temp/deleteme.txt"));
	temppath = Save Text File( "$temp/deleteme.txt", txt );
	If( Host is( "windows" ),
		hash = RunProgram( executable( "certutil" ), options( "-hashfile "|| temppath|| " SHA256" ), readfunction( "text" ) ); // win jmp 11
		//hash = RunProgram( executable( "certutil" ), options( {"-hashfile", temppath, "SHA1"} ), readfunction( "text" ) );
		rc = Pat Match(
			hash,
			Pat Regex( "\[^.+?:\s+]\" ) + Pat Span( "0123456789abcdefABCDEF " ) &amp;gt;&amp;gt; hexdata + Pat Regex(
				"\[\s*CertUtil: -hashfile command completed successfully.\s*$]\"
			)
		);
		If( rc != 1,
			Throw( "bad hash:" || hash )
		);
		hexdata = Regex( hexdata, " ", "", GLOBALREPLACE );
	, // else MAC
		hash = RunProgram( executable( "/usr/bin/openssl" ), options( {"dgst", "-sha1", temppath} ), readfunction( "text" ) );
		hexdata =regex(hash,"=\s*([0-9a-fA-F]{40})\s*","\1");
		if( length(hexdata) != 40, throw("bad hash:" || hash));
	);
	try(deleteFile("$temp/deleteme.txt"),show("did not delete temp file??"));
	Return( Hex To Blob( hexdata ) );
);

HMACSHA256 = Function( {key, message},
    {blocksize = 64}, // Where blocksize is that of the underlying hash function
    //show(length(key));
    If(
        Length( key ) &amp;gt; blocksize, // keys longer than blocksize are shortened
            //print("shorten:");
            key = SHA256( key );
            //show(key,length(key));
    );
    if(
        Length( key ) &amp;lt; blocksize, // keys shorter than blocksize are zero-padded
            key = key || Matrix To Blob( J( (blocksize - Length( key )), 1, 0 ), "uint", 1, "big" )
    );
   //show(length(key));
    o_key_pad = BITS:XORBLOB( Matrix To Blob( J( blocksize, 1, Hex To Number( "5c" ) ), "uint", 1, "big" ), key );
    i_key_pad = BITS:XORBLOB( Matrix To Blob( J( blocksize, 1, Hex To Number( "36" ) ), "uint", 1, "big" ), key );
   
    Return( SHA256( o_key_pad || SHA256( i_key_pad || message ) ) );
);

//from https://www.devglan.com/online-tools/hmac-sha256-online
if("090a613de1a32c513f5477019dbd395fe4c6727708e945277d6af6c5cf7a6778" != hex(HMACSHA256( Char To Blob( "dbc62ec300b2624c580611858d94f2332ac63" ), Char To Blob( "Why don't we test this" ) )), throw("hmac busted"));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I checked the certutil SHA256 function and that generates correct hashes. It seems that the HMACSHA256 or in the BITS functions that it calls, where it goes wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 14:56:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456310#M70173</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2022-01-27T14:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456381#M70175</link>
      <description>&lt;P&gt;try uppercase() on the left-hand-side test value. Looks like it works.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 15:35:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456381#M70175</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2022-01-27T15:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456534#M70188</link>
      <description>&lt;P&gt;HMAC-SHA256 is not currently built in to HTTP Request/OAuth2.&lt;/P&gt;
&lt;P&gt;It would have to be done the way Craige described.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 21:20:49 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456534#M70188</guid>
      <dc:creator>bryan_boone</dc:creator>
      <dc:date>2022-01-27T21:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: How do I generate HMAC-SHA256 hashes in JSL</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456641#M70194</link>
      <description>&lt;P&gt;Thanks for confirming that&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 08:46:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-I-generate-HMAC-SHA256-hashes-in-JSL/m-p/456641#M70194</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2022-01-28T08:46:26Z</dc:date>
    </item>
  </channel>
</rss>

