将cryptojs与c#hmacsha512匹配

时间:2013-06-28 16:02:40

标签: c# hmac cryptojs

为什么我的HMAC不同?
我在这里创建了一个visual studio 2012解决方案:https://github.com/tonyeung/hmacsha512

我的js

    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js"></script>

    var rawSig = "abc:123:Fri, 28 Jun 2013 15:03:00 GMT";
    var token = "c22b31a2-780b-4ac5-8bd5-1296b05130f4"
    var hash = CryptoJS.HmacSHA512(rawSig, token);
    var sig = hash.toString(CryptoJS.enc.Base64);

我的c#

    public JsonResult hmac(string sig, string token)
    {
        var signature = string.Empty;
        using (var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(token)))
        {
            var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(sig));
            signature = Convert.ToBase64String(hash);
        }

        return Json(signature);
    }

结果:

CryptoJS Result
93de274be49d5120f5fef8f7c7c91982ba3ad7ae4e967ef86bdd73cf9e5e67a133ffb185e6993b33a323aade155173bb6a2ddd61fde19a6d09f40260d732dbe3

C# Result  
k94nS+SdUSD1/vj3x8kZgro6165Oln74a91zz55eZ6Ez/7GF5pk7M6Mjqt4VUXO7ai3dYf3hmm0J9AJg1zLb4w==

1 个答案:

答案 0 :(得分:1)

我需要翻转cryptojs参考

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js"></script>

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>

发现此网站非常有用:http://jokecamp.wordpress.com/2012/10/21/examples-of-creating-base64-hashes-using-hmac-sha256-in-different-languages/