Example #1
0
        public static ProtectedString operator +(ProtectedString a, string b)
        {
            ProtectedString psB = new ProtectedString(false, b);

            return(a + psB);
        }
Example #2
0
        public ProtectedString Insert(int iStart, string strInsert)
        {
            if (iStart < 0)
            {
                throw new ArgumentOutOfRangeException("iStart");
            }
            if (strInsert == null)
            {
                throw new ArgumentNullException("strInsert");
            }
            if (strInsert.Length == 0)
            {
                return(this);
            }

            if (!m_bIsProtected)
            {
                return(new ProtectedString(false, ReadString().Insert(
                                               iStart, strInsert)));
            }

            UTF8Encoding utf8 = StrUtil.Utf8;

            char[]          v     = ReadChars(), vNew = null;
            byte[]          pbNew = null;
            ProtectedString ps;

            try
            {
                if (iStart > v.Length)
                {
                    throw new ArgumentOutOfRangeException("iStart");
                }

                char[] vIns = strInsert.ToCharArray();

                vNew = new char[v.Length + vIns.Length];
                Array.Copy(v, 0, vNew, 0, iStart);
                Array.Copy(vIns, 0, vNew, iStart, vIns.Length);
                Array.Copy(v, iStart, vNew, iStart + vIns.Length,
                           v.Length - iStart);

                pbNew = utf8.GetBytes(vNew);
                ps    = new ProtectedString(true, pbNew);

                Debug.Assert(utf8.GetString(pbNew, 0, pbNew.Length) ==
                             ReadString().Insert(iStart, strInsert));
            }
            finally
            {
                MemUtil.ZeroArray <char>(v);
                if (vNew != null)
                {
                    MemUtil.ZeroArray <char>(vNew);
                }
                if (pbNew != null)
                {
                    MemUtil.ZeroByteArray(pbNew);
                }
            }

            return(ps);
        }