System.Security.SecureString.InsertAt C# (CSharp) Method

InsertAt() public method

public InsertAt ( int index, char c ) : void
index int
c char
return void
		public void InsertAt (int index, char c)
		{
			if (disposed)
				throw new ObjectDisposedException ("SecureString");
			if (read_only) {
				throw new InvalidOperationException (Locale.GetText (
					"SecureString is read-only."));
			}
			if ((index < 0) || (index > length))
				throw new ArgumentOutOfRangeException ("index", "< 0 || > length");
			// insert increments length
			if (length >= MaxSize) {
				string msg = Locale.GetText ("Maximum string size is '{0}'.", MaxSize);
				throw new ArgumentOutOfRangeException ("index", msg);
			}

			try {
				Decrypt ();
				Alloc (++length, true);
				int n = index * 2;
				Buffer.BlockCopy (data, n, data, n + 2, data.Length - n - 2);
				data[n++] = (byte) (c >> 8);
				data[n] = (byte) c;
			}
			finally {
				Encrypt ();
			}
		}

Usage Example

Example #1
0
		public unsafe void UnsafeConstructor ()
		{
			try {
				SecureString ss = null;
				char[] data = new char[] { 'a', 'b', 'c' };
				fixed (char* p = &data[0]) {
					ss = new SecureString (p, data.Length);
				}
				Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
				Assert.AreEqual (3, ss.Length, "3");
				ss.AppendChar ('a');
				Assert.AreEqual (4, ss.Length, "4");
				ss.Clear ();
				Assert.AreEqual (0, ss.Length, "0b");
				ss.InsertAt (0, 'b');
				Assert.AreEqual (1, ss.Length, "1b");
				ss.SetAt (0, 'c');
				Assert.AreEqual (1, ss.Length, "1c");
				ss.RemoveAt (0);
				Assert.AreEqual (0, ss.Length, "0c");
				ss.Dispose ();
			}
			catch (NotSupportedException) {
				Assert.Ignore (NotSupported);
			}
		}
All Usage Examples Of System.Security.SecureString::InsertAt