public void UnicodeBytes() { Assert.AreEqual((byte)105, Test1.GetNonUnicodeBytes() [1], "#N01"); Assert.AreEqual((byte)32, Test1.GetNonUnicodeBytes() [5], "#N02"); Assert.AreEqual((byte)70, Test1.GetUnicodeBytes() [0], "#N03"); Assert.AreEqual((byte)70, Test1.GetNonUnicodeBytes() [0], "#N03b"); Assert.AreEqual((byte)0, Test1.GetUnicodeBytes() [1], "#N03c"); Assert.AreEqual((byte)105, Test1.GetNonUnicodeBytes() [1], "#N03d"); Assert.AreEqual((byte)105, Test1.GetUnicodeBytes() [2], "#N03e"); Assert.AreEqual((byte)114, Test1.GetNonUnicodeBytes() [2], "#N03f"); Assert.AreEqual((byte)0, Test1.GetUnicodeBytes() [3], "#N03g"); Assert.AreEqual((byte)115, Test1.GetNonUnicodeBytes() [3], "#N03h"); Assert.AreEqual((byte)114, Test1.GetUnicodeBytes() [4], "#N03i"); Assert.AreEqual((byte)116, Test1.GetNonUnicodeBytes() [4], "#N03j"); Assert.AreEqual((byte)105, Test1.GetUnicodeBytes() [2], "#N04"); try { byte test = Test1.GetUnicodeBytes() [105]; Assert.Fail("#N05"); } catch (Exception e) { #if TARGET_JVM Assert.IsTrue(typeof(IndexOutOfRangeException).IsAssignableFrom(e.GetType()), "#N06"); #else Assert.AreEqual(typeof(IndexOutOfRangeException), e.GetType(), "#N06"); #endif } }
public void UnicodeBytes() { Assert.Equal((byte)105, _test1.GetNonUnicodeBytes()[1]); Assert.Equal((byte)32, _test1.GetNonUnicodeBytes()[5]); Assert.Equal((byte)70, _test1.GetUnicodeBytes()[0]); Assert.Equal((byte)70, _test1.GetNonUnicodeBytes()[0]); Assert.Equal((byte)0, _test1.GetUnicodeBytes()[1]); Assert.Equal((byte)105, _test1.GetNonUnicodeBytes()[1]); Assert.Equal((byte)105, _test1.GetUnicodeBytes()[2]); Assert.Equal((byte)114, _test1.GetNonUnicodeBytes()[2]); Assert.Equal((byte)0, _test1.GetUnicodeBytes()[3]); Assert.Equal((byte)115, _test1.GetNonUnicodeBytes()[3]); Assert.Equal((byte)114, _test1.GetUnicodeBytes()[4]); Assert.Equal((byte)116, _test1.GetNonUnicodeBytes()[4]); Assert.Equal((byte)105, _test1.GetUnicodeBytes()[2]); try { byte test = _test1.GetUnicodeBytes()[105]; Assert.False(true); } catch (Exception e) { Assert.Equal(typeof(IndexOutOfRangeException), e.GetType()); } }
public void Create() { // SqlString (String) SqlString testString = new SqlString("Test"); Assert.Equal("Test", testString.Value); if (PlatformDetection.IsNotInvariantGlobalization) { // SqlString (String, int) testString = new SqlString("Test", 2057); Assert.Equal(2057, testString.LCID); // SqlString (int, SqlCompareOptions, byte[]) testString = new SqlString(2057, SqlCompareOptions.BinarySort | SqlCompareOptions.IgnoreCase, new byte[2] { 123, 221 }); Assert.Equal(2057, testString.CompareInfo.LCID); // SqlString(string, int, SqlCompareOptions) testString = new SqlString("Test", 2057, SqlCompareOptions.IgnoreNonSpace); Assert.False(testString.IsNull); // SqlString (int, SqlCompareOptions, byte[], bool) testString = new SqlString(2057, SqlCompareOptions.BinarySort, new byte[4] { 100, 100, 200, 45 }, true); Assert.Equal((byte)63, testString.GetNonUnicodeBytes()[0]); testString = new SqlString(2057, SqlCompareOptions.BinarySort, new byte[2] { 113, 100 }, false); Assert.Equal("qd", testString.Value); // SqlString (int, SqlCompareOptions, byte[], int, int) testString = new SqlString(2057, SqlCompareOptions.BinarySort, new byte[2] { 113, 100 }, 0, 2); Assert.False(testString.IsNull); // SqlString (int, SqlCompareOptions, byte[], int, int, bool) testString = new SqlString(2057, SqlCompareOptions.IgnoreCase, new byte[3] { 100, 111, 50 }, 1, 2, false); Assert.Equal("o2", testString.Value); testString = new SqlString(2057, SqlCompareOptions.IgnoreCase, new byte[3] { 123, 111, 222 }, 1, 2, true); Assert.False(testString.IsNull); } }
public void Create() { // SqlString (String) SqlString TestString = new SqlString("Test"); Assert.AreEqual("Test", TestString.Value, "#A01"); // SqlString (String, int) TestString = new SqlString("Test", 2057); Assert.AreEqual(2057, TestString.LCID, "#A02"); // SqlString (int, SqlCompareOptions, byte[]) TestString = new SqlString(2057, SqlCompareOptions.BinarySort | SqlCompareOptions.IgnoreCase, new byte [2] { 123, 221 }); Assert.AreEqual(2057, TestString.CompareInfo.LCID, "#A03"); // SqlString(string, int, SqlCompareOptions) TestString = new SqlString("Test", 2057, SqlCompareOptions.IgnoreNonSpace); Assert.IsTrue(!TestString.IsNull, "#A04"); // SqlString (int, SqlCompareOptions, byte[], bool) TestString = new SqlString(2057, SqlCompareOptions.BinarySort, new byte [4] { 100, 100, 200, 45 }, true); Assert.AreEqual((byte)63, TestString.GetNonUnicodeBytes() [0], "#A05"); TestString = new SqlString(2057, SqlCompareOptions.BinarySort, new byte [2] { 113, 100 }, false); Assert.AreEqual((String)"qd", TestString.Value, "#A06"); // SqlString (int, SqlCompareOptions, byte[], int, int) TestString = new SqlString(2057, SqlCompareOptions.BinarySort, new byte [2] { 113, 100 }, 0, 2); Assert.IsTrue(!TestString.IsNull, "#A07"); // SqlString (int, SqlCompareOptions, byte[], int, int, bool) TestString = new SqlString(2057, SqlCompareOptions.IgnoreCase, new byte [3] { 100, 111, 50 }, 1, 2, false); Assert.AreEqual("o2", TestString.Value, "#A08"); TestString = new SqlString(2057, SqlCompareOptions.IgnoreCase, new byte [3] { 123, 111, 222 }, 1, 2, true); Assert.IsTrue(!TestString.IsNull, "#A09"); }
public static SqlBytes Encrypt(SqlString password) { // Create a strong vector byte[] vector = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; RandomNumberGenerator.Create().GetNonZeroBytes(vector); // Get the password bytes byte[] pwdBytes = password.GetNonUnicodeBytes(); // Add the vector bytes to the password bytes and hash them both at the same time SHA256Managed sha256 = new System.Security.Cryptography.SHA256Managed(); byte[] outputBytes = sha256.ComputeHash(AddBytes(pwdBytes, vector)); // Return the resulting hash, and append the vector again to it so it can extracted later return(new SqlBytes(AddBytes(outputBytes, vector))); }
public static SqlBoolean Verify(SqlString password, SqlBytes hash) { byte[] vector = new byte[16]; byte[] pwdAndHash = new byte[32]; // Split the hash and vector into separate variables Array.Copy(hash.Value, 32, vector, 0, 16); Array.Copy(hash.Value, 0, pwdAndHash, 0, 32); // Get the password bytes that will be tested against the hash byte[] pwdBytes = password.GetNonUnicodeBytes(); // Compute a hash using the password provided, and the vector extracted from the hash SHA256Managed sha256 = new System.Security.Cryptography.SHA256Managed(); byte[] testHash = sha256.ComputeHash(AddBytes(pwdBytes, vector)); // Compare hash values to determine if the password provided matches return(new SqlBoolean(BitConverter.ToString(pwdAndHash) == BitConverter.ToString(testHash))); }
public void UnicodeBytes() { Assert.Equal((byte)105, _test1.GetNonUnicodeBytes()[1]); Assert.Equal((byte)32, _test1.GetNonUnicodeBytes()[5]); Assert.Equal((byte)70, _test1.GetUnicodeBytes()[0]); Assert.Equal((byte)70, _test1.GetNonUnicodeBytes()[0]); Assert.Equal((byte)0, _test1.GetUnicodeBytes()[1]); Assert.Equal((byte)105, _test1.GetNonUnicodeBytes()[1]); Assert.Equal((byte)105, _test1.GetUnicodeBytes()[2]); Assert.Equal((byte)114, _test1.GetNonUnicodeBytes()[2]); Assert.Equal((byte)0, _test1.GetUnicodeBytes()[3]); Assert.Equal((byte)115, _test1.GetNonUnicodeBytes()[3]); Assert.Equal((byte)114, _test1.GetUnicodeBytes()[4]); Assert.Equal((byte)116, _test1.GetNonUnicodeBytes()[4]); Assert.Equal((byte)105, _test1.GetUnicodeBytes()[2]); Assert.Throws <IndexOutOfRangeException>(() => _test1.GetUnicodeBytes()[105]); }