mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathCmsDigestedData.cs
More file actions
70 lines (56 loc) · 2.27 KB
/
Copy pathCmsDigestedData.cs
File metadata and controls
70 lines (56 loc) · 2.27 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
using System;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Cms
{
/// <summary>Containing class for a CMS DigestedData object.</summary>
public sealed class CmsDigestedData
{
private readonly ContentInfo m_contentInfo;
private readonly DigestedData m_digestedData;
public CmsDigestedData(byte[] digestedData)
: this(CmsUtilities.ReadContentInfo(digestedData))
{
}
public CmsDigestedData(Stream digestedData)
: this(CmsUtilities.ReadContentInfo(digestedData))
{
}
public CmsDigestedData(ContentInfo contentInfo)
{
m_contentInfo = contentInfo ?? throw new ArgumentNullException(nameof(contentInfo));
m_digestedData = CmsUtilities.SafeGetContent(contentInfo, DigestedData.GetInstance);
}
public ContentInfo ContentInfo => m_contentInfo;
public DigestedData DigestedData => m_digestedData;
public DerObjectIdentifier ContentType => m_contentInfo.ContentType;
public AlgorithmIdentifier DigestAlgorithm => m_digestedData.DigestAlgorithm;
/// <exception cref="CmsException"/>
public CmsTypedData GetDigestedContent() => CmsUtilities.ProcessContentOctetString(m_digestedData.EncapContentInfo);
public byte[] GetEncoded() => m_contentInfo.GetEncoded();
/// <exception cref="CmsException"/>
public bool Verify()
{
ContentInfo encapContentInfo = m_digestedData.EncapContentInfo;
Asn1OctetString encapContent = CmsUtilities.SafeGetContent(encapContentInfo, Asn1OctetString.GetInstance);
try
{
var calculated = DigestUtilities.CalculateDigest(m_digestedData.DigestAlgorithm.Algorithm,
encapContent.GetOctets());
return Arrays.AreEqual(m_digestedData.Digest.GetOctets(), calculated);
}
catch (CmsException)
{
throw;
}
catch (Exception e)
{
throw new CmsException("Unable to process content.", e);
}
}
}
}