C#实现php的hash_hmac函数

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=
        }
    }
}
复制代码

 

posted @   94cool  阅读(3472)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2013-08-06 CRC冗余校验码的介绍和实现
2013-08-06 极速Node.js:来自LinkedIn的10个性能提升秘籍
2012-08-06 Invoke 和 BeginInvoke 的区别
2012-08-06 【转】sqlce 修改表名 列名
2012-08-06 用PHP检测用户是用手机(Mobile)还是电脑(PC)访问网站
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示