更新时间:2023-11-28 23:03:40
//给出字符串中的密码
字符串密码= @1234ABCD//字节数组再该字符串的presentation
。字节[] EN codedPassword =新UTF8Encoding()GetBytes会(密码);//需要MD5计算哈希
字节[]哈希=((的HashAlgorithm)CryptoConfig.CreateFromName(MD5))ComputeHash(EN codedPassword)。//字符串重新presentation(类似于UNIX格式)
字符串连接codeD = BitConverter.ToString(散)
//没有破折号
.Replace( - ,的String.Empty)
//变为小写
。降低();// EN codeD中包含你想要的哈希
I use the following C# code to calculate a MD5 hash from a string.
It works well and generates a 32-character hex string like this:
900150983cd24fb0d6963f7d28e17f72
string sSourceData;
byte[] tmpSource;
byte[] tmpHash;
sSourceData = "MySourceData";
//Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);
tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);
// and then convert tmpHash to string...
Is there any way to use code like this to generate a 16-character hex string (or 12-character string)? A 32-character hex string is good but I think it'll be boring for the customer to enter the code!
// given, a password in a string
string password = @"1234abcd";
// byte array representation of that string
byte[] encodedPassword = new UTF8Encoding().GetBytes(password);
// need MD5 to calculate the hash
byte[] hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword);
// string representation (similar to UNIX format)
string encoded = BitConverter.ToString(hash)
// without dashes
.Replace("-", string.Empty)
// make lowercase
.ToLower();
// encoded contains the hash you are wanting