Windows Phone 8 MD5
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes; using System.Text;
using System.Windows.Media.Imaging;
namespace XguanjiaMsg
{
/// <summary>
/// MD5 32位加密
/// </summary>
public class MD5CryptoServiceProvider : MD5
{
public MD5CryptoServiceProvider()
: base()
{
}
}
/// <summary>
/// Summary description for MD5.
/// </summary>
public class MD5 : IDisposable
{
/// <summary>
/// Create 加密方法
/// </summary>
/// <param name="hashName"></param>
/// <returns></returns>
static public MD5 Create(string hashName)
{
if (hashName == "MD5")
return new MD5();
else
throw new NotSupportedException();
} static public String GetMd5String(String source)
{
MD5 md = MD5CryptoServiceProvider.Create();
byte[] hash; //Create a new instance of ASCIIEncoding to
//convert the string into an array of Unicode bytes.
UTF8Encoding enc = new UTF8Encoding();
// ASCIIEncoding enc = new ASCIIEncoding(); //Convert the string into an array of bytes.
byte[] buffer = enc.GetBytes(source); //Create the hash value from the array of bytes.
hash = md.ComputeHash(buffer); StringBuilder sb = new StringBuilder();
foreach (byte b in hash)
sb.Append(b.ToString("x2"));
return sb.ToString();
} static public MD5 Create()
{
return new MD5();
} #region base implementation of the MD5
#region constants
private const byte S11 = ;
private const byte S12 = ;
private const byte S13 = ;
private const byte S14 = ;
private const byte S21 = ;
private const byte S22 = ;
private const byte S23 = ;
private const byte S24 = ;
private const byte S31 = ;
private const byte S32 = ;
private const byte S33 = ;
private const byte S34 = ;
private const byte S41 = ;
private const byte S42 = ;
private const byte S43 = ;
private const byte S44 = ;
static private byte[] PADDING = new byte[] {
0x80, , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , ,
, , , , , , , , , , ,
};
#endregion #region F, G, H and I are basic MD5 functions.
static private uint F(uint x, uint y, uint z)
{
return (((x) & (y)) | ((~x) & (z)));
}
static private uint G(uint x, uint y, uint z)
{
return (((x) & (z)) | ((y) & (~z)));
}
static private uint H(uint x, uint y, uint z)
{
return ((x) ^ (y) ^ (z));
}
static private uint I(uint x, uint y, uint z)
{
return ((y) ^ ((x) | (~z)));
}
#endregion #region rotates x left n bits.
/// <summary>
/// rotates x left n bits.
/// </summary>
/// <param name="x"></param>
/// <param name="n"></param>
/// <returns></returns>
static private uint ROTATE_LEFT(uint x, byte n)
{
return (((x) << (n)) | ((x) >> ( - (n))));
}
#endregion #region FF, GG, HH, and II transformations
/// FF, GG, HH, and II transformations
/// for rounds 1, 2, 3, and 4.
/// Rotation is separate from addition to prevent recomputation.
static private void FF(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += F((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
static private void GG(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += G((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
static private void HH(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += H((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
static private void II(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
{
(a) += I((b), (c), (d)) + (x) + (uint)(ac);
(a) = ROTATE_LEFT((a), (s));
(a) += (b);
}
#endregion #region context info
/// <summary>
/// state (ABCD)
/// </summary>
uint[] state = new uint[]; /// <summary>
/// number of bits, modulo 2^64 (lsb first)
/// </summary>
uint[] count = new uint[]; /// <summary>
/// input buffer
/// </summary>
byte[] buffer = new byte[];
#endregion internal MD5()
{
Initialize();
} /// <summary>
/// MD5 initialization. Begins an MD5 operation, writing a new context.
/// </summary>
/// <remarks>
/// The RFC named it "MD5Init"
/// </remarks>
public virtual void Initialize()
{
count[] = count[] = ; // Load magic initialization constants.
state[] = 0x67452301;
state[] = 0xefcdab89;
state[] = 0x98badcfe;
state[] = 0x10325476;
} /// <summary>
/// MD5 block update operation. Continues an MD5 message-digest
/// operation, processing another message block, and updating the
/// context.
/// </summary>
/// <param name="input"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <remarks>The RFC Named it MD5Update</remarks>
protected virtual void HashCore(byte[] input, int offset, int count)
{
int i;
int index;
int partLen; // Compute number of bytes mod 64
index = (int)((this.count[] >> ) & 0x3F); // Update number of bits
if ((this.count[] += (uint)((uint)count << )) < ((uint)count << ))
this.count[]++;
this.count[] += ((uint)count >> ); partLen = - index; // Transform as many times as possible.
if (count >= partLen)
{
Buffer.BlockCopy(input, offset, this.buffer, index, partLen);
Transform(this.buffer, ); for (i = partLen; i + < count; i += )
Transform(input, offset + i); index = ;
}
else
i = ; // Buffer remaining input
Buffer.BlockCopy(input, offset + i, this.buffer, index, count - i);
} /// <summary>
/// MD5 finalization. Ends an MD5 message-digest operation, writing the
/// the message digest and zeroizing the context.
/// </summary>
/// <returns>message digest</returns>
/// <remarks>The RFC named it MD5Final</remarks>
protected virtual byte[] HashFinal()
{
byte[] digest = new byte[];
byte[] bits = new byte[];
int index, padLen; // Save number of bits
Encode(bits, , this.count, , ); // Pad out to 56 mod 64.
index = (int)((uint)(this.count[] >> ) & 0x3f);
padLen = (index < ) ? ( - index) : ( - index);
HashCore(PADDING, , padLen); // Append length (before padding)
HashCore(bits, , ); // Store state in digest
Encode(digest, , state, , ); // Zeroize sensitive information.
count[] = count[] = ;
state[] = ;
state[] = ;
state[] = ;
state[] = ; // initialize again, to be ready to use
Initialize(); return digest;
} /// <summary>
/// MD5 basic transformation. Transforms state based on 64 bytes block.
/// </summary>
/// <param name="block"></param>
/// <param name="offset"></param>
private void Transform(byte[] block, int offset)
{
uint a = state[], b = state[], c = state[], d = state[];
uint[] x = new uint[];
Decode(x, , block, offset, ); // Round 1
FF(ref a, b, c, d, x[], S11, 0xd76aa478); /* 1 */
FF(ref d, a, b, c, x[], S12, 0xe8c7b756); /* 2 */
FF(ref c, d, a, b, x[], S13, 0x242070db); /* 3 */
FF(ref b, c, d, a, x[], S14, 0xc1bdceee); /* 4 */
FF(ref a, b, c, d, x[], S11, 0xf57c0faf); /* 5 */
FF(ref d, a, b, c, x[], S12, 0x4787c62a); /* 6 */
FF(ref c, d, a, b, x[], S13, 0xa8304613); /* 7 */
FF(ref b, c, d, a, x[], S14, 0xfd469501); /* 8 */
FF(ref a, b, c, d, x[], S11, 0x698098d8); /* 9 */
FF(ref d, a, b, c, x[], S12, 0x8b44f7af); /* 10 */
FF(ref c, d, a, b, x[], S13, 0xffff5bb1); /* 11 */
FF(ref b, c, d, a, x[], S14, 0x895cd7be); /* 12 */
FF(ref a, b, c, d, x[], S11, 0x6b901122); /* 13 */
FF(ref d, a, b, c, x[], S12, 0xfd987193); /* 14 */
FF(ref c, d, a, b, x[], S13, 0xa679438e); /* 15 */
FF(ref b, c, d, a, x[], S14, 0x49b40821); /* 16 */ // Round 2
GG(ref a, b, c, d, x[], S21, 0xf61e2562); /* 17 */
GG(ref d, a, b, c, x[], S22, 0xc040b340); /* 18 */
GG(ref c, d, a, b, x[], S23, 0x265e5a51); /* 19 */
GG(ref b, c, d, a, x[], S24, 0xe9b6c7aa); /* 20 */
GG(ref a, b, c, d, x[], S21, 0xd62f105d); /* 21 */
GG(ref d, a, b, c, x[], S22, 0x2441453); /* 22 */
GG(ref c, d, a, b, x[], S23, 0xd8a1e681); /* 23 */
GG(ref b, c, d, a, x[], S24, 0xe7d3fbc8); /* 24 */
GG(ref a, b, c, d, x[], S21, 0x21e1cde6); /* 25 */
GG(ref d, a, b, c, x[], S22, 0xc33707d6); /* 26 */
GG(ref c, d, a, b, x[], S23, 0xf4d50d87); /* 27 */
GG(ref b, c, d, a, x[], S24, 0x455a14ed); /* 28 */
GG(ref a, b, c, d, x[], S21, 0xa9e3e905); /* 29 */
GG(ref d, a, b, c, x[], S22, 0xfcefa3f8); /* 30 */
GG(ref c, d, a, b, x[], S23, 0x676f02d9); /* 31 */
GG(ref b, c, d, a, x[], S24, 0x8d2a4c8a); /* 32 */ // Round 3
HH(ref a, b, c, d, x[], S31, 0xfffa3942); /* 33 */
HH(ref d, a, b, c, x[], S32, 0x8771f681); /* 34 */
HH(ref c, d, a, b, x[], S33, 0x6d9d6122); /* 35 */
HH(ref b, c, d, a, x[], S34, 0xfde5380c); /* 36 */
HH(ref a, b, c, d, x[], S31, 0xa4beea44); /* 37 */
HH(ref d, a, b, c, x[], S32, 0x4bdecfa9); /* 38 */
HH(ref c, d, a, b, x[], S33, 0xf6bb4b60); /* 39 */
HH(ref b, c, d, a, x[], S34, 0xbebfbc70); /* 40 */
HH(ref a, b, c, d, x[], S31, 0x289b7ec6); /* 41 */
HH(ref d, a, b, c, x[], S32, 0xeaa127fa); /* 42 */
HH(ref c, d, a, b, x[], S33, 0xd4ef3085); /* 43 */
HH(ref b, c, d, a, x[], S34, 0x4881d05); /* 44 */
HH(ref a, b, c, d, x[], S31, 0xd9d4d039); /* 45 */
HH(ref d, a, b, c, x[], S32, 0xe6db99e5); /* 46 */
HH(ref c, d, a, b, x[], S33, 0x1fa27cf8); /* 47 */
HH(ref b, c, d, a, x[], S34, 0xc4ac5665); /* 48 */ // Round 4
II(ref a, b, c, d, x[], S41, 0xf4292244); /* 49 */
II(ref d, a, b, c, x[], S42, 0x432aff97); /* 50 */
II(ref c, d, a, b, x[], S43, 0xab9423a7); /* 51 */
II(ref b, c, d, a, x[], S44, 0xfc93a039); /* 52 */
II(ref a, b, c, d, x[], S41, 0x655b59c3); /* 53 */
II(ref d, a, b, c, x[], S42, 0x8f0ccc92); /* 54 */
II(ref c, d, a, b, x[], S43, 0xffeff47d); /* 55 */
II(ref b, c, d, a, x[], S44, 0x85845dd1); /* 56 */
II(ref a, b, c, d, x[], S41, 0x6fa87e4f); /* 57 */
II(ref d, a, b, c, x[], S42, 0xfe2ce6e0); /* 58 */
II(ref c, d, a, b, x[], S43, 0xa3014314); /* 59 */
II(ref b, c, d, a, x[], S44, 0x4e0811a1); /* 60 */
II(ref a, b, c, d, x[], S41, 0xf7537e82); /* 61 */
II(ref d, a, b, c, x[], S42, 0xbd3af235); /* 62 */
II(ref c, d, a, b, x[], S43, 0x2ad7d2bb); /* 63 */
II(ref b, c, d, a, x[], S44, 0xeb86d391); /* 64 */ state[] += a;
state[] += b;
state[] += c;
state[] += d; // Zeroize sensitive information.
for (int i = ; i < x.Length; i++)
x[i] = ;
} /// <summary>
/// Encodes input (uint) into output (byte). Assumes len is
/// multiple of 4.
/// </summary>
/// <param name="output"></param>
/// <param name="outputOffset"></param>
/// <param name="input"></param>
/// <param name="inputOffset"></param>
/// <param name="count"></param>
private static void Encode(byte[] output, int outputOffset, uint[] input, int inputOffset, int count)
{
int i, j;
int end = outputOffset + count;
for (i = inputOffset, j = outputOffset; j < end; i++, j += )
{
output[j] = (byte)(input[i] & 0xff);
output[j + ] = (byte)((input[i] >> ) & 0xff);
output[j + ] = (byte)((input[i] >> ) & 0xff);
output[j + ] = (byte)((input[i] >> ) & 0xff);
}
} /// <summary>
/// Decodes input (byte) into output (uint). Assumes len is
/// a multiple of 4.
/// </summary>
/// <param name="output"></param>
/// <param name="outputOffset"></param>
/// <param name="input"></param>
/// <param name="inputOffset"></param>
/// <param name="count"></param>
static private void Decode(uint[] output, int outputOffset, byte[] input, int inputOffset, int count)
{
int i, j;
int end = inputOffset + count;
for (i = outputOffset, j = inputOffset; j < end; i++, j += )
output[i] = ((uint)input[j]) | (((uint)input[j + ]) << ) | (((uint)input[j + ]) << ) | (((uint)input[j + ]) <<
);
}
#endregion #region expose the same interface as the regular MD5 object protected byte[] HashValue;
protected int State;
public virtual bool CanReuseTransform
{
get
{
return true;
}
} public virtual bool CanTransformMultipleBlocks
{
get
{
return true;
}
}
public virtual byte[] Hash
{
get
{
if (this.State != )
throw new InvalidOperationException();
return (byte[])HashValue.Clone();
}
}
public virtual int HashSize
{
get
{
return HashSizeValue;
}
}
protected int HashSizeValue = ; public virtual int InputBlockSize
{
get
{
return ;
}
}
public virtual int OutputBlockSize
{
get
{
return ;
}
} public void Clear()
{
Dispose(true);
} public byte[] ComputeHash(byte[] buffer)
{
return ComputeHash(buffer, , buffer.Length);
}
public byte[] ComputeHash(byte[] buffer, int offset, int count)
{
Initialize();
HashCore(buffer, offset, count);
HashValue = HashFinal();
return (byte[])HashValue.Clone();
} public byte[] ComputeHash(System.IO.Stream inputStream)
{
Initialize();
int count = ;
byte[] buffer = new byte[];
while ( < (count = inputStream.Read(buffer, , )))
{
HashCore(buffer, , count);
}
HashValue = HashFinal();
return (byte[])HashValue.Clone();
} public int TransformBlock(
byte[] inputBuffer,
int inputOffset,
int inputCount,
byte[] outputBuffer,
int outputOffset
)
{
if (inputBuffer == null)
{
throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < )
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if ((inputCount < ) || (inputCount > inputBuffer.Length))
{
throw new ArgumentException("inputCount");
}
if ((inputBuffer.Length - inputCount) < inputOffset)
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if (this.State == )
{
Initialize();
this.State = ;
} HashCore(inputBuffer, inputOffset, inputCount);
if ((inputBuffer != outputBuffer) || (inputOffset != outputOffset))
{
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
}
return inputCount;
}
public byte[] TransformFinalBlock(
byte[] inputBuffer,
int inputOffset,
int inputCount
)
{
if (inputBuffer == null)
{
throw new ArgumentNullException("inputBuffer");
}
if (inputOffset < )
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if ((inputCount < ) || (inputCount > inputBuffer.Length))
{
throw new ArgumentException("inputCount");
}
if ((inputBuffer.Length - inputCount) < inputOffset)
{
throw new ArgumentOutOfRangeException("inputOffset");
}
if (this.State == )
{
Initialize();
}
HashCore(inputBuffer, inputOffset, inputCount);
HashValue = HashFinal();
byte[] buffer = new byte[inputCount];
Buffer.BlockCopy(inputBuffer, inputOffset, buffer, , inputCount);
this.State = ;
return buffer;
}
#endregion protected virtual void Dispose(bool disposing)
{
if (!disposing)
Initialize();
}
public void Dispose()
{
Dispose(true);
}
}
}
Windows Phone 8 MD5的更多相关文章
- Windows下计算md5值
目录 Windows下计算md5值 1.linux 下计算md5值 2.Windows下计算md5值 Windows下计算md5值 1.linux 下计算md5值 [root@master yl]# ...
- Windows自带MD5 SHA1 SHA256命令行工具
感恩大佬LiuYanYGZ的文章 MyHash 检验工具http://www.zdfans.com/html/4346.html HashMyFiles Hash校验工具http://www.nirs ...
- Windows 系统判断MD5 值的办法
Linux 系统的文件要传到Windows系统里面,传输过程中网络不稳定,为了判断文件是否完整传输,所以就用md5的方式判断是否同一个文件 Linux系统 [root@augusite ~]# md5 ...
- WINDOWS自带md5校验工具
WINDOWS自带的工具certutil.exe, certutil -hashfile chropp.exe MD5; 就可以了
- 利用python计算windows全盘文件md5值的脚本
import hashlib import os import time import configparser import uuid def test_file_md5(file_path): t ...
- Windows命令计算MD5与SHA1/256值
certutil -hashfile file MD5 certutil -hashfile file SHA1 certutil -hashfile file SHA256 示例如下:
- 【windows】【md5】查看文件的md5值
certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile filename SHA256 ...
- windows查看文件MD5值的命令
今天需要,就记录一下. certutil -hashfile filename MD5 certutil -hashfile filename SHA1 certutil -hashfile file ...
- windows powershell校验下载的文件MD5和SHA1值
Windows自带MD5 SHA1 SHA256命令行工具 certutil -hashfile <文件名> <hash类型> 打开windows powershell,进入到 ...
随机推荐
- 提高SQL执行效率
原文地址:http://www.cnblogs.com/hlxs/archive/2012/05/07/2487082.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ...
- HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram
HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram ...
- Android中Dialog的使用
上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用. 1.什么是Dialog? Dialog就是对话框的一种方式! 在Android开发中.我们常常会须要在Android ...
- SICP 1.20经验
1.20 两者之间的主要区别是,使我们明白的操作顺序. 网上找一些答案,都死了扩大. 我们所从事的IT的. 展开搞死人IT实践. 首先考虑应用程序 我们得到 gcd(206, 40) -> gc ...
- BZOJ 2002 HNOI2010 弹飞羊 块
标题效果,LCT解释版本:见 http://blog.csdn.net/popoqqq/article/details/38849471 如今,用一只手滑动块,并再次改写这个问题0.0 块短啊 将进入 ...
- hdu3037(lucas定理)
给定n,m,p 表示<=m个豆子放在n棵树上,一共有多少种方案数, 总的方案书mod p 如果将m个豆子放在n棵树上, 可以使用插板法 得到方案数是C(n+m-1,n-1) 那么将0< ...
- Android_declare-styleable_自己定义控件的属性
1.简单实例 (1).在res/values文件下定义一个attrs.xml文件 <? xml version="1.0" encoding="utf-8" ...
- ubuntu系统下给你的ipad充电(适用于所有ios设备)
用ipad的都知道,当我们的ipad插上电脑的usb端口默认是不能充电的.windows和ubuntu平台解决办法分别如下. windows平台: 安装软件 ai_charger http://eve ...
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
原文:乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pa ...
- 【甘道夫】HBase开发环境搭建过程中可能遇到的异常:No FileSystem for scheme: hdfs
异常: 2014-02-24 12:15:48,507 WARN [Thread-2] util.DynamicClassLoader (DynamicClassLoader.java:<in ...