[Adapdev-commits] Adapdev/src/Adapdev.Tests/Cryptography HasherTest.cs,NONE,1.1 CryptoTest.cs,1.4,1.
Status: Beta
Brought to you by:
intesar66
From: Sean M. <int...@us...> - 2006-02-21 04:31:28
|
Update of /cvsroot/adapdev/Adapdev/src/Adapdev.Tests/Cryptography In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9122/src/Adapdev.Tests/Cryptography Modified Files: CryptoTest.cs Added Files: HasherTest.cs Log Message: --- NEW FILE: HasherTest.cs --- using System; using Adapdev.Cryptography; using NUnit.Framework; namespace Adapdev.Tests.Cryptography { /// <summary> /// Summary description for HasherTest. /// </summary> /// [TestFixture] public class HasherTest { private string _text = "some text"; [Test] public void DefaultHash() { string hash = Hasher.Hash(this._text); Assert.IsTrue(Hasher.IsHashEqual(this._text, hash), "Hash is incorrect."); } [Test] public void HashMD5() { Hasher.HashType hashType = Hasher.HashType.MD5; string hash = Hasher.Hash(this._text, hashType); Assert.IsTrue(Hasher.IsHashEqual(this._text, hash, hashType), "Hash is incorrect."); } [Test] public void HashSHA() { Hasher.HashType hashType = Hasher.HashType.SHA; string hash = Hasher.Hash(this._text, hashType); Assert.IsTrue(Hasher.IsHashEqual(this._text, hash, hashType), "Hash is incorrect."); } [Test] public void HashSHA256() { Hasher.HashType hashType = Hasher.HashType.SHA256; string hash = Hasher.Hash(this._text, hashType); Assert.IsTrue(Hasher.IsHashEqual(this._text, hash, hashType), "Hash is incorrect."); } [Test] public void HashSHA384() { Hasher.HashType hashType = Hasher.HashType.SHA384; string hash = Hasher.Hash(this._text, hashType); Assert.IsTrue(Hasher.IsHashEqual(this._text, hash, hashType), "Hash is incorrect."); } [Test] public void HashSHA512() { Hasher.HashType hashType = Hasher.HashType.SHA512; string hash = Hasher.Hash(this._text, hashType); Assert.IsTrue(Hasher.IsHashEqual(this._text, hash, hashType), "Hash is incorrect."); } } } Index: CryptoTest.cs =================================================================== RCS file: /cvsroot/adapdev/Adapdev/src/Adapdev.Tests/Cryptography/CryptoTest.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CryptoTest.cs 16 Nov 2005 07:01:52 -0000 1.4 --- CryptoTest.cs 21 Feb 2006 04:31:17 -0000 1.5 *************** *** 27,30 **** --- 27,31 ---- { this.EncryptDecrypt(this._password8, this._vector8, EncryptionAlgorithm.Des); + this.EncryptDecryptFile(this._password8, this._vector8, EncryptionAlgorithm.Des); } *************** *** 33,36 **** --- 34,38 ---- { this.EncryptDecrypt(this._password16, this._vector8, EncryptionAlgorithm.TripleDes); + this.EncryptDecryptFile(this._password16, this._vector8, EncryptionAlgorithm.TripleDes); } *************** *** 39,42 **** --- 41,45 ---- { this.EncryptDecrypt(this._password16, this._vector16, EncryptionAlgorithm.Rijndael); + this.EncryptDecryptFile(this._password16, this._vector16, EncryptionAlgorithm.Rijndael); } *************** *** 45,48 **** --- 48,52 ---- { this.EncryptDecrypt(EncryptionAlgorithm.Des); + this.EncryptDecryptFile(EncryptionAlgorithm.Des); } *************** *** 51,54 **** --- 55,59 ---- { this.EncryptDecrypt(EncryptionAlgorithm.TripleDes); + this.EncryptDecryptFile(EncryptionAlgorithm.TripleDes); } *************** *** 57,60 **** --- 62,66 ---- { this.EncryptDecrypt(EncryptionAlgorithm.Rijndael); + this.EncryptDecryptFile(EncryptionAlgorithm.Rijndael); } *************** *** 76,79 **** --- 82,102 ---- } + private void EncryptDecryptFile(string key, string vector, EncryptionAlgorithm algorithm) + { + Console.WriteLine("Processing " + algorithm.ToString()); + + // Get the encrypted value + byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", this._text, key, vector, algorithm); + // Display it + //Console.WriteLine("Encrypted: " + Encoding.ASCII.GetString(cipherText)); + // Decrypt + string plainText = Crypto.DecryptFromFile("encrypted.dat", key, vector, algorithm); + // Display it + Console.WriteLine("Decrypted: " + plainText); + + // Decrypted value should be same as original + Assert.AreEqual(this._text, plainText, "Incorrect value decrypted"); + } + private void EncryptDecrypt(EncryptionAlgorithm algorithm) { *************** *** 92,95 **** --- 115,136 ---- Assert.AreEqual(this._text, plainText, "Incorrect value decrypted"); } + + private void EncryptDecryptFile(EncryptionAlgorithm algorithm) + { + Console.WriteLine("Processing " + algorithm.ToString()); + + // Get the encrypted value using the built in key and vector + byte[] cipherText = Crypto.EncryptToFile("encrypted.dat", this._text, algorithm); + // Display it + //Console.WriteLine("Encrypted: " + Encoding.ASCII.GetString(cipherText)); + // Get the decrypted value using the built in key and vector + string plainText = Crypto.DecryptFromFile("encrypted.dat", algorithm); + // Display it + Console.WriteLine("Decrypted: " + plainText); + + // Decrypted value should be same as original + Assert.AreEqual(this._text, plainText, "Incorrect value decrypted"); + } + } } |