hash_hmac 在 php 中有 3 个参数,相当于 csharp,javascript

hash_hmac with 3 params in php, equivalent csharp, javascript

描述:

我似乎无法弄清楚 PHP 是如何产生以下内容的:

echo hash_hmac("sha1", "what is this", true);
echo PHP_EOL; // end of line
echo base64_encode(hash_hmac("sha1", "what is this", true));

Link 在线查看 output(需要 copy/paste)。

如文档所述,您需要 datakey 以及适当的 output 来创建 SHA1 HMAC 哈希。

string hash_hmac(string $algo, string $data, string $key [, bool $raw_output = false])

我想要的是使用 csharpjavascript.

创建准确的输出

尝试过:

当我有 datakey 时很好,我可以在 csharp 和 javascript 上生成相同的 SHA1 HMAC 散列。

// PHP
echo base64_encode(hash_hmac("sha1", "data", "key", true));

Link 在线查看 output(需要 copy/paste)。

// CSharp
public static void Main(string[] args)
{
    Console.WriteLine(CreatePhpSha1HmacHash("data", "key"));
}

public static string CreatePhpSha1HmacHash(string data, string key)
{
    if (data == null)
    {
        data = string.Empty;
    }

    var encoding = new System.Text.UTF8Encoding(); // It's UTF-8 for my example

    var keyBytes = encoding.GetBytes(key);
    var dataBytes = encoding.GetBytes(data);

    using (var hmac = new System.Security.Cryptography.HMACSHA1(keyBytes))
    {
        var hash = hmac.ComputeHash(dataBytes);

        return Convert.ToBase64String(hash);
    }
}

Link 在线查看 output(需要 copy/paste)。

// Javascript
var hash = CryptoJS.HmacSHA1('data', 'key');
var base64 = CryptoJS.enc.Base64.stringify(hash);

console.log('Sha1 hmac hash: ' + base64);

Link在线查看output


问题:

当不使用必需的两个参数时,如何创建与描述中显示的 php 示例完全相同的输出?有人可以向我解释 php 在那种情况下在做什么吗?


答案

@GentlemanMax:PHP 将在内部将 TRUE 转换为 STRING,因此 KEY 将转换为 "1" 作为字符串值。当 raw_output 设置为 TRUE 时,它输出原始二进制数据。 FALSE 输出小写十六进制数。


解决方案:

// CSharp
public static void Main(string[] args)
{
    // echo base64_encode(hash_hmac("sha1", "what is this", true));
    // echo base64_encode(hash_hmac("sha1", "what is this", true, false));
    Console.WriteLine(ToBase64EncodedHmacSha1("what is this", "1", false));
    Console.WriteLine(ToBase64EncodedHmacSha1("what is this", "1", false.ToString()));

    // echo base64_encode(hash_hmac("sha1", "what is this", true, true));
    Console.WriteLine(ToBase64EncodedHmacSha1("what is this", "1", true));
    Console.WriteLine(ToBase64EncodedHmacSha1("what is this", "1", true.ToString()));
}

public static string ToBase64EncodedHmacSha1(string data, string key, bool rawOutput = false)
{
    bool result;

    if (bool.TryParse(key, out result))
    {
        key = result ? 1.ToString() : 0.ToString();
    }

    var keyBytes = Encoding.UTF8.GetBytes(key);
    var dataBytes = Encoding.UTF8.GetBytes(data);

    using (var hmac = new HMACSHA1(keyBytes))
    {
        var hash = hmac.ComputeHash(dataBytes);

        if (rawOutput)
        {
            // output: raw binary
            return Convert.ToBase64String(hash);
        }

        // Convert an array of bytes to a string of hex digits.
        var hex = string.Concat(hash.Select(x => x.ToString("x2").ToLower()));

        var hexBytes = Encoding.UTF8.GetBytes(hex);

        // output: lowercase hexits
        return Convert.ToBase64String(hexBytes);
    }
}

Link 在线查看 output(需要 copy/paste)。


// Javascript 
function toBase64EncodedHmacSha1(data, key, rawOutput) {
    // if boolean, cast to string
    if (typeof(key) === 'boolean') {
        key = key ? '1' : '0';
    }

    // optional
    if (typeof(rawOutput) === 'undefined') {
        rawOutput = false;
    }

    // check type
    if (typeof(rawOutput) !== 'boolean') {
        throw new Error('Raw output is Boolean value: true/false');
    }

    var hash = CryptoJS.HmacSHA1(data, key);

    if (rawOutput) {
        // output: raw binary
        return CryptoJS.enc.Base64.stringify(hash);
    }

    var hex = CryptoJS.enc.Hex.stringify(hash);
    var wordArray = CryptoJS.enc.Utf8.parse(hex);

    // output: lowercase hexits
    return CryptoJS.enc.Base64.stringify(wordArray);
}

// echo base64_encode(hash_hmac("sha1", "what is this", true));
// echo base64_encode(hash_hmac("sha1", "what is this", true, false));
console.log(toBase64EncodedHmacSha1('what is this', true));
console.log(toBase64EncodedHmacSha1('what is this', true, false));

// echo base64_encode(hash_hmac("sha1", "what is this", true, true));
console.log(toBase64EncodedHmacSha1('what is this', true, true));

console.log(toBase64EncodedHmacSha1('what is this', true, 'This will throw error'));

Link在线查看output

这里的关键是 PHP 会在内部将 true 转换为一个字符串。在 php 中,true 转换为 "1" 所以

hash_hmac("sha1", "data", true);

完全等同于

hash_hmac("sha1", "data", "1")

如果您不将第 4 个参数传递给 hash_hmac,那么它会以十六进制输出哈希值。这不是您在 c# 或 javascript 中所做的事情。以下是您可以使用的一些等价物:

//PHP
hash_hmac("sha1", "data", true)

将输出与

相同的结果
//JS
var hash = CryptoJS.HmacSHA1('data', "1")
console.log ( CryptoJS.enc.Hex.stringify(hash) ); //Note .Hex instead of .Base64

同样,

//PHP
base64_encode(hash_hmac("sha1", "data", true, true));

等同于做

//JS
var hash = CryptoJS.HmacSHA1('data', "1")
console.log ( CryptoJS.enc.Base64.stringify(hash) );

顺便说一句,PHP 将尝试将 $key 的所有非字符串值转换为字符串。您始终可以通过调用 strval($key) 来查看您实际使用的密钥是什么。