Para consultar una API REST, necesito autenticarme con HMAC-SHA256. ¿Alguien sabe de una manera de hacer esto en JSL? Incluso podríamos usar otra API REST o webscraper para hacerlo. Por ejemplo este sitio web: https://www.devglan.com/herramientas-online/hmac-sha256-online puedo calcular el hash, pero ¿cómo puedo hacer que funcione?
certutil en windows. abre SSL en mac. (Pero creo que es posible que no necesite hacerlo, vea la oración final a continuación).
hace mucho tiempo en 7 cosas que debes saber sobre Twitter Necesitaba un SHA1. "certutil" ya está instalado en Windows y aparentemente también hará SHA256. Puede comenzar con el código en el archivo .zip AUTH.jsl que usa el programa de ejecución y analiza la salida.
Obtendrá un montón de opciones de calidad variable con esta búsqueda https://www.google.com/search?q=windows+hmac+sha256+certutil
La parte HMAC se describe https://en.wikipedia.org/wiki/HMAC y parece haber una función relacionada en el archivo AUTH.jsl. Ha pasado mucho tiempo desde que pensé en ello.
Creo que esto también puede estar integrado en las funciones httpRequest/oauth2 de JMP @Paul_Nelson @bryan_boone pero no los he estudiado todavía.
Definitivamente interesado en la solución integrada, pero mientras tanto, ¡su solución también funciona! Reemplacé SHA1 con SHA256 en la línea 9 de su archivo auth.jsl y blockize = 256, ¡gracias!
@Craige_Hales por alguna razón hice que funcionara más temprano hoy, luego lo guardé y ahora ya no funcionará. ¿Puedes ver mi error? BITS.jsl y AUTH.jsl de su paquete de twitter también se adjuntan como referencia.
Actualización, era el tamaño de bloque, es 64 para SHA265. Supuse que debería ser 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"));Revisé la función certutil SHA256 y eso genera hashes correctos. Parece que el HMACSHA256 o en las funciones de BITS que llama, donde sale mal.
intente con mayúsculas () en el valor de prueba del lado izquierdo. Parece que funciona.
HMAC-SHA256 no está integrado actualmente en HTTP Request/OAuth2.
Tendría que hacerse como lo describió Craige.