using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; namespace myMethod
{
class lgs
{
static void Main()
{
Console.ReadKey();
} /// <summary>
/// 使用不同的编码格式将 字符串 → 字节数组
/// </summary>
/// <returns></returns>
static void StringToByteArray(string param, out byte[] result)
{
//result = Encoding.ASCII.GetBytes(param);
//result = Encoding.UTF32.GetBytes(param);
result = Encoding.UTF7.GetBytes(param);
} /// <summary>
/// 使用不同的编码格式将 字节数组 → 字符串
/// </summary>
/// <returns></returns>
static void ByteArrayToString(byte[] param, out string result)
{
//result = Encoding.UTF7.GetString(param);
//result = Encoding.Default.GetString(param);
result = Encoding.ASCII.GetString(param);
} /// <summary>
/// 字符串转换为流
/// </summary>
static void StringToStream(string param, out MemoryStream result)
{
byte[] tmpArray = Convert.FromBase64String(param); //相当于将字符串转换为等效的字节数组
result = new MemoryStream(tmpArray);
} /// <summary>
/// 流转换了字符串
/// </summary>
static void StreamToString(MemoryStream param, out string result)
{
//方式一 :param.ToArray() 将流内容写入字节数组
result = Encoding.Default.GetString(param.ToArray()); //方式二:
result = Convert.ToBase64String(param.ToArray());
} /// <summary>
/// 字节数组转换为流
/// </summary>
static void ByteArrayToStream(byte[] param, out MemoryStream result)
{
//方式一:
//result = new MemoryStream(param); //方式二:
result = new MemoryStream();
result.Read(param, , param.Length);
} /// <summary>
/// 流转换为字节数组
/// </summary>
static void SteamToByteArray(MemoryStream param, out byte[] result)
{
//方式一:
result = param.ToArray(); //方式二:
//param.Write(result, 0, (int)param.Length);
} /// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fileName">要计算 MD5 值的文件名和路径</param>
/// <returns>MD5 值16进制字符串</returns>
static string MD5File(string fileName)
{
return HashFile(fileName, "md5");
} /// <summary>
/// 计算文件的 sha1 值
/// </summary>
/// <param name="fileName">要计算 sha1 值的文件名和路径</param>
/// <returns>sha1 值16进制字符串</returns>
static string SHA1File(string fileName)
{
return HashFile(fileName, "sha1");
} /// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fileName">要计算哈希值的文件名和路径</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值16进制字符串</returns>
static string HashFile(string fileName, string algName)
{
if (!File.Exists(fileName))
return string.Empty; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
byte[] hashBytes = HashData(fs, algName);
fs.Close();
return ByteArrayToHexString(hashBytes);
} /// <summary>
/// 计算哈希值
/// </summary>
/// <param name="stream">要计算哈希值的 Stream</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值字节数组</returns>
static byte[] HashData(Stream stream, string algName)
{
HashAlgorithm algorithm;
if (algName == null)
{
throw new ArgumentNullException("algName 不能为 null");
}
if (string.Compare(algName, "sha1", true) == )
{
algorithm = SHA1.Create();
}
else
{
if (string.Compare(algName, "md5", true) != )
{
throw new Exception("algName 只能使用 sha1 或 md5");
}
algorithm = MD5.Create();
}
return algorithm.ComputeHash(stream);
} /// <summary>
/// 字节数组转换为16进制表示的字符串
/// </summary>
static string ByteArrayToHexString(byte[] buf)
{
// ToString(buf) --- 由以连字符分隔的十六进制对构成的字符串,其中每一对表示 value 中对应的元素;例如“7F-2C-4A”。
return BitConverter.ToString(buf).Replace("-", "");
}
}
}
参考:https://www.oschina.net/code/snippet_2256220_53535