Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从整数生成MD5哈希(32/64个字符)_C#_.net_Hash_Int_Md5 - Fatal编程技术网

C# 如何从整数生成MD5哈希(32/64个字符)

C# 如何从整数生成MD5哈希(32/64个字符),c#,.net,hash,int,md5,C#,.net,Hash,Int,Md5,我已经用谷歌搜索和检查过了 如何从整数生成MD5哈希(32/64个字符) 我得到的是从字符串或字节数组生成MD5哈希字符串的示例。但在我的例子中,我需要从整数中获取MD5哈希 我知道GetHashCode()方法用于获取整数的哈希代码。但这种方法不适用于我的情况 我是否需要将整数转换为字符串或字节数组以获得预期的MD5哈希字符串 请建议我怎么做?你可以试试这个 int intValue = ; // your value byte[] intBytes = BitConverter.GetByt

我已经用谷歌搜索和检查过了

如何从整数生成MD5哈希(32/64个字符)

我得到的是从字符串或字节数组生成MD5哈希字符串的示例。但在我的例子中,我需要从整数中获取MD5哈希

我知道
GetHashCode()
方法用于获取整数的哈希代码。但这种方法不适用于我的情况

我是否需要将整数转换为字符串或字节数组以获得预期的MD5哈希字符串

请建议我怎么做?

你可以试试这个

int intValue = ; // your value
byte[] intBytes = BitConverter.GetBytes(intValue);
Array.Reverse(intBytes);
byte[] result = intBytes; // you are most probably working on a little-endian machine
byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(result);

// string representation (similar to UNIX format)
string encoded = BitConverter.ToString(hash)
   // without dashes
   .Replace("-", string.Empty)
   // make lowercase
   .ToLower();
你可以试试这个

int intValue = ; // your value
byte[] intBytes = BitConverter.GetBytes(intValue);
Array.Reverse(intBytes);
byte[] result = intBytes; // you are most probably working on a little-endian machine
byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(result);

// string representation (similar to UNIX format)
string encoded = BitConverter.ToString(hash)
   // without dashes
   .Replace("-", string.Empty)
   // make lowercase
   .ToLower();

首先需要将整数转换为字节数组,然后可以执行以下操作:

byte[] hashValue;
using (var md5 = MD5.Create())
{
    hashValue = md5.ComputeHash(BitConverter.GetBytes(5));
}

首先需要将整数转换为字节数组,然后可以执行以下操作:

byte[] hashValue;
using (var md5 = MD5.Create())
{
    hashValue = md5.ComputeHash(BitConverter.GetBytes(5));
}

如果你想知道“生命的意义”的md5散列是什么,你可以

这假设您可以

public string CalculateMD5Hash(string input)
{
    // step 1, calculate MD5 hash from input
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hash = md5.ComputeHash(inputBytes);

    // step 2, convert byte array to hex string
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
}
公共字符串计算emd5hash(字符串输入)
{
//步骤1,从输入计算MD5散列
MD5 MD5=System.Security.Cryptography.MD5.Create();
byte[]inputBytes=System.Text.Encoding.ASCII.GetBytes(输入);
byte[]hash=md5.ComputeHash(inputBytes);
//步骤2,将字节数组转换为十六进制字符串
StringBuilder sb=新的StringBuilder();
for(int i=0;i
如果你想知道“生命的意义”的md5哈希是什么,你可以

这假设您可以

public string CalculateMD5Hash(string input)
{
    // step 1, calculate MD5 hash from input
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
    byte[] hash = md5.ComputeHash(inputBytes);

    // step 2, convert byte array to hex string
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
}
公共字符串计算emd5hash(字符串输入)
{
//步骤1,从输入计算MD5散列
MD5 MD5=System.Security.Cryptography.MD5.Create();
byte[]inputBytes=System.Text.Encoding.ASCII.GetBytes(输入);
byte[]hash=md5.ComputeHash(inputBytes);
//步骤2,将字节数组转换为十六进制字符串
StringBuilder sb=新的StringBuilder();
for(int i=0;i
类似这样的内容:

  int source = 123;
  String hash;

  // Do not omit "using" - should be disposed
  using (var md5 = System.Security.Cryptography.MD5.Create()) 
  {
    hash = String.Concat(md5.ComputeHash(BitConverter
      .GetBytes(source))
      .Select(x => x.ToString("x2")));
  }

  // Test
  // d119fabe038bc5d0496051658fd205e6
  Console.Write(hash);
大概是这样的:

  int source = 123;
  String hash;

  // Do not omit "using" - should be disposed
  using (var md5 = System.Security.Cryptography.MD5.Create()) 
  {
    hash = String.Concat(md5.ComputeHash(BitConverter
      .GetBytes(source))
      .Select(x => x.ToString("x2")));
  }

  // Test
  // d119fabe038bc5d0496051658fd205e6
  Console.Write(hash);

谢谢大家。在参考了所有答案之后,我在这里发布了我的答案,其中包含了从整数/字符串/字节数组生成“MD5哈希(32/64个字符)”的通用方法。可能对其他人有帮助

using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;

namespace ConvertIntToHashCodeConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 100;
            Console.WriteLine(GetHashMD5(number.ToString()));
            Console.WriteLine(GetHashStringFromInteger(number));
            Console.Read();
        }
        /// <summary>
        /// Get the Hash Value for MD5 Hash(32 Characters) from an integer
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string GetHashStringFromInteger(int number)
        {
            string hash;
            using (var md5 = System.Security.Cryptography.MD5.Create())
            {
                hash = String.Concat(md5.ComputeHash(BitConverter
                  .GetBytes(number))
                  .Select(x => x.ToString("x2")));
            }
            return hash;
        }

        /// <summary>
        /// Get the Hash Value for sha256 Hash(64 Characters)
        /// </summary>
        /// <param name="data">The Input Data</param>
        /// <returns></returns>
        public static string GetHash256(string data)
        {
            string hashResult = string.Empty;

            if (data != null)
            {
                using (SHA256 sha256 = SHA256Managed.Create())
                {
                    byte[] dataBuffer = Encoding.UTF8.GetBytes(data);
                    byte[] dataBufferHashed = sha256.ComputeHash(dataBuffer);
                    hashResult = GetHashString(dataBufferHashed);
                }
            }
            return hashResult;
        }

        /// <summary>
        /// Get the Hash Value for MD5 Hash(32 Characters)
        /// </summary>
        /// <param name="data">The Input Data</param>
        /// <returns></returns>
        public static string GetHashMD5(string data)
        {
            string hashResult = string.Empty;
            if (data != null)
            {
                using (MD5 md5 = MD5.Create())
                {
                    byte[] dataBuffer = Encoding.UTF8.GetBytes(data);
                    byte[] dataBufferHashed = md5.ComputeHash(dataBuffer);
                    hashResult = GetHashString(dataBufferHashed);
                }
            }
            return hashResult;
        }
        /// <summary>
        /// Get the Encrypted Hash Data
        /// </summary>
        /// <param name="dataBufferHashed">Buffered Hash Data</param>
        /// <returns> Encrypted hash String </returns>
        private static string GetHashString(byte[] dataBufferHashed)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in dataBufferHashed)
            {
                sb.Append(b.ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
使用系统;
使用System.Security.Cryptography;
使用系统文本;
使用System.Linq;
命名空间ConvertIntToHashCodeConsoleApp
{
班级计划
{
静态void Main(字符串[]参数)
{
整数=100;
Console.WriteLine(GetHashMD5(number.ToString());
Console.WriteLine(GetHashStringFromInteger(number));
Console.Read();
}
/// 
///从整数中获取MD5哈希(32个字符)的哈希值
/// 
/// 
/// 
公共静态字符串GetHashStringFromInteger(整数)
{
字符串散列;
使用(var md5=System.Security.Cryptography.md5.Create())
{
hash=String.Concat(md5.ComputeHash(位转换器
.GetBytes(数字))
.选择(x=>x.ToString(“x2”));
}
返回散列;
}
/// 
///获取sha256哈希的哈希值(64个字符)
/// 
///输入数据
/// 
公共静态字符串GetHash256(字符串数据)
{
string hashResult=string.Empty;
如果(数据!=null)
{
使用(SHA256 SHA256=SHA256Managed.Create())
{
byte[]dataBuffer=Encoding.UTF8.GetBytes(数据);
字节[]dataBufferHashed=sha256.ComputeHash(dataBuffer);
hashResult=GetHashString(数据缓冲哈希);
}
}
返回哈希结果;
}
/// 
///获取MD5哈希的哈希值(32个字符)
/// 
///输入数据
/// 
公共静态字符串GetHashMD5(字符串数据)
{
string hashResult=string.Empty;
如果(数据!=null)
{
使用(MD5 MD5=MD5.Create())
{
byte[]dataBuffer=Encoding.UTF8.GetBytes(数据);
字节[]dataBufferHashed=md5.ComputeHash(dataBuffer);
hashResult=GetHashString(数据缓冲哈希);
}
}
返回哈希结果;
}
/// 
///获取加密的哈希数据
/// 
///缓冲哈希数据
///加密哈希字符串
私有静态字符串GetHashString(字节[]数据缓冲哈希)
{
StringBuilder sb=新的StringBuilder();
foreach(数据缓冲哈希中的字节b)
{
某人附加(b.ToString(“X2”));
}
使某人返回字符串();
}
}
}

修改/欢迎为这段代码提供更好的解决方案。

谢谢大家。在参考了所有答案后,我在这里发布了我的答案,其中包含用于“从整数/字符串/字节数组生成MD5哈希(32/64个字符)”的通用方法。可能对其他人有帮助

using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;

namespace ConvertIntToHashCodeConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 100;
            Console.WriteLine(GetHashMD5(number.ToString()));
            Console.WriteLine(GetHashStringFromInteger(number));
            Console.Read();
        }
        /// <summary>
        /// Get the Hash Value for MD5 Hash(32 Characters) from an integer
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string GetHashStringFromInteger(int number)
        {
            string hash;
            using (var md5 = System.Security.Cryptography.MD5.Create())
            {
                hash = String.Concat(md5.ComputeHash(BitConverter
                  .GetBytes(number))
                  .Select(x => x.ToString("x2")));
            }
            return hash;
        }

        /// <summary>
        /// Get the Hash Value for sha256 Hash(64 Characters)
        /// </summary>
        /// <param name="data">The Input Data</param>
        /// <returns></returns>
        public static string GetHash256(string data)
        {
            string hashResult = string.Empty;

            if (data != null)
            {
                using (SHA256 sha256 = SHA256Managed.Create())
                {
                    byte[] dataBuffer = Encoding.UTF8.GetBytes(data);
                    byte[] dataBufferHashed = sha256.ComputeHash(dataBuffer);
                    hashResult = GetHashString(dataBufferHashed);
                }
            }
            return hashResult;
        }

        /// <summary>
        /// Get the Hash Value for MD5 Hash(32 Characters)
        /// </summary>
        /// <param name="data">The Input Data</param>
        /// <returns></returns>
        public static string GetHashMD5(string data)
        {
            string hashResult = string.Empty;
            if (data != null)
            {
                using (MD5 md5 = MD5.Create())
                {
                    byte[] dataBuffer = Encoding.UTF8.GetBytes(data);
                    byte[] dataBufferHashed = md5.ComputeHash(dataBuffer);
                    hashResult = GetHashString(dataBufferHashed);
                }
            }
            return hashResult;
        }
        /// <summary>
        /// Get the Encrypted Hash Data
        /// </summary>
        /// <param name="dataBufferHashed">Buffered Hash Data</param>
        /// <returns> Encrypted hash String </returns>
        private static string GetHashString(byte[] dataBufferHashed)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in dataBufferHashed)
            {
                sb.Append(b.ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
使用系统;
使用System.Security.Cryptography;
使用系统文本;
使用System.Linq;
命名空间ConvertIntToHashCodeConsoleApp
{
班级计划
{
静态void Main(字符串[]参数)
{
整数=100;
Console.WriteLine(GetHashMD5(number.ToString());
Console.WriteLine(GetHashStringFromInteger(number));
Console.Read();
}
/// 
///从整数中获取MD5哈希(32个字符)的哈希值
/// 
/// 
/// 
公共静态字符串GetHashStringFromInteger(整数)
{
字符串散列;
使用(var md5=System.Security.Cryptography.md5.Create())
{
散列=S