[英]How to generate MD5 Hash(32/64 Characters) from an integer
I have gooogled and checked 我已经仔细检查了
How to generate MD5 Hash(32/64 Characters) from an integer?
如何从整数生成MD5哈希(32/64个字符)?
What i got is there are examples for generating MD5 Hash string from a string or from a byte array. 我得到的是从字符串或字节数组生成MD5哈希字符串的示例。 But in my case i need to get MD5 Hash from an integer.
但就我而言,我需要从整数获取MD5哈希值。
I know that GetHashCode()
method is there to get the hash code for an integer. 我知道有
GetHashCode()
方法可以获取整数的哈希码。 But this method is not applicable in my case. 但是这种方法不适用于我的情况。
Do i need to convert integer to a string or to an byte array to get the expected MD5 Hash string ? 我是否需要将整数转换为字符串或字节数组以获取预期的MD5哈希字符串?
Please suggest how can i do this? 请提出我该怎么做?
Something like this: 像这样:
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);
First you need to convert your integer into a byte array then you can do this: 首先,您需要将整数转换为字节数组,然后可以执行以下操作:
byte[] hashValue;
using (var md5 = MD5.Create())
{
hashValue = md5.ComputeHash(BitConverter.GetBytes(5));
}
If you want to know what is the md5 hash of "meaning of life" you can 如果您想知道“生命的意义”的md5哈希值是什么,您可以
int meaningOfLife = 42;
var result = CalculateMD5Hash(""+meaningOfLife);
This assumes you can 假设您可以
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();
}
You can try this, 你可以试试看
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();
Thank you All.After referring all the answers, here i am posting my answer which is containg Generic methods for Generating "MD5 Hash(32/64 Characters) from an integer/string/byte array". 谢谢大家。参考所有答案之后,我在这里发布我的答案,其中包含用于从整数/字符串/字节数组生成“ MD5哈希(32/64个字符)的通用方法”。 Might be helpful for others.
可能对别人有帮助。
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();
}
}
}
Modify / any better solution for this code is always welcome. 总是欢迎修改/对此代码有任何更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.