为了查询 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。
它必须按照克雷格描述的方式进行。