Example #1
0
        /// <summary>
        ///     Create a Threat List.
        /// </summary>
        /// <param name="descriptor">
        ///     A <see cref="ThreatListDescriptor" /> identifying the threat list.
        /// </param>
        /// <param name="state">
        ///     The state, formatted as a hexadecimal encoded string, of the threat list when it was retrieved from the
        ///     Google Safe Browsing API.
        /// </param>
        /// <param name="retrieveDate">
        ///     The date, in Coordinated Universal Time (UTC), the threat list was retrieved from the Google Safe
        ///     Browsing API. If the date is not expressed in UTC, it is converted to it.
        /// </param>
        /// <param name="waitToDate">
        ///     The date, in Coordinated Universal Time (UTC), a client must wait to before retrieving the threat list
        ///     from the Google Safe Browsing API again. If the date is not expressed in UTC, it is converted to it. A
        ///     null reference indicates a client does not need to wait before retrieving the threat list from the
        ///     Google Safe Browsing API again.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="descriptor" /> is a null reference, or if <paramref name="state" /> is a
        ///     null reference.
        /// </exception>
        /// <exception cref="System.FormatException">
        ///     Thrown if <paramref name="state" /> is not hexadecimal encoded.
        /// </exception>
        public ThreatList(ThreatListDescriptor descriptor, string state, DateTime retrieveDate, DateTime?waitToDate)
        {
            Guard.ThrowIf(nameof(descriptor), descriptor).Null();

            this.Descriptor   = descriptor;
            this.RetrieveDate = retrieveDate.ToUniversalTime();
            this.State        = CreateState(state);
            this.WaitToDate   = waitToDate?.ToUniversalTime();

            // <summary>
            //      Create State.
            // </summary>
            string CreateState(string cState)
            {
                // ...
                //
                // Throws an exception if the operation fails.
                var cIsStateHexadecimalEncoded = cState.IsHexadecimalEncoded();

                if (!cIsStateHexadecimalEncoded)
                {
                    var cDetailMessage = $"A state ({cState}) is not hexadecimal encoded.";
                    throw new FormatException(cDetailMessage);
                }

                return(cState);
            }
        }
Example #2
0
        /// <summary>
        ///     Create an Unsafe Threat.
        /// </summary>
        /// <param name="sha256Hash">
        ///     A full SHA256 hash, formatted as a hexadecimal encoded string, identifying the threat.
        /// </param>
        /// <param name="associatedThreatListDescriptor">
        ///     A <see cref="ThreatListDescriptor" /> identifying the <see cref="ThreatList" /> the threat is
        ///     associated with.
        /// </param>
        /// <param name="expirationDate">
        ///     The date, in Coordinated Universal Time (UTC), the threat should be considered unsafe to. If the date
        ///     is not in UTC, it is converted to it.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="sha256Hash" /> is a null reference, or if
        ///     <paramref name="associatedThreatListDescriptor" /> is a null reference.
        /// </exception>
        /// <exception cref="System.FormatException">
        ///     Thrown if <paramref name="sha256Hash" /> is not hexadecimal encoded.
        /// </exception>
        public UnsafeThreat(string sha256Hash, ThreatListDescriptor associatedThreatListDescriptor, DateTime expirationDate)
        {
            Guard.ThrowIf(nameof(associatedThreatListDescriptor), associatedThreatListDescriptor).Null();

            this.AssociatedThreatListDescriptor = associatedThreatListDescriptor;
            this.ExpirationDate = expirationDate.ToUniversalTime();
            this.Sha256Hash     = CreateSha256Hash(sha256Hash);

            // <summary>
            //      Create SHA256 Hash.
            // </summary>
            string CreateSha256Hash(string cSha256Hash)
            {
                // ...
                //
                // Throws an exception if the operation fails.
                var cIsSha256HashHexadecimalEncoded = cSha256Hash.IsHexadecimalEncoded();

                if (!cIsSha256HashHexadecimalEncoded)
                {
                    var cDetailMessage = $"A SHA256 hash ({cSha256Hash}) is not hexadecimal encoded.";
                    throw new FormatException(cDetailMessage);
                }

                return(cSha256Hash);
            }
        }
Example #3
0
 /// <summary>
 ///     Create an Invalid Threat List.
 /// </summary>
 /// <param name="threatListDescriptor">
 ///     A <see cref="ThreatListDescriptor" /> identifying the threat list.
 /// </param>
 /// <returns>
 ///     An invalid threat list.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="threatListDescriptor" /> is a null reference.
 /// </exception>
 public static ThreatList CreateInvalid(ThreatListDescriptor threatListDescriptor)
 {
     // ...
     //
     // Throws an exception if the operation fails.
     return(InvalidThreatListCache.CreateInvalidThreatList(threatListDescriptor));
 }
Example #4
0
 /// <summary>
 ///     Create an Expired Threat List.
 /// </summary>
 /// <param name="threatListDescriptor">
 ///     A <see cref="ThreatListDescriptor" /> identifying the threat list.
 /// </param>
 /// <param name="threatListState">
 ///     The state, formatted as a hexadecimal encoded string, of the threat list when it was retrieved from the
 ///     Google Safe Browsing API.
 /// </param>
 /// <returns>
 ///     An expired threat list.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="threatListDescriptor" /> is a null reference, or if
 ///     <paramref name="threatListState" /> is a null reference.
 /// </exception>
 /// <exception cref="System.FormatException">
 ///     Thrown if <paramref name="threatListState" /> is not hexadecimal encoded.
 /// </exception>
 public static ThreatList CreateExpired(ThreatListDescriptor threatListDescriptor, string threatListState)
 {
     // ...
     //
     // Throws an exception if the operation fails.
     return(new ThreatList(threatListDescriptor, threatListState, DateTime.UtcNow));
 }
        /// <summary>
        ///     Set Threat List's Descriptor.
        /// </summary>
        /// <param name="value">
        ///     A <see cref="ThreatListDescriptor" /> identifying the <see cref="ThreatList" />.
        /// </param>
        /// <returns>
        ///     This threat list builder.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="value" /> is a null reference.
        /// </exception>
        public ThreatListBuilder SetDescriptor(ThreatListDescriptor value)
        {
            Guard.ThrowIf(nameof(value), value).Null();

            this.Descriptor = value;
            return(this);
        }
Example #6
0
            /// <summary>
            ///     Create an Invalid Threat List.
            /// </summary>
            /// <param name="threatListDescriptor">
            ///     A <see cref="ThreatListDescriptor" /> identifying the threat list.
            /// </param>
            /// <returns>
            ///     An invalid threat list.
            /// </returns>
            /// <exception cref="System.ArgumentNullException">
            ///     Thrown if <paramref name="threatListDescriptor" /> is a null reference.
            /// </exception>
            internal static ThreatList CreateInvalidThreatList(ThreatListDescriptor threatListDescriptor)
            {
                Guard.ThrowIf(nameof(threatListDescriptor), threatListDescriptor).Null();

                var threatList = InvalidThreatListCache.Cache.GetOrAdd(threatListDescriptor, tld => {
                    var cThreatList = new ThreatList(tld, InvalidThreatListCache.InvalidThreatListState, DateTime.UtcNow);
                    return(cThreatList);
                });

                return(threatList);
            }
Example #7
0
 /// <summary>
 ///     Create a Threat List.
 /// </summary>
 /// <param name="descriptor">
 ///     A <see cref="ThreatListDescriptor" /> identifying the threat list.
 /// </param>
 /// <param name="state">
 ///     The state, formatted as a hexadecimal encoded string, of the threat list when it was retrieved from the
 ///     Google Safe Browsing API.
 /// </param>
 /// <param name="retrieveDate">
 ///     The date, in Coordinated Universal Time (UTC), the threat list was retrieved from the Google Safe
 ///     Browsing API. If the date is not expressed in UTC, it is converted to it.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="descriptor" /> is a null reference, or if <paramref name="state" /> is a
 ///     null reference.
 /// </exception>
 /// <exception cref="System.FormatException">
 ///     Thrown if <paramref name="state" /> is not hexadecimal encoded.
 /// </exception>
 public ThreatList(ThreatListDescriptor descriptor, string state, DateTime retrieveDate) : this(descriptor, state, retrieveDate, null)
 {
 }