Repository navigation
Expand file tree
/
Copy pathMultipleTransformsNonBlock.cs
More file actions
64 lines (51 loc) · 1.68 KB
/
Copy pathMultipleTransformsNonBlock.cs
File metadata and controls
64 lines (51 loc) · 1.68 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
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace HashLib
{
internal abstract class MultipleTransformNonBlock : Hash, INonBlockHash
{
private List<ArraySegment<byte>> m_list = new List<ArraySegment<byte>>();
public MultipleTransformNonBlock(int a_hash_size, int a_block_size)
: base(a_hash_size, a_block_size)
{
}
public override void Initialize()
{
m_list.Clear();
}
public override void TransformBytes(byte[] a_data, int a_index, int a_length)
{
Debug.Assert(a_index >= 0);
Debug.Assert(a_length >= 0);
Debug.Assert(a_index + a_length <= a_data.Length);
m_list.Add(new ArraySegment<byte>(a_data, a_index, a_length));
}
public override HashResult TransformFinal()
{
HashResult result = ComputeAggregatedBytes(Aggregate());
Initialize();
return result;
}
private byte[] Aggregate()
{
int sum = 0;
foreach (ArraySegment<byte> seg in m_list)
sum += seg.Count;
byte[] res = new byte[sum];
int index = 0;
foreach (ArraySegment<byte> seg in m_list)
{
Array.Copy(seg.Array, seg.Offset, res, index, seg.Count);
index += seg.Count;
}
return res;
}
public override HashResult ComputeBytes(byte[] a_data)
{
Initialize();
return ComputeAggregatedBytes(a_data);
}
protected abstract HashResult ComputeAggregatedBytes(byte[] a_data);
}
}