Windows store app Hash(MD5/SHA1/SHA256/SHA384/SHA512)
2015-03-11 16:38
330 查看
public class HashAlgorithm { /// <summary> /// 加密类型 /// </summary> public enum AlgorithmType { MD5=1, SHA1, SHA256, SHA384, SHA512, } /// <summary> /// 返回Hash码的类型 /// </summary> public enum HashType { Base64String = 1, HexString, } /// <summary> /// 将message进行加密,并返回加密后的Hash码 /// </summary> /// <param name="message">待加密信息</param> /// <param name="algorithmType">加密类型,默认为MD5</param> /// <param name="hashType">加密后得到的Hash的类型,默认返回为十六进制字符串</param> /// <returns>加密后得到的Hash</returns> static public String Hash(String message, AlgorithmType algorithmType=AlgorithmType.MD5, HashType hashType=HashType.HexString) { if (string.IsNullOrEmpty(message)) { throw new ArgumentException("Invalid argument"); } String algorithmName = ""; if (AlgorithmType.MD5 == algorithmType) algorithmName = HashAlgorithmNames.Md5;//加密类型/算法 else if (AlgorithmType.SHA1 == algorithmType) algorithmName = HashAlgorithmNames.Sha1;//加密类型/算法 else if (AlgorithmType.SHA256 == algorithmType) algorithmName = HashAlgorithmNames.Sha256;//加密类型/算法 else if (AlgorithmType.SHA384 == algorithmType) algorithmName = HashAlgorithmNames.Sha384;//加密类型/算法 else if (AlgorithmType.SHA512 == algorithmType) algorithmName = HashAlgorithmNames.Sha512;//加密类型/算法 return Hash(message, algorithmName, hashType); } static private String Hash(String message, String algorithmName, HashType hashType) { // 把待加密信息转换成二进制数据(Convert the message string to binary data.) IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8); // 创建HashAlgorithmProvider实例(Create a HashAlgorithmProvider object.) HashAlgorithmProvider algorithmProvider = HashAlgorithmProvider.OpenAlgorithm(algorithmName); // 获取当前加密类型/名称(retrieve the name of the hashing algorithm.) //String algorithmTypeUsed = algorithmProvider.AlgorithmName;//得到加密类型/名称 IBuffer buffHash = algorithmProvider.HashData(buffUtf8Msg);//加密// Hash the message. // Verify that the hash length equals the length specified for the algorithm. if (buffHash.Length != algorithmProvider.HashLength)//校验加密是否成功 { throw new Exception("There was an error creating the hash"); } // 转换成字符串(Convert the hash to a string (for display).) String strHash=""; if (HashType.Base64String == hashType) strHash = CryptographicBuffer.EncodeToBase64String(buffHash); else if (HashType.HexString == hashType) strHash = CryptographicBuffer.EncodeToHexString(buffHash); return strHash;// Return the encoded string } }
测试:
// Hash a message.
String strMsg = "hello world";//待加密信息
String strEncodedHash = HashAlgorithm.Hash(strMsg);//进行加密,加密类型MD5,返回Hash码为十六进制字符串
相关文章推荐
- Windows Store App 加载设计视图提示Package failed updates, dependency or conflict validation. (Exception from HRESULT: 0x80073CF3)解决方法
- [Windows 8小技巧]如何知道Windows 8 Store App大小及可用存储空间
- Windows store app[Part 3]:认识WinRT的异步机制
- windows 8 store app 百度新闻快速浏览器 隐私声明
- Windows store app[Part 4]:深入WinRT的异步机制
- Windows Store apps开发[33]Part 1:使用Grid App (XAML)模板创建第一个Windows 8 app
- 【Windows 8 Store App】学习二:ResourceLoader
- Windows store app[Part 2]:全新的File System与Uri不匹配的问题
- Windows store app[Part 1]:读取U盘数据
- 【Windows 8 Store App】学习:目录
- [Win8Y原创] Windows Store App 开发入门系列
- 【Windows 8 Store App】学习四:关于页面导航
- 【Windows 8 Store App】学习三:HTTP
- Windows store app[Part 2]:全新的File System与Uri不匹配的问题
- windows store app search contract
- [Windows 8小技巧]如何知道Windows 8 Store App大小及可用存储空间
- 【Windows 8 Store App】学习二:ResourceLoader
- 【Windows Store App】 System brushes/fonts/Segoe UI Symbol Icon
- Windows store app[Part 3]:认识WinRT的异步机制
- 【Windows 8 Store App】学习一:获取设备信息