-
Notifications
You must be signed in to change notification settings - Fork 4
/
HashX13.cs
72 lines (60 loc) · 2.35 KB
/
HashX13.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HashLib;
namespace Blockcore.NBitcoin.Crypto
{
// this hashing class is not thread safe to use with static instances.
// the hashing objects maintain state during hash calculation.
// to use in a multi threaded environment create a new instance for every hash.
public sealed class HashX13
{
private readonly List<IHash> hashers;
private readonly object hashLock;
private static readonly Lazy<HashX13> SingletonInstance = new Lazy<HashX13>(LazyThreadSafetyMode.PublicationOnly);
public HashX13()
{
this.hashers = new List<IHash>
{
HashFactory.Crypto.SHA3.CreateBlake512(),
HashFactory.Crypto.SHA3.CreateBlueMidnightWish512(),
HashFactory.Crypto.SHA3.CreateGroestl512(),
HashFactory.Crypto.SHA3.CreateSkein512_Custom(),
HashFactory.Crypto.SHA3.CreateJH512(),
HashFactory.Crypto.SHA3.CreateKeccak512(),
HashFactory.Crypto.SHA3.CreateLuffa512(),
HashFactory.Crypto.SHA3.CreateCubeHash512(),
HashFactory.Crypto.SHA3.CreateSHAvite3_512_Custom(),
HashFactory.Crypto.SHA3.CreateSIMD512(),
HashFactory.Crypto.SHA3.CreateEcho512(),
HashFactory.Crypto.SHA3.CreateHamsi512(),
HashFactory.Crypto.SHA3.CreateFugue512(),
};
this.hashLock = new object();
this.Multiplier = 1;
}
public uint Multiplier { get; private set; }
/// <summary>
/// using the instance method is not thread safe.
/// to calling the hashing method in a multi threaded environment use the create() method
/// </summary>
public static HashX13 Instance => SingletonInstance.Value;
public static HashX13 Create()
{
return new HashX13();
}
public uint256 Hash(byte[] input)
{
byte[] buffer = input;
lock (this.hashLock)
{
foreach (IHash hasher in this.hashers)
{
buffer = hasher.ComputeBytes(buffer).GetBytes();
}
}
return new uint256(buffer.Take(32).ToArray());
}
}
}