MimeKit.MimePart.ComputeContentMd5 C# (CSharp) Method

ComputeContentMd5() public method

Computes the MD5 checksum of the content.
Computes the MD5 checksum of the MIME content in its canonical format and then base64-encodes the result.
/// The is null. ///
public ComputeContentMd5 ( ) : string
return string
		public string ComputeContentMd5 ()
		{
			if (ContentObject == null)
				throw new InvalidOperationException ("Cannot compute Md5 checksum without a ContentObject.");

			using (var stream = ContentObject.Open ()) {
				byte[] checksum;

				using (var filtered = new FilteredStream (stream)) {
					if (ContentType.IsMimeType ("text", "*"))
						filtered.Add (new Unix2DosFilter ());

					using (var md5 = MD5.Create ())
						checksum = md5.ComputeHash (filtered);
				}

				var base64 = new Base64Encoder (true);
				var digest = new byte[base64.EstimateOutputLength (checksum.Length)];
				int n = base64.Flush (checksum, 0, checksum.Length, digest);

				return Encoding.ASCII.GetString (digest, 0, n);
			}
		}

Usage Example

Exemplo n.º 1
0
		public void TestMimePartContentObject ()
		{
			byte[] data = Encoding.ASCII.GetBytes ("abcd");

			// Checksum will be wrong if content is encoded in any way.
			string checksum;
			using (var md5 = MD5.Create ())
				checksum = Convert.ToBase64String (md5.ComputeHash (data));

			var msg = new MimePart ("application", "octet-stream",
				new ContentObject (new MemoryStream (data), ContentEncoding.Binary)
			);

			Assert.AreEqual (checksum, msg.ComputeContentMd5 (), "Content MD5 is wrong");
			Assert.AreEqual (ContentEncoding.Binary, msg.ContentObject.Encoding, "ContentEncoding is wrong");
		}
All Usage Examples Of MimeKit.MimePart::ComputeContentMd5