forked from MatthewKing/DeviceId
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashDeviceIdFormatter.cs
54 lines (49 loc) · 2.32 KB
/
HashDeviceIdFormatter.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace DeviceId.Formatters
{
/// <summary>
/// An implementation of <see cref="IDeviceIdFormatter"/> that combines the components into a hash.
/// </summary>
public class HashDeviceIdFormatter : IDeviceIdFormatter
{
/// <summary>
/// A function that returns the hash algorithm to use.
/// </summary>
private readonly Func<HashAlgorithm> _hashAlgorithm;
/// <summary>
/// The <see cref="IByteArrayEncoder"/> to use to encode the resulting hash.
/// </summary>
private readonly IByteArrayEncoder _byteArrayEncoder;
/// <summary>
/// Initializes a new instance of the <see cref="HashDeviceIdFormatter"/> class.
/// </summary>
/// <param name="hashAlgorithm">A function that returns the hash algorithm to use.</param>
/// <param name="byteArrayEncoder">The <see cref="IByteArrayEncoder"/> to use to encode the resulting hash.</param>
public HashDeviceIdFormatter(Func<HashAlgorithm> hashAlgorithm, IByteArrayEncoder byteArrayEncoder)
{
_hashAlgorithm = hashAlgorithm ?? throw new ArgumentNullException(nameof(hashAlgorithm));
_byteArrayEncoder = byteArrayEncoder ?? throw new ArgumentNullException(nameof(byteArrayEncoder));
}
/// <summary>
/// Returns the device identifier string created by combining the specified <see cref="IDeviceIdComponent"/> instances.
/// </summary>
/// <param name="components">A sequence containing the <see cref="IDeviceIdComponent"/> instances to combine into the device identifier string.</param>
/// <returns>The device identifier string.</returns>
public string GetDeviceId(IEnumerable<IDeviceIdComponent> components)
{
if (components == null)
{
throw new ArgumentNullException(nameof(components));
}
var value = string.Join(",", components.OrderBy(x => x.Name).Select(x => x.GetValue()));
var bytes = Encoding.UTF8.GetBytes(value);
using var algorithm = _hashAlgorithm.Invoke();
var hash = algorithm.ComputeHash(bytes);
return _byteArrayEncoder.Encode(hash);
}
}
}