Esempio n. 1
0
        /// <summary>
        /// Read the protected string and return it protected with a sequence
        /// of bytes generated by a random stream.
        /// </summary>
        /// <param name="crsRandomSource">Random number source.</param>
        /// <returns>Protected string.</returns>
        public byte[] ReadXorredString(CryptoRandomStream crsRandomSource)
        {
            Debug.Assert(crsRandomSource != null); if (crsRandomSource == null)
            {
                throw new ArgumentNullException("crsRandomSource");
            }

            byte[] pbData = ReadUtf8();
            uint   uLen   = (uint)pbData.Length;

            byte[] randomPad = crsRandomSource.GetRandomBytes(uLen);
            Debug.Assert(randomPad.Length == pbData.Length);

            for (uint i = 0; i < uLen; ++i)
            {
                pbData[i] ^= randomPad[i];
            }

            return(pbData);
        }
Esempio n. 2
0
        /// <summary>
        /// Get the data xorred with bytes from a <c>CryptoRandomStream</c>.
        /// </summary>
        public byte[] ReadXorredData(CryptoRandomStream crsRandomSource)
        {
            if (crsRandomSource == null)
            {
                Debug.Assert(false); throw new ArgumentNullException("crsRandomSource");
            }

            byte[] pbData = ReadData();
            int    cb     = pbData.Length;

            byte[] pbPad = crsRandomSource.GetRandomBytes((uint)cb);
            Debug.Assert(pbPad.Length == cb);

            for (int i = 0; i < cb; ++i)
            {
                pbData[i] ^= pbPad[i];
            }

            MemUtil.ZeroByteArray(pbPad);
            return(pbData);
        }
Esempio n. 3
0
        public override ProtectedString Generate(PwProfile prf, CryptoRandomStream crsRandomSource)
        {
            PwProfile p = InitProfile(prf);

            string sPattern = MapCharsets2Pattern(prf);

            p.Pattern = sPattern;
            int iLength = p.Pattern.Length;

            //Add additionaly specified characters if any
            //Increase overall length of generated password by 1: 1 per set = at least 1 out of the additinaly specified characters
            string sAdditionalChars = GetAdditionalChars(prf);

            if (AddAdditionalChars(p, sAdditionalChars))
            {
                iLength++;
            }

            //Calculate remaining length
            iLength = (int)p.Length - iLength;

            bool bSuccess;

            FinalizePattern(p, sPattern, sAdditionalChars, iLength, out bSuccess);

            if (!bSuccess)
            {
                return(ProtectedString.Empty);
            }

            ProtectedString ps = ProtectedString.Empty;

            if (PwGenerator.Generate(out ps, p, crsRandomSource.GetRandomBytes(32), null) == PwgError.Success)
            {
                return(ps);
            }
            return(ProtectedString.Empty);
        }