hash_hmac使用纯粹的经典ASP

时间:2013-01-02 17:16:57

标签: asp-classic

我想知道,有没有办法在经典ASP中实现hash_hmac("sha256", $token, $signkey, true)(php)?

我需要它来验证来自Facebook的signed_request https://developers.facebook.com/docs/howtos/login/signed-request/

// Adding the verification of the signed_request below
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); 
if ($sig !== $expected_sig) {
  error_log('Bad Signed JSON signature!');
  return null;
}

3 个答案:

答案 0 :(得分:9)

我一直在使用我在亚马逊论坛上找到的文件。这是线程: https://forums.aws.amazon.com/message.jspa?messageID=147377

它使用.wsc文件,它只是一个JScript文件,用于定义可在ASP代码中使用的对象。像这样:

' ### be sure to have sha256.wsc in the same folder as this script
    Dim sha256
    Set sha256 = GetObject( "script:" & Server.MapPath("sha256.wsc") )
    sha256.hexcase = 0

    Dim result
    result = sha256.b64_hmac_sha256( secretkey, stringtosign )

这是一个最初用于签署Amazon API请求的文件。由于我不明白的原因,这包括.wsc文件中的这行代码:

d=d.replace ( /\s/g, "\n");

这会将所有空格字符(包括空格)转换为'\ n'。很难相信空间需要转换为“\ n”。无论如何,我不得不注释掉这一行,以使代码适合我!它确实有效。我已经使用了一段时间没有问题。

来自sha256.wsc文件:

/*
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
 * in FIPS 180-2
 * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for details.
 * Adapted into a WSC for use in classic ASP by Daniel O'Malley
 * (based on an SHA-1 example by Erik Oosterwaal)
 * for use with the Amazon Product Advertising API
 */

直接链接到sha256.wsc文件: https://forums.aws.amazon.com/servlet/JiveServlet/download/9-34858-139271-2601/sha256.wsc

我无法找到官方下载网站。

答案 1 :(得分:1)

看一下microsoft capicom.dll。你可以下载它here

可以找到参考here

另一种选择是使用.net类实现该函数并使其“可见”,以便您可以使用来自经典asp的.net DLL ...

答案 2 :(得分:0)

检查我们如何在此存储库中使用加密算法的javascript实现:https://github.com/ictmanagement/redsysHMAC256_API_ASP

如果你打开这个文件:https://github.com/ictmanagement/redsysHMAC256_API_ASP/blob/master/include/dvim_apiRedsys_VB.asp,你会发现我们如何得到与php function hash_hmac("sha256", $token, $signkey, true)

相同的结果
    '/******  MAC Function ******/
    'recibe String|WordArray , retorna WordArray
    Private Function mac256(ent, key) 
        Dim encWA
        Set encWA = ConvertUtf8StrToWordArray(ent)
        Dim keyWA
        Set keyWA = ConvertUtf8StrToWordArray(key)
        Dim resWA
        Set resWA = CryptoJS.HmacSHA256(encWA, keyWA)
        Set mac256 = resWA
    End Function