欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#获取哈希加密生成随机安全码的类实例

程序员文章站 2023-12-12 15:06:52
本文实例讲述了c#获取哈希加密生成随机安全码的类。分享给大家供大家参考。具体分析如下: 这个c#类封装了一些hash加密的功能,可以用于得到随机哈希加密字符串使用非常方便...

本文实例讲述了c#获取哈希加密生成随机安全码的类。分享给大家供大家参考。具体分析如下:

这个c#类封装了一些hash加密的功能,可以用于得到随机哈希加密字符串使用非常方便

using system;
using system.text;
using system.security.cryptography;
namespace dotnet.utilities
{
  /// <summary>
  /// 得到随机安全码(哈希加密)。
  /// </summary>
  public class hashencode
  {
    public hashencode()
    {
      //
      // todo: 在此处添加构造函数逻辑
      //
    }
    /// <summary>
    /// 得到随机哈希加密字符串
    /// </summary>
    /// <returns></returns>
    public static string getsecurity()
    {     
      string security = hashencoding(getrandomvalue());   
      return security;
    }
    /// <summary>
    /// 得到一个随机数值
    /// </summary>
    /// <returns></returns>
    public static string getrandomvalue()
    {     
      random seed = new random();
      string randomvaule = seed.next(1, int.maxvalue).tostring();
      return randomvaule;
    }
    /// <summary>
    /// 哈希加密一个字符串,sharejs.com
    /// </summary>
    /// <param name="security"></param>
    /// <returns></returns>
    public static string hashencoding(string security)
    {           
      byte[] value;
      unicodeencoding code = new unicodeencoding();
      byte[] message = code.getbytes(security);
      sha512managed arithmetic = new sha512managed();
      value = arithmetic.computehash(message);
      security = "";
      foreach(byte o in value)
      {
        security += (int) o + "o";
      }
      return security;
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: