从字符串计算MD5哈希

编程入门 行业动态 更新时间:2024-10-27 22:27:39
本文介绍了从字符串计算MD5哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用下面的C#code从字符串计算MD5哈希值。它运作良好,并产生一个32字符的十六进制字符串是这样的: 900150983cd24fb0d6963f7d28e17f72

字符串sSourceData;字节[] tmpSource;字节[] tmpHash;sSourceData =MySourceData;//创建一个从源数据的字节数组。tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData);tmpHash =新MD5CryptoServiceProvider()ComputeHash(tmpSource)。//然后转换tmpHash串...

有没有办法使用code这样生成一个16字符的十六进制字符串(或12个字符的字符串)? 32个字符的十六进制字符串是好的,但我认为这将是枯燥为客户输入code!

解决方案

//给出字符串中的密码字符串密码= @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

更多推荐

从字符串计算MD5哈希

本文发布于:2023-10-22 22:45:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   哈希

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!