public static void CopyUTF8ToSecureString()

in src/PFXImportPowershell/EncryptionUtilities/Source/SecureStringUtil.cs [161:238]


        public static void CopyUTF8ToSecureString(byte[] utf8Value, SecureString dest)
        {
            if (utf8Value == null)
            {
                throw new ArgumentNullException(nameof(utf8Value));
            }

            if (dest == null)
            {
                throw new ArgumentNullException(nameof(dest));
            }

            int offset = 0;
            dest.Clear();

            while (offset < utf8Value.Length)
            {
                uint currentByte = utf8Value[offset];

                if ((currentByte & 0x80) == 0)
                {
                    // Single-byte character

                    dest.AppendChar((char)currentByte);
                    offset += 1;
                }
                else if ((currentByte & 0xE0) == 0xC0)
                {
                    // Two-byte character

                    if (offset + 1 >= utf8Value.Length ||
                        (utf8Value[offset + 1] & 0xC0) != 0x80)
                    {
                        throw new InvalidDataException("Invalid UTF-8 encoding");
                    }

                    char charToAppend = (char)(((uint)(currentByte & 0x1F)) << 6 |
                        ((uint)(utf8Value[offset + 1] & 0x3F)));
                    dest.AppendChar(charToAppend);
                    offset += 2;
                }
                else if ((currentByte & 0xF0) == 0xE0)
                {
                    // Three-byte character

                    if (offset + 2 >= utf8Value.Length ||
                        (utf8Value[offset + 1] & 0xC0) != 0x80 ||
                        (utf8Value[offset + 2] & 0xC0) != 0x80)
                    {
                        throw new InvalidDataException("Invalid UTF-8 encoding");
                    }

                    char charToAppend = (char)(((uint)(currentByte & 0x0F)) << 12 |
                        ((uint)(utf8Value[offset + 1] & 0x3F)) << 6 |
                        ((uint)(utf8Value[offset + 2] & 0x3F)));

                    dest.AppendChar(charToAppend);
                    offset += 3;
                }
                else
                {
                    // This is not necessarily invalid UTF-8 encoding.
                    // For example, it could be a code point outside the BMP.
                    // Rather, all UTF-8 characters up to 3-byte encoding
                    // are in code point range of 0x0000..0xFFFF, and thus
                    // encode a value that fits into a single UCS-2 character. 
                    // Example: U+1F355 is the "SLICE OF PIZZA" unicode character.
                    //          U+1F355 is UTF-8 encoded as the four-byte sequence F0 9F 8D 95
                    //          This would be valid UTF-8, but fail here.
                    throw new InvalidDataException("Cannot convert UTF-8 characters above 0xFFFF into USC-2");
                }
            }

            if (offset != utf8Value.Length)
            {
                throw new InvalidDataException("Invalid UTF-8 encoding");
            }
        }