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,进入到 ...
随机推荐
- 也说Javascript对象拷贝及疑问
一.浅拷贝 当我们需要将一个对象拷贝至另一个对象时,我们一般会这么实现 function shadowCopy(source,target){ var target=target||{}; for(v ...
- javamail发送邮件(转)
今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题.为了以后使用方便,自己写了段代码,打成jar包,以方便以后使用.呵呵 以下三段代码是我的全部代码,朋友们如果想用,直接 ...
- 恢复js文件在windows默认打开方式
解决办法: 运行 regedit 打开注册表编辑器,定位 "HKEY_CLASSES_ROOT" > ".js" 这一项,双击默认值将数值数据改为&quo ...
- Hibernate 配置详解(11)
hibernate.session_factory_name_is_jndi 配置hibernate.cfg.xml中SessionFactory的name属性是否作为JNDI名称绑定.默认是true ...
- uvaLive5713 次小生成树
uvaLive5713 修建道路使得n个点任意两点之间都可以连通,每个点有都有一定的人口,现在可以免费修一条道路, A是免费修的道路两端结点的人口之和, B的其它不是免费修道路的长度的总和 要求的是A ...
- Codeforces Round#310 div2
C题:这题说的是套娃,如果做题的时候知道是套娃,那就好理解多了 规则1:套娃A可以放到套娃B里面,当且仅当套娃B没有放在其他套娃里面 规则2:套娃A放在套娃B里面,且套娃B没有放在其他套娃里面,那么可 ...
- Coder的利器
Coder的利器记载 工作近三年,使用PC快六年,拥抱Mac整一年,投具器石榴裙三年.14年第一次被同事推荐Everything,开启了JeffJade对工具的折腾之旅,并乐此不疲.时去两年,这必然是 ...
- HTTP请求响应过程 与HTTPS区别
原文:HTTP请求响应过程 与HTTPS区别 HTTP协议学习笔记,基础,干货 HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏 ...
- Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发
原文 Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发 前言 大部份的Android 都具有实体或虚拟的Back键. 因此在处理多页面应用程式时 ...
- Python入门(转)
Python 简洁的语法和对动态输入的支持,再加上解释性语言的本质, 使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发 特点:简单.易学.免费.开源.高层语言.可移植 ...