private static void DoCheckFileProperties(FileInfo fileInfo)
            {
                if (fileInfo == null || !fileInfo.Exists)
                {
                    return;
                }

                lock (FileCheckingLock)
                {
                    var checksum = Sha1.GetInstance().GenerateInHex(fileInfo);
                    var version  = GetVersionFrom(checksum);
                    if (!UnknownVersion.Equals(version))
                    {
                        Logger.GetInstance(typeof(DefaultOpenVRManager)).Debug($"{fileInfo.FullName}, sha1: {checksum}, version: {version}");
                        return;
                    }
                    Logger.GetInstance(typeof(DefaultOpenVRManager)).Warn($"{fileInfo.FullName}, sha1: {checksum}, version: {version}");

                    var sourceFileName = fileInfo.FullName;
                    var destFileName   = $"{sourceFileName}_{Convert.ToTimestampInMilli(DateTime.UtcNow)}.bak";
                    try
                    {
                        File.Move(sourceFileName, destFileName);
                    }
                    catch (Exception e)
                    {
                        Logger.GetInstance(typeof(DefaultOpenVRManager)).Error($"Can not move unknown \"{sourceFileName}\" to \"{destFileName}\". {e}");
                    }
                }
            }
            private static bool CheckPathWritable(string path)
            {
                if (!Directory.Exists(path))
                {
                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch (Exception e)
                    {
                        Logger.GetInstance(typeof(DefaultOpenVRManager)).Error($"Can not create \"{path}\", {e.Message}");
                        return(false);
                    }
                }

                var now          = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);
                var testFilePath = Path.Combine(path, $"{Sha1.GetInstance().GenerateInHex(now)}.tmp");

                try
                {
                    File.WriteAllText(testFilePath, now);
                    return(true);
                }
                catch (Exception e)
                {
                    Logger.GetInstance(typeof(DefaultOpenVRManager)).Error($"Can not write file to \"{path}\", {e.Message}");
                }
                return(false);
            }
Esempio n. 3
0
        public static void Default_4_ValidateInHex_WithContent()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            Assert.True(sha1.ValidateInHex("", "da39a3ee5e6b4b0d3255bfef95601890afd80709"));
            Assert.True(sha1.ValidateInHex("123", "40bd001563085fc35165329ea1ff5c5ecbdbbeef"));
        }
Esempio n. 4
0
        public static void Default_2_ValidateInBase64_WithContent()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            Assert.True(sha1.ValidateInBase64("", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="));
            Assert.True(sha1.ValidateInBase64("123", "QL0AFWMIX8NRZTKeof9cXsvbvu8="));
        }
Esempio n. 5
0
 protected override bool OnSetName(string name)
 {
     if (!string.IsNullOrWhiteSpace(name))
     {
         _pipeName = Sha1.GetInstance().GenerateInHex(name);
     }
     return(true);
 }
Esempio n. 6
0
        public static void Default_5_ValidateInAll_WithContent()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            Assert.True(sha1.ValidateInAll("", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="));
            Assert.True(sha1.ValidateInAll("123", "QL0AFWMIX8NRZTKeof9cXsvbvu8="));
            Assert.True(sha1.ValidateInAll("", "da39a3ee5e6b4b0d3255bfef95601890afd80709"));
            Assert.True(sha1.ValidateInAll("123", "40bd001563085fc35165329ea1ff5c5ecbdbbeef"));
        }
Esempio n. 7
0
        public static void Default_1_GenerateInBase64_WithContent()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            var value = sha1.GenerateInBase64("");

            Assert.Equal("2jmj7l5rSw0yVb/vlWAYkK/YBwk=", value);
            var value2 = sha1.GenerateInBase64("123");

            Assert.Equal("QL0AFWMIX8NRZTKeof9cXsvbvu8=", value2);
        }
            private static string GetBinaryFilePath(string platform, string fileName)
            {
                var path = GetApplicationDataPath();

                if (string.IsNullOrWhiteSpace(path))
                {
                    return(string.Empty);
                }
                var path2 = Sha1.GetInstance().GenerateInHex($"{ModuleAssembly.Location}_{NativeApiVersion}_{platform}_{fileName}");

                return(Path.Combine(path, path2, fileName));
            }
        public static void FileVerifierTest_Default_3_Sha1_Hex()
        {
            var filePath = Path.Combine(Path.GetTempPath(), $"__test_{Guid.NewGuid().ToString()}__");
            var fileInfo = new FileInfo(filePath);

            File.WriteAllText(filePath, Guid.NewGuid().ToString());
            var checksum = Sha1.GetInstance().GenerateInHex(fileInfo);
            var verified = FileVerifier.VerifyAsync(fileInfo, fileInfo.Length, checksum, FileVerifier.ChecksumType.Sha1, CancellationToken.None).Result;

            Assert.True(verified);
            try { fileInfo.Delete(); } catch (Exception) { }
        }
Esempio n. 10
0
        public static void Default_3_GenerateInHex_WithContent()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            var value = sha1.GenerateInHex("");

            Assert.Equal("da39a3ee5e6b4b0d3255bfef95601890afd80709", value);
            var value2 = sha1.GenerateInHex("123");

            Assert.Equal("40bd001563085fc35165329ea1ff5c5ecbdbbeef", value2);
        }
Esempio n. 11
0
        public static void Default_4_ValidateInHexAsync_WithFile()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            var path = @"%USERPROFILE%\TestData.Sha1.txt";

            if (!Runtime.Platform.IsWindows)
            {
                path = @"%HOME%/TestData.Sha1.txt";
            }
            var file = new FileInfo(Environment.ExpandEnvironmentVariables(path));

            Assert.True(sha1.ValidateInHexAsync(file, "f5e24078c0936ca78815260e7d58d1a940966eba").Result);
        }
Esempio n. 12
0
        public static void Default_1_GenerateInBase64_WithFile()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            var path = @"%USERPROFILE%\TestData.Sha1.txt";

            if (!Runtime.Platform.IsWindows)
            {
                path = @"%HOME%/TestData.Sha1.txt";
            }
            var file = new FileInfo(Environment.ExpandEnvironmentVariables(path));

            Assert.Equal("9eJAeMCTbKeIFSYOfVjRqUCWbro=", sha1.GenerateInBase64(file));
        }
Esempio n. 13
0
        public static void Default_2_ValidateInBase64Async_WithFile()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
            var path = @"%USERPROFILE%\TestData.Sha1.txt";

            if (!Runtime.Platform.IsWindows)
            {
                path = @"%HOME%/TestData.Sha1.txt";
            }
            var file = new FileInfo(Environment.ExpandEnvironmentVariables(path));

            Assert.True(sha1.ValidateInBase64Async(file, "9eJAeMCTbKeIFSYOfVjRqUCWbro=").Result);
        }
Esempio n. 14
0
        public static void Default_0_GetInstance()
        {
            var sha1 = Sha1.GetInstance();

            Assert.NotNull(sha1);
        }
Esempio n. 15
0
 public Client()
 {
     _pipeName = Sha1.GetInstance().GenerateInHex("");
 }
Esempio n. 16
0
 public Provider()
 {
     _pipeName = Sha1.GetInstance().GenerateInHex("");
 }
        public static async Task <bool> VerifyAsync(
            FileInfo fileInfo,
            long size,
            string checksum,
            ChecksumType checksumType,
            CancellationToken cancellationToken)
        {
            if (fileInfo == null || !fileInfo.Exists || fileInfo.Length != size)
            {
                return(false);
            }

            switch (checksumType)
            {
            case ChecksumType.Md5:

                if (checksum.Length == 32)
                {
                    return(checksum == await Md5.GetInstance()
                           .GenerateInHexAsync(fileInfo, cancellationToken).ConfigureAwait(false));
                }

                if (checksum.Length == 24)
                {
                    return(checksum == await Md5.GetInstance()
                           .GenerateInBase64Async(fileInfo, cancellationToken).ConfigureAwait(false));
                }

                break;

            case ChecksumType.Sha1:

                if (checksum.Length == 40)
                {
                    return(checksum == await Sha1.GetInstance()
                           .GenerateInHexAsync(fileInfo, cancellationToken).ConfigureAwait(false));
                }

                if (checksum.Length == 28)
                {
                    return(checksum == await Sha1.GetInstance()
                           .GenerateInBase64Async(fileInfo, cancellationToken).ConfigureAwait(false));
                }

                break;

            case ChecksumType.Sha256:

                if (checksum.Length == 64)
                {
                    return(checksum == await Sha256.GetInstance()
                           .GenerateInHexAsync(fileInfo, cancellationToken).ConfigureAwait(false));
                }

                if (checksum.Length == 44)
                {
                    return(checksum == await Sha256.GetInstance()
                           .GenerateInBase64Async(fileInfo, cancellationToken).ConfigureAwait(false));
                }

                break;
            }

            throw new NotSupportedException($"Not supported! path: {fileInfo.FullName} size: {size} checksum: {checksum} checksumType: {checksumType}");
        }
Esempio n. 18
0
        private FilePropertiesInfo(FileInfo fileInfo)
        {
            if (fileInfo == null)
            {
                return;
            }
            if (!fileInfo.Exists)
            {
                Logger.GetInstance(typeof(FilePropertiesInfo)).Warn("Can not find " + fileInfo.FullName + " to get properties");
                return;
            }

            X509Certificate certificate = null;

            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))
                {
                    Logger.GetInstance(typeof(FilePropertiesInfo)).Warn("Can not find certificate from file " + fileInfo.FullName);
                }
                else if (!CachedErrorPaths.Contains(key))
                {
                    Logger.GetInstance(typeof(FilePropertiesInfo)).Warn("Can not find certificate from file " + fileInfo.FullName);
                    CachedErrorPaths.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);
            }

            var versionInfo = FileVersionInfo.GetVersionInfo(fileInfo.FullName);

            try
            {
                Version = string.Format(
                    CultureInfo.InvariantCulture,
                    @"{0}.{1}.{2}.{3}",
                    versionInfo.FileMajorPart,
                    versionInfo.FileMinorPart,
                    versionInfo.FileBuildPart,
                    versionInfo.FilePrivatePart
                    );
            }
            catch (Exception)
            {
                Logger.GetInstance(typeof(FilePropertiesInfo)).Warn("Can not find version from file " + fileInfo.FullName);
                Version = "0.0.0.0";
            }
            try
            {
                ProductVersion = string.Format(
                    CultureInfo.InvariantCulture,
                    @"{0}.{1}.{2}.{3}",
                    versionInfo.ProductMajorPart,
                    versionInfo.ProductMinorPart,
                    versionInfo.ProductBuildPart,
                    versionInfo.ProductPrivatePart
                    );
            }
            catch (Exception)
            {
                Logger.GetInstance(typeof(FilePropertiesInfo)).Warn("Can not find product version from file " + fileInfo.FullName);
                ProductVersion = "0.0.0.0";
            }

            if (Verified)
            {
                TimestampList.AddRange(Authenticode.GetTimestampList(fileInfo));
            }
        }