示例#1
0
        internal AssuredStreamHeader(AssuredStreamCreateArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            args.VerifyArgs("args");

            _fileType = args.FileType;
            _hashType = args.HashType;

            if (args.StrongNameKey == null)
            {
                _fileHash      = new byte[QQnCryptoHelpers.GetHashBits(_hashType) / 8];
                _hashSignature = new byte[0];
            }
            else
            {
                _snk           = args.StrongNameKey;
                _fileHash      = new byte[_snk.HashLength];
                _hashSignature = new byte[_snk.SignatureLength];
                _hashType      = _snk.HashType;
            }
            _guid = args.NullGuid ? Guid.Empty : Guid.NewGuid();
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AssuredSubStream"/> class.
        /// </summary>
        /// <param name="parentStream">The parent stream.</param>
        /// <param name="verificationMode">The verification mode.</param>
        public AssuredSubStream(Stream parentStream, VerificationMode verificationMode)
            : base(parentStream, true)
        {
            AssuredStream signedParent = GetService <AssuredStream>();

            if (signedParent != null)
            {
                _snk      = signedParent.AssemblyStrongNameKey;
                _hashType = signedParent.HashType;
            }
            else
            {
                _hashType = HashType.SHA1;
            }

            _headerPosition = parentStream.Position;

            _streamHash    = new byte[QQnCryptoHelpers.GetHashBits(_hashType) / 8];
            _hashSignature = new byte[(_snk != null) ? _snk.SignatureLength : 0];

            if (parentStream.CanWrite && parentStream.CanSeek && parentStream.Position == parentStream.Length)
            {
                _updating = true;
                WriteHeader();
            }
            else
            {
                QQnBinaryReader br = new QQnBinaryReader(BaseStream);
                _streamHash    = br.ReadBytes(_streamHash.Length);
                _hashSignature = br.ReadBytes(_hashSignature.Length);
                _hashLength    = br.ReadInt64();
                _hashPosition  = BaseStream.Position;

                if (verificationMode != VerificationMode.None)
                {
                    if (_snk != null && !_snk.VerifyHash(_streamHash, _hashSignature))
                    {
                        throw new CryptographicException("Stream hash verification failed");
                    }
                }

                if (verificationMode == VerificationMode.Full)
                {
                    if (!VerifyHash())
                    {
                        throw new CryptographicException("Invalid hash value");
                    }
                }
            }
        }