Ejemplo n.º 1
0
        public string GenerateRandomPassword()
        {
            var passwordBytes = this._platform.GenerateRandomBytes(32);

            char[] passwordChars = Base64Encoder.EncodeDataToBase64CharArray(passwordBytes);

            string passwordString = new string(passwordChars).Remove(43).Replace("/", "$");
            var    sb             = new StringBuilder();

            for (var i = 0; i != passwordString.Length; ++i)
            {
                sb.Append(passwordString[i]);
                var insertSpace   = (i + 1) % 5 == 0;
                var insertNewLine = (i + 1) % 25 == 0;
                if (insertNewLine)
                {
                    sb.Append(Environment.NewLine);
                }
                else if (insertSpace)
                {
                    sb.Append(" ");
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public static CipherV2 DissectXDSSecText(string xdsSecText, LongRunningOperationContext context)
        {
            try
            {
                var xdsSec = WhiteListXDSSecCharacters(xdsSecText, context);

                if (!xdsSec.StartsWith(XDSSecSlashText, StringComparison.OrdinalIgnoreCase))
                {
                    throw CommonFormatException("The prefix '{0}' is missing.".FormatInvariant(XDSSecSlashText));
                }

                var xdsSecTextV2Base64 = xdsSec.Remove(0, XDSSecSlashText.Length).Replace('$', '/');

                var xdsSecTextV2Bytes = Base64Encoder.DecodeBase64StringToBinary(xdsSecTextV2Base64);
                return(DissectBytesToCipherV2(xdsSecTextV2Bytes));
            }
            catch (Exception e)
            {
                if (e.Message.StartsWith(LocalizableStrings.MsgFormatError))
                {
                    throw;
                }
                throw CommonFormatException(e.Message);
            }
        }
Ejemplo n.º 3
0
        public static XDSSecText CreateXDSSecText(byte[] cipherV2, int length, int breakAfter = 74)
        {
            Guard.NotNull(cipherV2);
            int maxBytesNeeded;

            if (length == -1)
            {
                length         = Base64Encoder.CalculateBase64EncodedLengthInChars(cipherV2.Length);
                maxBytesNeeded = cipherV2.Length;
            }
            else
            {
                maxBytesNeeded = Base64Encoder.SafeEstimateBytesNeededForNBase64Chars(length);
            }
            char[] charsToProduce = new char[length + 4]; // safety margin
            Convert.ToBase64CharArray(cipherV2, 0, Math.Min(cipherV2.Length, maxBytesNeeded), charsToProduce, 0);


            var sb       = new StringBuilder();
            var ellipsis = " ...";

            length = length - ellipsis.Length;
            var charsInLine         = 0;
            var charactersProcessed = 0;

            foreach (var c in XDSSecSlashText)
            {
                sb.Append(c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            foreach (var c in charsToProduce)
            {
                if (length == charactersProcessed++)
                {
                    sb.Append(ellipsis);
                    break;
                }


                sb.Append(c == '/' ? '$' : c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            return(new XDSSecText(sb.ToString()));
        }
Ejemplo n.º 4
0
        public static XDSSecText CreateXDSSecText(CipherV2 cipherV2)
        {
            Guard.NotNull(cipherV2);

            var xdsSecTextV2Bytes = ByteArrays.Concatenate(
                // len			Sum(len)		Start Index
                new[] { CipherV2.Version },                // 1			1				0
                new[] { cipherV2.RoundsExponent.Value },   // 1			2				1
                new[] { cipherV2.PlaintextPadding.Value }, // 1			3				2
                cipherV2.IV16.GetBytes(),                  // 16			19				3
                cipherV2.MACCipher16.GetBytes(),           // 16			35				19
                cipherV2.RandomKeyCipher32.GetBytes(),     // 32			67				35
                cipherV2.MessageCipher.GetBytes()          // len			67 + len		67
                );

            if (xdsSecTextV2Bytes.Length < HeaderLenght)
            {
                throw new Exception("Data cannot be shorter than the required header.");
            }

            var xdsSecTextV2Base64 = Base64Encoder.EncodeDataToBase64CharArray(xdsSecTextV2Bytes);

            var       sb          = new StringBuilder();
            const int breakAfter  = 74;
            var       charsInLine = 0;

            foreach (var c in XDSSecSlashText)
            {
                sb.Append(c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            foreach (var c in xdsSecTextV2Base64)
            {
                sb.Append(c == '/' ? '$' : c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            return(new XDSSecText(sb.ToString()));
        }