forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManifestTokens.cs
More file actions
54 lines (44 loc) · 1.52 KB
/
Copy pathManifestTokens.cs
File metadata and controls
54 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.WinGetSourceCreator
{
using System.Security.Cryptography;
public class ManifestTokens
{
public ManifestTokens()
{
}
public Dictionary<string, string> Tokens { get; private set; } = new Dictionary<string, string>();
public void AddHashToken(string file, string token)
{
if (!token.StartsWith("<") || !token.EndsWith(">"))
{
throw new Exception("Token should be in the form of <TOKEN VALUE>");
}
var hash = HashFile(file);
this.Tokens.Add(token, hash);
}
/// <summary>
/// Gets the hash of the specified file.
/// </summary>
/// <param name="filePath">File path.</param>
/// <returns>Hash of file.</returns>
private static string HashFile(string filePath)
{
if (!File.Exists(filePath))
{
throw new FileNotFoundException(filePath);
}
string hash = string.Empty;
using SHA256 mySHA256 = SHA256.Create();
using FileStream fs = File.OpenRead(filePath);
fs.Position = 0;
byte[] hashValue = mySHA256.ComputeHash(fs);
for (int i = 0; i < hashValue.Length; i++)
{
hash += $"{hashValue[i]:X2}";
}
return hash;
}
}
}