summary refs log tree commit diff
path: root/crypto/src/asn1/esf/OtherHash.cs
blob: 0d888823458556b6e33b8df335bd24c260dd5685 (plain) (blame)
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
using System;

using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Esf
{
    /// <remarks>
    /// <code>
    /// OtherHash ::= CHOICE {
    ///		sha1Hash	OtherHashValue, -- This contains a SHA-1 hash
    /// 	otherHash	OtherHashAlgAndValue
    ///	}
    ///	
    ///	OtherHashValue ::= OCTET STRING
    /// </code>
    /// </remarks>
    public class OtherHash
		: Asn1Encodable, IAsn1Choice
	{
        public static OtherHash GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is OtherHash otherHash)
                return otherHash;
            if (obj is Asn1OctetString asn1OctetString)
                return new OtherHash(asn1OctetString);
            return new OtherHash(OtherHashAlgAndValue.GetInstance(obj));
        }

        public static OtherHash GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return Asn1Utilities.GetInstanceFromChoice(taggedObject, declaredExplicit, GetInstance);
        }

        private readonly Asn1OctetString m_sha1Hash;
        private readonly OtherHashAlgAndValue m_otherHash;

        public OtherHash(byte[] sha1Hash)
		{
			if (sha1Hash == null)
				throw new ArgumentNullException(nameof(sha1Hash));

			m_sha1Hash = new DerOctetString(sha1Hash);
		}

		public OtherHash(Asn1OctetString sha1Hash)
		{
			m_sha1Hash = sha1Hash ?? throw new ArgumentNullException(nameof(sha1Hash));
		}

		public OtherHash(OtherHashAlgAndValue otherHash)
		{
			m_otherHash = otherHash ?? throw new ArgumentNullException(nameof(otherHash));
        }

		public AlgorithmIdentifier HashAlgorithm =>
			m_otherHash?.HashAlgorithm ?? new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1);

		public byte[] GetHashValue() => m_otherHash?.GetHashValue() ?? m_sha1Hash.GetOctets();

		public override Asn1Object ToAsn1Object() => m_otherHash?.ToAsn1Object() ?? m_sha1Hash;
	}
}