/// <summary>
        ///     Add an Unsafe Threat.
        /// </summary>
        /// <param name="value">
        ///     An <see cref="UnsafeThreat" />.
        /// </param>
        /// <returns>
        ///     This full hash response builder.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="value" /> is a null reference.
        /// </exception>
        public FullHashResponseBuilder AddUnsafeThreat(UnsafeThreat value)
        {
            Guard.ThrowIf(nameof(value), value).Null();

            this.UnsafeThreats.Add(value);
            return(this);
        }
        /// <summary>
        ///     Add 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>
        /// <returns>
        ///     This full hash response builder.
        /// </returns>
        /// <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 FullHashResponseBuilder AddUnsafeThreat(string sha256Hash, ThreatListDescriptor associatedThreatListDescriptor, DateTime expirationDate)
        {
            // ...
            //
            // Throws an exception if the operation fails.
            var unsafeThreat = new UnsafeThreat(sha256Hash, associatedThreatListDescriptor, expirationDate);

            this.UnsafeThreats.Add(unsafeThreat);

            return(this);
        }
Example #3
0
        /// <summary>
        ///     Create an Unsafe Threat.
        /// </summary>
        /// <param name="this">
        ///     An <see cref="UnsafeThreatModel" />.
        /// </param>
        /// <returns>
        ///     An <see cref="UnsafeThreat" /> if <paramref name="this" /> is not a null reference. A null reference
        ///     otherwise.
        /// </returns>
        internal static UnsafeThreat AsUnsafeThreat(this UnsafeThreatModel @this)
        {
            UnsafeThreat unsafeThreat = null;

            if (@this != null)
            {
                var associatedThreatListDescriptor = CreateAssociatedThreatListDescriptor(@this);
                var expirationDate = CreateExpirationDate(@this);
                var sha256Hash     = @this.Threat.Sha256Hash.Base64Decode().HexadecimalEncode();
                unsafeThreat          = new UnsafeThreat(sha256Hash, associatedThreatListDescriptor, expirationDate);
                unsafeThreat.Metadata = CreateMetadata(@this);
            }

            return(unsafeThreat);

            // <summary>
            //      Create Associated Threat List Descriptor.
            // </summary>
            ThreatListDescriptor CreateAssociatedThreatListDescriptor(UnsafeThreatModel cThis)
            {
                var cPlatformType    = cThis.PlatformType.AsPlatformType();
                var cThreatEntryType = cThis.ThreatEntryType.AsThreatEntryType();
                var cThreatType      = cThis.ThreatType.AsThreatType();
                var cAssociatedThreatListDescriptor = new ThreatListDescriptor(cThreatType, cPlatformType, cThreatEntryType);

                return(cAssociatedThreatListDescriptor);
            }

            // <summary>
            //      Create Expiration Date.
            // </summary>
            DateTime CreateExpirationDate(UnsafeThreatModel cThis)
            {
                var cExpirationDate = DateTime.UtcNow;

                if (cThis.CacheDuration != null)
                {
                    var cCacheDuration         = cThis.CacheDuration.Substring(0, cThis.CacheDuration.Length - 1);
                    var cIsCacheDurationParsed = double.TryParse(cCacheDuration, out var cCacheDurationDouble);
                    if (cIsCacheDurationParsed)
                    {
                        cExpirationDate = DateTime.UtcNow.AddSeconds(cCacheDurationDouble);
                    }
                }

                return(cExpirationDate);
            }

            Dictionary <string, string> CreateMetadata(UnsafeThreatModel cThis)
            {
                var cMetadata = new Dictionary <string, string>();

                if (cThis.Metadata?.Entries != null)
                {
                    foreach (var cMetadataEntry in cThis.Metadata.Entries)
                    {
                        var cKey   = cMetadataEntry.Key.Base64Decode().AsciiDecode();
                        var cValue = cMetadataEntry.Value.Base64Decode().AsciiDecode();
                        cMetadata[cKey] = cValue;
                    }
                }

                return(cMetadata);
            }
        }