| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Security.Cryptography; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// An algorithm used for encryption and decryption. |
| | 12 | | /// </summary> |
| | 13 | | public readonly struct EncryptionAlgorithm : IEquatable<EncryptionAlgorithm> |
| | 14 | | { |
| | 15 | | internal const string Rsa15Value = "RSA1_5"; |
| | 16 | | internal const string RsaOaepValue = "RSA-OAEP"; |
| | 17 | | internal const string RsaOaep256Value = "RSA-OAEP-256"; |
| | 18 | |
|
| | 19 | | private readonly string _value; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Initializes a new instance of the <see cref="EncryptionAlgorithm"/> structure. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="value">The string value of the instance.</param> |
| | 25 | | public EncryptionAlgorithm(string value) |
| | 26 | | { |
| 26 | 27 | | _value = value ?? throw new ArgumentNullException(nameof(value)); |
| 26 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Gets an RSA1_5 <see cref="EncryptionAlgorithm"/>. |
| | 32 | | /// </summary> |
| 10 | 33 | | public static EncryptionAlgorithm Rsa15 { get; } = new EncryptionAlgorithm(Rsa15Value); |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Gets an RSA-OAEP <see cref="EncryptionAlgorithm"/>. |
| | 37 | | /// </summary> |
| 18 | 38 | | public static EncryptionAlgorithm RsaOaep { get; } = new EncryptionAlgorithm(RsaOaepValue); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets an RSA-OAEP256 <see cref="EncryptionAlgorithm"/>. |
| | 42 | | /// </summary> |
| 14 | 43 | | public static EncryptionAlgorithm RsaOaep256 { get; } = new EncryptionAlgorithm(RsaOaep256Value); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Determines if two <see cref="EncryptionAlgorithm"/> values are the same. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="left">The first <see cref="EncryptionAlgorithm"/> to compare.</param> |
| | 49 | | /// <param name="right">The second <see cref="EncryptionAlgorithm"/> to compare.</param> |
| | 50 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur |
| 0 | 51 | | public static bool operator ==(EncryptionAlgorithm left, EncryptionAlgorithm right) => left.Equals(right); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Determines if two <see cref="EncryptionAlgorithm"/> values are different. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="left">The first <see cref="EncryptionAlgorithm"/> to compare.</param> |
| | 57 | | /// <param name="right">The second <see cref="EncryptionAlgorithm"/> to compare.</param> |
| | 58 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu |
| 0 | 59 | | public static bool operator !=(EncryptionAlgorithm left, EncryptionAlgorithm right) => !left.Equals(right); |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Converts a string to a <see cref="EncryptionAlgorithm"/>. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="value">The string value to convert.</param> |
| 20 | 65 | | public static implicit operator EncryptionAlgorithm(string value) => new EncryptionAlgorithm(value); |
| | 66 | |
|
| | 67 | | /// <inheritdoc/> |
| | 68 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 69 | | public override bool Equals(object obj) => obj is EncryptionAlgorithm other && Equals(other); |
| | 70 | |
|
| | 71 | | /// <inheritdoc/> |
| 24 | 72 | | public bool Equals(EncryptionAlgorithm other) => string.Equals(_value, other._value, StringComparison.Ordinal); |
| | 73 | |
|
| | 74 | | /// <inheritdoc/> |
| | 75 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 76 | | public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| | 77 | |
|
| | 78 | | /// <inheritdoc/> |
| 96 | 79 | | public override string ToString() => _value; |
| | 80 | |
|
| 8 | 81 | | internal RSAEncryptionPadding GetRsaEncryptionPadding() => _value switch |
| 8 | 82 | | { |
| 0 | 83 | | Rsa15Value => RSAEncryptionPadding.Pkcs1, |
| 0 | 84 | | RsaOaepValue => RSAEncryptionPadding.OaepSHA1, |
| 0 | 85 | | RsaOaep256Value => RSAEncryptionPadding.OaepSHA256, |
| 16 | 86 | | _ => null, |
| 8 | 87 | | }; |
| | 88 | | } |
| | 89 | | } |