프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkusAutoUpdate / src / NetSparkle.Shared / Utilities.cs @ d8f5045e

이력 | 보기 | 이력해설 | 다운로드 (5.42 KB)

1
using NetSparkleUpdater.Enums;
2
using System;
3
using System.Collections.Generic;
4
using System.IO;
5
using System.Net;
6
using System.Runtime.InteropServices;
7
using System.Security.Cryptography;
8

    
9
namespace NetSparkleUpdater
10
{
11
    /// <summary>
12
    /// Provides commonly used utility functions.
13
    /// </summary>
14
    public class Utilities
15
    {
16
        /// <summary>
17
        /// Removes trailing 0 components from the given version.
18
        /// </summary>
19
        /// <param name="version">Version object</param>
20
        /// <returns>Version string</returns>
21
        public static string GetVersionString(Version version)
22
        {
23
            if (version.Revision != 0)
24
            {
25
                return version.ToString();
26
            }
27
            if (version.Build != 0)
28
            {
29
                return version.ToString(3);
30
            }
31
            return version.ToString(2);
32
        }
33
        
34
        /// <summary>
35
        /// Signs a file with the given private key.
36
        /// </summary>
37
        /// <param name="fileToSignPath">Path to the file you want to sign</param>
38
        /// <param name="privateKeyFilePath">Path to the private key file</param>
39
        /// <returns>DSA signature as base64 string</returns>
40
        public static string GetDSASignature(string fileToSignPath, string privateKeyFilePath)
41
        {
42
            if (string.IsNullOrEmpty(fileToSignPath) || !File.Exists(fileToSignPath))
43
            {
44
                return null;
45
            }
46
            if (string.IsNullOrEmpty(privateKeyFilePath) || !File.Exists(privateKeyFilePath))
47
            {
48
                return null;
49
            }
50
            var privateKey = File.ReadAllText(privateKeyFilePath);
51
            if (!string.IsNullOrEmpty(privateKey))
52
            {
53
                DSACryptoServiceProvider cryptoProvider = new DSACryptoServiceProvider();
54
                cryptoProvider.FromXmlString(privateKey);
55

    
56
                using (Stream inputStream = File.OpenRead(fileToSignPath))
57
                {
58
                    byte[] hash = null;
59
                    hash = cryptoProvider.SignData(inputStream);
60
                    var dsaSignature = Convert.ToBase64String(hash);
61
                    return dsaSignature;
62
                }
63
            }
64

    
65
            return null;
66
        }
67

    
68
        /// <summary>
69
        /// Creates a <see cref="Uri"/> from a URL string. If the URL is relative, converts it to an absolute URL based on the appcast URL.
70
        /// </summary>
71
        /// <param name="url">relative or absolute URL</param>
72
        /// <param name="appcastURL">URL to appcast</param>
73
        public static Uri GetAbsoluteURL(string url, string appcastURL)
74
        {
75
            return new Uri(new Uri(appcastURL), url);
76
        }
77

    
78
        /// <summary>
79
        /// Convert a number of bytes to a user-readable string
80
        /// </summary>
81
        /// <param name="numBytes">Number of bytes to convert</param>
82
        /// <returns>A string that represents the number of bytes in KB, MB, or GB if numBytes > 1024.
83
        /// If numBytes is less than 1024, returns numBytes.</returns>
84
        public static string NumBytesToUserReadableString(long numBytes)
85
        {
86
            if (numBytes > 1024)
87
            {
88
                double numBytesDecimal = numBytes;
89
                // Put in KB
90
                numBytesDecimal /= 1024;
91
                if (numBytesDecimal > 1024)
92
                {
93
                    // Put in MB
94
                    numBytesDecimal /= 1024;
95
                    if (numBytesDecimal > 1024)
96
                    {
97
                        // Put in GB
98
                        numBytesDecimal /= 1024;
99
                        return numBytesDecimal.ToString("F2") + " GB";
100
                    }
101
                    return numBytesDecimal.ToString("F2") + " MB";
102
                }
103
                return numBytesDecimal.ToString("F2") + " KB";
104
            }
105
            return numBytes.ToString();
106
        }
107

    
108
        // From WalletWasabi:
109
        // https://github.com/zkSNACKs/WalletWasabi/blob/8d42bce976605cca3326ea6c998b2294494900e6/WalletWasabi/Helpers/EnvironmentHelpers.cs
110
        public static string GetFullBaseDirectory()
111
        {
112
#if NETCORE
113
            var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);
114

    
115
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
116
            {
117
                if (!fullBaseDirectory.StartsWith("/"))
118
                {
119
                    fullBaseDirectory = fullBaseDirectory.Insert(0, "/");
120
                }
121
            }
122

    
123
            return fullBaseDirectory;
124
#else
125
            // https://stackoverflow.com/a/837501/3938401
126
            return System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
127
#endif
128
        }
129

    
130
        public static byte[] ConvertStreamToByteArray(Stream stream)
131
        {
132
            // read the data
133
            byte[] data = new byte[stream.Length];
134
            stream.Read(data, 0, data.Length);
135
            return data;
136
        }
137

    
138
        public static bool IsSignatureNeeded(SecurityMode securityMode, bool doesKeyInfoExist)
139
        {
140
            switch (securityMode)
141
            {
142
                case SecurityMode.UseIfPossible:
143
                    // if we have a dsa key, we need a signature
144
                    return doesKeyInfoExist;
145
                case SecurityMode.Strict:
146
                    // we always need a signature
147
                    return true;
148
                case SecurityMode.Unsafe:
149
                    return false;
150
            }
151
            return false;
152
        }
153
    }
154
}
클립보드 이미지 추가 (최대 크기: 500 MB)