C#實現php的hash_hmac函數

來源:互聯網
上載者:User

標籤:

from:http://blog.csdn.net/ciaos/article/details/12618487

 

PHP程式碼範例如下

 <?php

        $res1 = hash_hmac("sha1","signatureString", "secret");
        echo $res1."\n";
        //ee1b654aa861c41fd5813dc365ef106c9801f8f6
        echo base64_encode($res1)."\n";
        //ZWUxYjY1NGFhODYxYzQxZmQ1ODEzZGMzNjVlZjEwNmM5ODAxZjhmNg==

        $res2 = hash_hmac("sha1","signatureString", "secret", true);
        //echo "$res2\n";//binary byte[]
        $data = unpack(‘C*‘, $res2);
        print_r($data);
        /*
        Array
        (
            [1] => 238
            [2] => 27
            [3] => 101
            [4] => 74
            [5] => 168
            [6] => 97
            [7] => 196
            [8] => 31
            [9] => 213
            [10] => 129
            [11] => 61
            [12] => 195
            [13] => 101
            [14] => 239
            [15] => 16
            [16] => 108
            [17] => 152
            [18] => 1
            [19] => 248
            [20] => 246
        )

        */

        echo base64_encode("$res2\n");
        //7htlSqhhxB/VgT3DZe8QbJgB+PYK
?>

C#程式碼範例如下

 using System;

using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
    class Program
    {
        private Object hash_hmac(string signatureString, string secretKey, bool raw_output = false)
        {
            var enc = Encoding.UTF8;
            HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
            hmac.Initialize();

            byte[] buffer = enc.GetBytes(signatureString);
            if (raw_output)
            {
                return hmac.ComputeHash(buffer);
            }
            else
            {
                return BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();
            }
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            String res1 = (String)p.hash_hmac("signatureString", "secret");
            Console.WriteLine(res1);
            //ee1b654aa861c41fd5813dc365ef106c9801f8f6
            Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(res1)));
            //ZWUxYjY1NGFhODYxYzQxZmQ1ODEzZGMzNjVlZjEwNmM5ODAxZjhmNg==

            byte[] ret = (byte[])p.hash_hmac("signatureString", "secret", true);
            for (int i = 0; i < ret.Length; i++) {
                Console.Write(ret[i] + " ");
            }
            //238 27 101 74 168 97 196 31 213 129 61 195 101 239 16 108 152 1 248 246
            Console.WriteLine();

            Console.WriteLine(Convert.ToBase64String(ret));
            //7htlSqhhxB/VgT3DZe8QbJgB+PY=
        }
    }
}

 

C#實現php的hash_hmac函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.