Files
acceptance-tests
data
debian
docs
external
ikvm-native
libgc
llvm
m4
man
mcs
build
class
Accessibility
Commons.Xml.Relaxng
Cscompmgd
CustomMarshalers
Facades
I18N
IBM.Data.DB2
ICSharpCode.SharpZipLib
Microsoft.Build
Microsoft.Build.Engine
Microsoft.Build.Framework
Microsoft.Build.Tasks
Microsoft.Build.Utilities
Microsoft.CSharp
Microsoft.NuGet.Build.Tasks
Microsoft.VisualC
Microsoft.Web.Infrastructure
MicrosoftAjaxLibrary
Mono.Btls.Interface
Mono.C5
Mono.CSharp
Mono.Cairo
Mono.Cecil
Mono.Cecil.Mdb
Mono.CodeContracts
Mono.CompilerServices.SymbolWriter
Mono.Data.Sqlite
Mono.Data.Tds
Mono.Debugger.Soft
Mono.Http
Mono.Management
Mono.Messaging
Mono.Messaging.RabbitMQ
Mono.Options
Mono.Parallel
Mono.Posix
Mono.Profiler.Log
Mono.Runtime.Tests
Mono.Security
Assembly
Documentation
Mono.Math
Mono.Math.Prime
Mono.Math.Prime.Generator
Mono.Security
Mono.Security.Authenticode
Mono.Security.Cryptography
Mono.Security.Interface
Mono.Security.Protocol.Ntlm
Mono.Security.Protocol.Tls
Mono.Security.Protocol.Tls.Handshake
Mono.Security.Protocol.Tls.Handshake.Client
Mono.Security.Protocol.Tls.Handshake.Server
Mono.Security.X509
Mono.Security.X509.Extensions
Mono.Xml
Test
Mono.Math
Mono.Security
Mono.Security.Authenticode
Mono.Security.Cryptography
ARC4ManagedTest.cs
CryptoConvertTest.cs
DiffieHellmanManagedTest.cs
KeyPairPersistenceTest.cs
MD2ManagedTest.cs
MD2Test.cs
MD4ManagedTest.cs
MD4Test.cs
PKCS1Test.cs
PKCS8Test.cs
RSAManagedTest.cs
SHA224ManagedTest.cs
SHA224Test.cs
Mono.Security.Interface
Mono.Security.Protocol.Ntlm
Mono.Security.X509
Mono.Security.X509.Extensions
Makefile
Mono.Security.dll.sources
Mono.Security_test.dll.sources
monodroid_Mono.Security_test.dll.build-failure-exclude.sources
monodroid_Mono.Security_test.dll.exclude.sources
monodroid_Mono.Security_test.dll.sources
monotouch_Mono.Security.dll.sources
monotouch_runtime_Mono.Security.dll.sources
monotouch_tv_Mono.Security.dll.sources
monotouch_tv_runtime_Mono.Security.dll.sources
xammac_Mono.Security.dll.sources
Mono.Security.Win32
Mono.Simd
Mono.Tasklets
Mono.WebBrowser
Mono.XBuild.Tasks
Novell.Directory.Ldap
PEAPI
RabbitMQ.Client
SMDiagnostics
System
System.ComponentModel.Composition.4.5
System.ComponentModel.DataAnnotations
System.Configuration
System.Configuration.Install
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Linq
System.Data.OracleClient
System.Data.Services
System.Data.Services.Client
System.Deployment
System.Design
System.DirectoryServices
System.DirectoryServices.Protocols
System.Drawing
System.Drawing.Design
System.Dynamic
System.EnterpriseServices
System.IO.Compression
System.IO.Compression.FileSystem
System.IdentityModel
System.IdentityModel.Selectors
System.Json
System.Json.Microsoft
System.Management
System.Messaging
System.Net
System.Net.Http
System.Net.Http.Formatting
System.Net.Http.WebRequest
System.Net.Http.WinHttpHandler
System.Numerics
System.Numerics.Vectors
System.Reactive.Core
System.Reactive.Debugger
System.Reactive.Experimental
System.Reactive.Interfaces
System.Reactive.Linq
System.Reactive.Observable.Aliases
System.Reactive.PlatformServices
System.Reactive.Providers
System.Reactive.Runtime.Remoting
System.Reactive.Windows.Forms
System.Reactive.Windows.Threading
System.Reflection.Context
System.Runtime.Caching
System.Runtime.CompilerServices.Unsafe
System.Runtime.DurableInstancing
System.Runtime.Remoting
System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Soap
System.Security
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.Web
System.ServiceProcess
System.Threading.Tasks.Dataflow
System.Transactions
System.Web
System.Web.Abstractions
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Extensions
System.Web.Extensions.Design
System.Web.Http
System.Web.Http.SelfHost
System.Web.Http.WebHost
System.Web.Mobile
System.Web.Mvc3
System.Web.Razor
System.Web.RegularExpressions
System.Web.Routing
System.Web.Services
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
System.Windows
System.Windows.Forms
System.Windows.Forms.DataVisualization
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.XML
System.Xaml
System.Xml.Linq
System.Xml.Serialization
SystemWebTestShim
WebMatrix.Data
WindowsBase
aot-compiler
corlib
dlr
doc
legacy
lib
monodoc
notes
reference-assemblies
referencesource
test-helpers
LICENSE
Makefile
Open.snk
README
ecma.pub
mono.pub
mono.snk
msfinal.pub
reactive.pub
silverlight.pub
winfx.pub
winfx3.pub
docs
errors
ilasm
jay
mcs
nunit24
packages
tests
tools
AUTHORS
COPYING
INSTALL.txt
Makefile
MonoIcon.png
README
ScalableMonoIcon.svg
mkinstalldirs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h

250 lines
8.4 KiB
C#
Raw Normal View History

//
// MD2Test.cs - NUnit Test Cases for MD2 (RFC1319)
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
//
// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.Mono.Security.Cryptography {
// References:
// a. The MD2 Message-Digest Algorithm
// http://www.ietf.org/rfc/rfc1319.txt
// MD2 is a abstract class - so ALL of the test included here wont be tested
// on the abstract class but should be tested in ALL its descendants.
public abstract class MD2Test {
protected MD2 hash;
static void Fail (string msg)
{
Assert.Fail (msg);
}
static void AssertEquals (string msg, int expected, int actual)
{
Assert.AreEqual (expected, actual, msg);
}
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Fail (msg + " -> First array is NULL");
if (array2 == null)
Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
if (array1.Length > 0) {
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
}
Assert.IsTrue (a, msg);
}
// MD2 ("") = 8350e5a3e24c153df2275c9f80692773
[Test]
public void RFC1319_Test1 ()
{
string className = hash.ToString ();
byte[] result = { 0x83, 0x50, 0xe5, 0xa3, 0xe2, 0x4c, 0x15, 0x3d, 0xf2, 0x27, 0x5c, 0x9f, 0x80, 0x69, 0x27, 0x73 };
byte[] input = new byte [0];
string testName = className + " 1";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
// N/A RFC1319_e (testName, hash, input, result);
}
// MD2 ("a") = 32ec01ec4a6dac72c0ab96fb34c0b5d1
[Test]
public void RFC1319_Test2 ()
{
string className = hash.ToString ();
byte[] result = { 0x32, 0xec, 0x01, 0xec, 0x4a, 0x6d, 0xac, 0x72, 0xc0, 0xab, 0x96, 0xfb, 0x34, 0xc0, 0xb5, 0xd1 };
byte[] input = Encoding.Default.GetBytes ("a");
string testName = className + " 2";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("abc") = da853b0d3f88d99b30283a69e6ded6bb
[Test]
public void RFC1319_Test3 ()
{
string className = hash.ToString ();
byte[] result = { 0xda, 0x85, 0x3b, 0x0d, 0x3f, 0x88, 0xd9, 0x9b, 0x30, 0x28, 0x3a, 0x69, 0xe6, 0xde, 0xd6, 0xbb };
byte[] input = Encoding.Default.GetBytes ("abc");
string testName = className + " 3";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("message digest") = ab4f496bfb2a530b219ff33031fe06b0
[Test]
public void RFC1319_Test4 ()
{
string className = hash.ToString ();
byte[] result = { 0xab, 0x4f, 0x49, 0x6b, 0xfb, 0x2a, 0x53, 0x0b, 0x21, 0x9f, 0xf3, 0x30, 0x31, 0xfe, 0x06, 0xb0 };
byte[] input = Encoding.Default.GetBytes ("message digest");
string testName = className + " 4";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("abcdefghijklmnopqrstuvwxyz") = 4e8ddff3650292ab5a4108c3aa47940b
[Test]
public void RFC1319_Test5 ()
{
string className = hash.ToString ();
byte[] result = { 0x4e, 0x8d, 0xdf, 0xf3, 0x65, 0x02, 0x92, 0xab, 0x5a, 0x41, 0x08, 0xc3, 0xaa, 0x47, 0x94, 0x0b };
byte[] input = Encoding.Default.GetBytes ("abcdefghijklmnopqrstuvwxyz");
string testName = className + " 5";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
// da33def2a42df13975352846c30338cd
[Test]
public void RFC1319_Test6 ()
{
string className = hash.ToString ();
byte[] result = { 0xda, 0x33, 0xde, 0xf2, 0xa4, 0x2d, 0xf1, 0x39, 0x75, 0x35, 0x28, 0x46, 0xc3, 0x03, 0x38, 0xcd };
byte[] input = Encoding.Default.GetBytes ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
string testName = className + " 6";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
// MD2 ("123456789012345678901234567890123456789012345678901234567890123456
// 78901234567890") = d5976f79d83d3a0dc9806c3c66f3efd8
[Test]
public void RFC1319_Test7 ()
{
string className = hash.ToString ();
byte[] result = { 0xd5, 0x97, 0x6f, 0x79, 0xd8, 0x3d, 0x3a, 0x0d, 0xc9, 0x80, 0x6c, 0x3c, 0x66, 0xf3, 0xef, 0xd8 };
byte[] input = Encoding.Default.GetBytes ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
string testName = className + " 7";
RFC1319_a (testName, hash, input, result);
RFC1319_b (testName, hash, input, result);
RFC1319_c (testName, hash, input, result);
RFC1319_d (testName, hash, input, result);
RFC1319_e (testName, hash, input, result);
}
public void RFC1319_a (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input);
AssertEquals (testName + ".a.1", result, output);
AssertEquals (testName + ".a.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_b (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] output = hash.ComputeHash (input, 0, input.Length);
AssertEquals (testName + ".b.1", result, output);
AssertEquals (testName + ".b.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_c (string testName, MD2 hash, byte[] input, byte[] result)
{
MemoryStream ms = new MemoryStream (input);
byte[] output = hash.ComputeHash (ms);
AssertEquals (testName + ".c.1", result, output);
AssertEquals (testName + ".c.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_d (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] output = hash.TransformFinalBlock (input, 0, input.Length);
AssertEquals (testName + ".d.1", input, output);
AssertEquals (testName + ".d.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
public void RFC1319_e (string testName, MD2 hash, byte[] input, byte[] result)
{
byte[] copy = new byte [input.Length];
for (int i=0; i < input.Length - 1; i++)
hash.TransformBlock (input, i, 1, copy, i);
byte[] output = hash.TransformFinalBlock (input, input.Length - 1, 1);
AssertEquals (testName + ".e.1", input [input.Length - 1], output [0]);
AssertEquals (testName + ".e.2", result, hash.Hash);
// required or next operation will still return old hash
hash.Initialize ();
}
// none of those values changes for any implementation of MD2
[Test]
public virtual void StaticInfo ()
{
string className = hash.ToString ();
AssertEquals (className + ".HashSize", 128, hash.HashSize);
AssertEquals (className + ".InputBlockSize", 1, hash.InputBlockSize);
AssertEquals (className + ".OutputBlockSize", 1, hash.OutputBlockSize);
}
[Test]
public virtual void Create ()
{
// create the default implementation
HashAlgorithm h = MD2.Create ();
Assert.IsTrue ((h is MD2Managed), "MD2Managed");
// Note: will fail is default is changed in machine.config
}
}
}