http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3636|回复: 4

[其他] HMAC-SHA1签名方法,在.NET中怎么用

[复制链接]
发表于 2013-11-14 14:24:06 | 显示全部楼层 |阅读模式
HMAC-SHA1签名方法,在网上找了好多、但大多都是PHP的 、请问下 有谁在.ASP.NET写过这个算法呢


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-11-14 14:31:35 | 显示全部楼层
[C#] 纯文本查看 复制代码
using System;

namespace Tamir.SharpSsh.jsch.jce
{
	/* -*-mode:java; c-basic-offset:2; -*- */
	/*
	Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	  1. Redistributions of source code must retain the above copyright notice,
		 this list of conditions and the following disclaimer.

	  2. Redistributions in binary form must reproduce the above copyright 
		 notice, this list of conditions and the following disclaimer in 
		 the documentation and/or other materials provided with the distribution.

	  3. The names of the authors may not be used to endorse or promote products
		 derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
	FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
	INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
	OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
	EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	*/

	public class HMACSHA1 : MAC
	{
		private const String name="hmac-sha1";
		private const int bsize=20;
        private System.Security.Cryptography.HMAC mentalis_mac;
		private System.Security.Cryptography.CryptoStream cs;
		//private Mac mac;
		public int getBlockSize(){return bsize;}
		public void init(byte[] key) 
		{
			if(key.Length>bsize)
			{
				byte[] tmp=new byte[bsize];
				Array.Copy(key, 0, tmp, 0, bsize);	  
				key=tmp;
			}
			//    SecretKeySpec skey=new SecretKeySpec(key, "HmacSHA1");
			//    mac=Mac.getInstance("HmacSHA1");
			//    mac.init(skey);
            mentalis_mac = new System.Security.Cryptography.HMACMD5(key);
            cs = new System.Security.Cryptography.CryptoStream(System.IO.Stream.Null, mentalis_mac, System.Security.Cryptography.CryptoStreamMode.Write);
		} 
		private byte[] tmp=new byte[4];
		public void update(int i)
		{
			tmp[0]=(byte)(i>>24);
			tmp[1]=(byte)(i>>16);
			tmp[2]=(byte)(i>>8);
			tmp[3]=(byte)i;
			update(tmp, 0, 4);
		}
		public void update(byte[] foo, int s, int l)
		{
			//mac.update(foo, s, l);      
			cs.Write(  foo , s, l);
		}
		public byte[] doFinal()
		{
			Console.WriteLine("Sha1");
			//return mac.doFinal();
			cs.Close();
			byte[] result = mentalis_mac.Hash;
			byte[] key = mentalis_mac.Key;
			mentalis_mac.Clear();
			init(key);

			return result;
		}
		public String getName()
		{
			return name;
		}
	}

}
发表于 2013-11-14 14:32:33 | 显示全部楼层
 楼主| 发表于 2013-11-14 14:56:35 | 显示全部楼层
这是我在网上找到的一个参考方法  帮忙看下  有什么需要改正的不

/// <summary>
    /// hmacSha1算法加密(生成长度40)
    /// </summary>
    /// <param name="encryptText">加密明文</param>
    /// <param name="encryptKey">加密密钥</param>
    /// <returns></returns>
    public string hmacSha1(string encryptText, string encryptKey)
    {
        HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.Default.GetBytes(encryptKey));
        byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.Default.GetBytes(encryptText));
        StringBuilder EnText = new StringBuilder();
        foreach (byte Byte in RstRes)
        {
            EnText.AppendFormat("{0:x2}", Byte);
        }
        return EnText.ToString();
    }
 楼主| 发表于 2013-11-14 14:57:17 | 显示全部楼层
站长苏飞 发表于 2013-11-14 14:32
http://sharpssh2.codeplex.com/SourceControl/changeset/view/37212#SharpSSH/SharpSSH/Crypto/MAC/HMACSH ...

这是我在网上找到的一个参考方法  帮忙看下  有什么需要改正的不

/// <summary>
    /// hmacSha1算法加密(生成长度40)
    /// </summary>
    /// <param name="encryptText">加密明文</param>
    /// <param name="encryptKey">加密密钥</param>
    /// <returns></returns>
    public string hmacSha1(string encryptText, string encryptKey)
    {
        HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.Default.GetBytes(encryptKey));
        byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.Default.GetBytes(encryptText));
        StringBuilder EnText = new StringBuilder();
        foreach (byte Byte in RstRes)
        {
            EnText.AppendFormat("{0:x2}", Byte);
        }
        return EnText.ToString();
    }

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-15 18:27

© 2014-2021

快速回复 返回顶部 返回列表