Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MGF1.cs
1using System;
3
5{
10 public class MGF1(HashFunction HashFunction) : MaskGenerationFunction
11 {
12 private static readonly HashFunction defaultHashFunction = new Sha1();
13
14 private HashFunction hashFunction = HashFunction;
15 private bool configured;
16
20 public MGF1()
21 : this(defaultHashFunction)
22 {
23 }
24
28 public override string Oid => "1.2.840.113549.1.1.8";
29
33 public override bool IsConfigured => this.configured;
34
40 public override bool Configure(Vector SecurityInfo)
41 {
42 if (SecurityInfo.Length >= 2)
43 {
44 if (SecurityInfo[1] is not HashFunction HashFunction)
45 {
46 if (SecurityInfo[1] is Vector v &&
47 v.Length == 1 &&
48 v.FirstElement is HashFunction HashFunction2)
49 {
50 HashFunction = HashFunction2;
51 }
52 else
53 return false;
54 }
55
56 this.hashFunction = HashFunction;
57 }
58
59 this.configured = true;
60
61 return true;
62 }
63
65 public override string ToString()
66 {
67 return "MGF1(" + this.hashFunction.ToString() + ")";
68 }
69
76 public override byte[] CalculateMask(byte[] Seed, int Length)
77 {
78 if (Length < 0)
79 throw new ArgumentOutOfRangeException(nameof(Length), "Length must be non-negative.");
80
81 byte[] Result = new byte[Length];
82 byte[] C = new byte[Seed.Length + 4];
83 int i = 0;
84
85 Buffer.BlockCopy(Seed, 0, C, 0, Seed.Length);
86
87 while (true)
88 {
89 byte[] T = this.hashFunction.ComputeHash(C);
90 int d = Math.Min(T.Length, Length - i);
91
92 Buffer.BlockCopy(T, 0, Result, i, d);
93 i += d;
94
95 if (i == Length)
96 return Result;
97
98 d = C.Length;
99 while (d > 0)
100 {
101 if (++C[--d] != 0)
102 break;
103 }
104 }
105 }
106 }
107}
abstract byte[] ComputeHash(byte[] Data)
Computes a Hash Digest from binary data.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.