為了查詢 REST API,我需要使用 HMAC-SHA256 進行驗證。 有人知道在 JSL 中做到這一點的方法嗎? 我們甚至可以使用另一個 REST API 或網路爬蟲來做到這一點。 例如這個網站: https://www.devglan.com/online-tools/hmac-sha256-online可以計算哈希值,但我該如何讓它工作?
HMAC-SHA256 目前未內建在 HTTP 請求/OAuth2 中。
必須按照克雷格描述的方式完成。
嘗試對左側測試值使用 uppercase() 。 看起來有效。
@Craige_Hales由於某種原因,我今天早些時候讓它工作,然後保存了它,現在它不再工作了。 你看得出我的錯誤嗎? 也附上了您的 twitterpackage 中的 BITS.jsl 和 AUTH.jsl 以供參考。
更新,這是區塊大小,SHA265 的大小是 64 我以為應該是 265
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 " ) >> 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 ) > blocksize, // keys longer than blocksize are shortened
//print("shorten:");
key = SHA256( key );
//show(key,length(key));
);
if(
Length( key ) < 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"));我檢查了 certutil SHA256 函數,它產生了正確的雜湊值。 看來是HMACSHA256還是在它所呼叫的BITS函數中,哪裡出了問題。
對內建解決方案絕對感興趣,但同時您的解決方案也有效! 我在你的 auth.jsl 檔案的第 9 行用 SHA256 取代了 SHA1,謝謝!
Windows 上的 certutil。 Mac 上的 openssl。 (但我認為你可能不需要,請參閱下面的最後一句話。)
很久以前在關於 Twitter 需要了解的 7 件事我需要 SHA1。 「certutil」已經安裝在 Windows 上,而且顯然也會執行 SHA256。 您可以從 .zip AUTH.jsl 檔案中的程式碼開始,該檔案使用 runprogram 並解析輸出。
透過此搜索,您將獲得大量不同品質的選擇https://www.google.com/search?q=windows+hmac+sha256+certutil
HMAC部分描述https://en.wikipedia.org/wiki/HMAC和似乎有一個相關的功能在 AUTH.jsl 檔案中。 我已經很久沒有思考這個問題了。編輯:在編寫 JSL 時,我可能使用了維基百科的範例。 如果您自己編寫,維基百科還提供了一些測試案例。
我認為這也可能內建在 JMP 的 httpRequest/oauth2 函數中@保羅尼爾森@bryan_boone但我還沒有研究過它們。