| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Security.Cryptography; |
| | | 4 | | |
| | | 5 | | namespace Renci.SshNet.Common |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Describes object identifier for DER encoding. |
| | | 9 | | /// </summary> |
| | | 10 | | #pragma warning disable CA1815 // Override equals and operator equals on value types |
| | | 11 | | public struct ObjectIdentifier |
| | | 12 | | #pragma warning restore CA1815 // Override equals and operator equals on value types |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the object identifier. |
| | | 16 | | /// </summary> |
| | 12298 | 17 | | public ulong[] Identifiers { get; private set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="ObjectIdentifier"/> struct. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="identifiers">The identifiers.</param> |
| | | 23 | | /// <exception cref="ArgumentNullException"><paramref name="identifiers"/> is <see langword="null"/>.</exception |
| | | 24 | | /// <exception cref="ArgumentException"><paramref name="identifiers"/> has less than two elements.</exception> |
| | | 25 | | public ObjectIdentifier(params ulong[] identifiers) |
| | 2774 | 26 | | { |
| | 2774 | 27 | | if (identifiers is null) |
| | 3 | 28 | | { |
| | 3 | 29 | | throw new ArgumentNullException(nameof(identifiers)); |
| | | 30 | | } |
| | | 31 | | |
| | 2771 | 32 | | if (identifiers.Length < 2) |
| | 3 | 33 | | { |
| | 3 | 34 | | throw new ArgumentException("Must contain at least two elements.", nameof(identifiers)); |
| | | 35 | | } |
| | | 36 | | |
| | 2768 | 37 | | Identifiers = identifiers; |
| | 2768 | 38 | | } |
| | | 39 | | |
| | | 40 | | internal static ObjectIdentifier FromHashAlgorithmName(HashAlgorithmName hashAlgorithmName) |
| | 2771 | 41 | | { |
| | 2771 | 42 | | var oid = CryptoConfig.MapNameToOID(hashAlgorithmName.Name); |
| | | 43 | | |
| | 2771 | 44 | | if (oid is null) |
| | 3 | 45 | | { |
| | 3 | 46 | | throw new ArgumentException($"Could not map `{hashAlgorithmName}` to OID.", nameof(hashAlgorithmName)); |
| | | 47 | | } |
| | | 48 | | |
| | 2768 | 49 | | var identifiers = oid.Split('.').Select(ulong.Parse).ToArray(); |
| | | 50 | | |
| | 2768 | 51 | | return new ObjectIdentifier(identifiers); |
| | 2768 | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |