Пример #1
0
        public static string Base64Encode(string text, Settings.Settings settings = null)
        {
            /*
             * finalBytes will contain the following:
             * +---------+-------+------+
             * | Salt    | Salt  | Data |
             * | bytes   | bytes |      |
             * | Length  |       |      |
             * | 2 bytes |       |      |
             * +---------+-------+------+
             */
            if (string.IsNullOrEmpty(text))
            {
                return(text);
            }
            settings ??= new Settings.Settings();

            ushort saltSize = ByteHelper.GetByteSize(settings.SaltSize);

            byte[] bytes      = settings.Encoding.GetBytes(text);
            byte[] finalBytes = new byte[Constants.SHORT_SIZE + saltSize             // Salt
                                         + bytes.Length                              // Data
                                ];
            int n = 0;

            BitConverter.GetBytes(saltSize).CopyTo(finalBytes, n);
            n = Constants.SHORT_SIZE;

            if (saltSize > 0)
            {
                byte[] saltBytes = new byte[saltSize];

                using (IRandomNumberGenerator random = CreateRandomNumberGenerator())
                    random.GetNonZeroBytes(saltBytes);

                saltBytes.CopyTo(finalBytes, n);
                n += saltBytes.Length;
            }

            bytes.CopyTo(finalBytes, n);
            return(Convert.ToBase64String(finalBytes));
        }