為了查詢 REST API,我需要使用 HMAC-SHA256 進行身份驗證。 有人知道在 JSL 中執行此操作的方法嗎? 我們甚至可以使用另一個 REST API 或 webscraper 來做到這一點。 例如這個網站: https://www.devglan.com/online-tools/hmac-sha256-online可以計算哈希,但我如何使它工作?
Windows 上的 certutil。 mac上的openssl。 (但我認為您可能不需要,請參閱下面的最後一句話。)
很久以前在關於推特的 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 文件中。 我已經很久沒有考慮過了。
我認為這也可能內置於 JMP 的 httpRequest/oauth2 函數中@Paul_Nelson @bryan_boone但我還沒有研究過它們。
絕對對內置解決方案感興趣,但同時您的解決方案也可以工作! 我在 auth.jsl 文件的第 9 行將 SHA1 替換為 SHA256,blocksize = 256,謝謝!
@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 函數中出錯了。
HMAC-SHA256 當前未內置到 HTTP 請求/OAuth2。
必須按照克雷格描述的方式進行。