<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Htc.Vita.Core.Interop;
using Htc.Vita.Core.Log;

namespace Htc.Vita.Core.Diagnosis
{
    public class FileSignatureInfo
    {
        private static readonly Logger Log = Logger.GetInstance(typeof(FileSignatureInfo));

        private readonly X509Certificate _certificate;

        public string IssuerDistinguishedName { get; }
        public string IssuerName { get; }
        public string PublicKey { get; }
        public string SubjectDistinguishedName { get; }
        public string SubjectName { get; }
        public bool Verified { get; }

        private FileSignatureInfo(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return;
            }
            if (!fileInfo.Exists)
            {
                Log.Warn("Can not find " + fileInfo.FullName + " to get certificate");
                return;
            }
            try
            {
                _certificate = X509Certificate.CreateFromSignedFile(fileInfo.FullName);
            }
            catch (Exception)
            {
                Log.Warn("Can not find certificate from file " + fileInfo.FullName);
            }
            if (_certificate != null)
            {
                IssuerDistinguishedName = _certificate.Issuer;
                IssuerName = DistinguishedName.Parse(IssuerDistinguishedName).O;
                SubjectDistinguishedName = _certificate.Subject;
                SubjectName = DistinguishedName.Parse(SubjectDistinguishedName).O;
                PublicKey = _certificate.GetPublicKeyString();
                Verified = Authenticode.IsVerified(fileInfo);
            }<commit_msg>Reduce file signature warning log<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using Htc.Vita.Core.Crypto;
using Htc.Vita.Core.Interop;
using Htc.Vita.Core.Log;

namespace Htc.Vita.Core.Diagnosis
{
    public class FileSignatureInfo
    {
        private static readonly HashSet<string> CachedErrorPathes = new HashSet<string>();
        private static readonly Logger Log = Logger.GetInstance(typeof(FileSignatureInfo));

        private const int ErrorPathCacheTimeInMilli = 1000 * 60 * 60;

        private readonly X509Certificate _certificate;

        public string IssuerDistinguishedName { get; }
        public string IssuerName { get; }
        public string PublicKey { get; }
        public string SubjectDistinguishedName { get; }
        public string SubjectName { get; }
        public bool Verified { get; }

        private FileSignatureInfo(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return;
            }
            if (!fileInfo.Exists)
            {
                Log.Warn("Can not find " + fileInfo.FullName + " to get certificate");
                return;
            }
            try
            {
                _certificate = X509Certificate.CreateFromSignedFile(fileInfo.FullName);
            }
            catch (Exception)
            {
                var key = Sha1.GetInstance().GenerateInHex(
                        fileInfo.FullName + "_" + Util.Convert.ToTimestampInMilli(DateTime.UtcNow) / ErrorPathCacheTimeInMilli
                );
                if (string.IsNullOrEmpty(key))
                {
                    Log.Warn("Can not find certificate from file " + fileInfo.FullName);
                }
                else if (!CachedErrorPathes.Contains(key))
                {
                    Log.Warn("Can not find certificate from file " + fileInfo.FullName);
                    CachedErrorPathes.Add(key);
                }
            }
            if (_certificate != null)
            {
                IssuerDistinguishedName = _certificate.Issuer;
                IssuerName = DistinguishedName.Parse(IssuerDistinguishedName).O;
                SubjectDistinguishedName = _certificate.Subject;
                SubjectName = DistinguishedName.Parse(SubjectDistinguishedName).O;
                PublicKey = _certificate.GetPublicKeyString();
                Verified = Authenticode.IsVerified(fileInfo);
            }
{'fraction_non_alphanumeric': 0.07725714285714286, 'fraction_numerical': 0.006171428571428572, 'mean_word_length': 1.8789473684210527, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2362925', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1078, 'n_words': 233}