-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCertHelpers.cs
More file actions
78 lines (66 loc) · 2.1 KB
/
CertHelpers.cs
File metadata and controls
78 lines (66 loc) · 2.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#region
using System;
using System.IO;
#endregion
namespace Ultz.Extensions.PrivacyEnhancedMail
{
public class CertHelpers
{
/// <summary>
/// This helper function parses an integer size from the reader using the ASN.1 format
/// </summary>
/// <param name="rd"></param>
/// <returns></returns>
public static int DecodeIntegerSize(BinaryReader rd)
{
byte byteValue;
int count;
byteValue = rd.ReadByte();
if (byteValue != 0x02) // indicates an ASN.1 integer value follows
{
return 0;
}
byteValue = rd.ReadByte();
if (byteValue == 0x81)
{
count = rd.ReadByte(); // data size is the following byte
}
else if (byteValue == 0x82)
{
var hi = rd.ReadByte(); // data size in next 2 bytes
var lo = rd.ReadByte();
count = BitConverter.ToUInt16(new[] {lo, hi}, 0);
}
else
{
count = byteValue; // we already have the data size
}
//remove high order zeros in data
while (rd.ReadByte() == 0x00)
{
count -= 1;
}
rd.BaseStream.Seek(-1, SeekOrigin.Current);
return count;
}
/// <summary>
/// </summary>
/// <param name="inputBytes"></param>
/// <param name="alignSize"></param>
/// <returns></returns>
public static byte[] AlignBytes(byte[] inputBytes, int alignSize)
{
var inputBytesSize = inputBytes.Length;
if (alignSize != -1 && inputBytesSize < alignSize)
{
var buf = new byte[alignSize];
for (var i = 0; i < inputBytesSize; ++i)
{
buf[i + (alignSize - inputBytesSize)] = inputBytes[i];
}
return buf;
}
return inputBytes; // Already aligned, or doesn't need alignment
}
}
}