HMACSha1加密方法

HMACSha1加密方法最近开发接口对接方使用 hmacsha1 来进行加密验证 特定找了文章查看 https www cnblogs com yhnet p 12448637 htmlhmacsha1 在很多签名计算中都很常用了 基本分为两种一种是原始直接返回字符串 一种是 baset64 后返回基本使用 base64 的比较多原始版本 regionHMACSH 加密将二进制数据直接转为字符串返回 summary HMACSHA1 加密 summary

最近开发接口对接方使用hmacsha1来进行加密验证,特定找了文章查看。

https://www.cnblogs.com/yhnet/p/12448637.html

基本使用base64的比较多

原始版本

#region HMACSHA1加密 将二进制数据直接转为字符串返回 ///  /// HMACSHA1加密 ///  /// 要加密的原串 ///私钥 /// 
     public static string HMACSHA1Text(string text,string key) { //HMACSHA1加密 HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key); byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(text); byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer); var enText = new StringBuilder(); foreach (byte iByte in hashBytes) { enText.AppendFormat("{0:x2}", iByte); } return enText.ToString(); } #endregion

base64版本

#region HMACSHA1加密 二进制数据转Base64后再返回 ///  /// HMACSHA1加密 ///  /// 要加密的原串 ///私钥 /// 
     public static string HMACSHA1Base64(string text,string key) { //HMACSHA1加密 HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key); byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(text); byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer); return Convert.ToBase64String(hashBytes); } #endregion

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205888.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午4:57
下一篇 2026年3月19日 下午4:57


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号